4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
112 #include <sys/timeb.h>
113 #include <mmsystem.h>
114 #define getopt_long_only getopt_long
115 #define memalign(align, size) malloc(size)
119 #if defined(__APPLE__) || defined(main)
121 int qemu_main(int argc
, char **argv
, char **envp
);
122 int main(int argc
, char **argv
)
124 return qemu_main(argc
, argv
, NULL
);
127 #define main qemu_main
129 #endif /* CONFIG_SDL */
133 #define main qemu_main
134 #endif /* CONFIG_COCOA */
137 #include "hw/boards.h"
139 #include "hw/pcmcia.h"
141 #include "hw/audiodev.h"
145 #include "hw/watchdog.h"
146 #include "hw/smbios.h"
154 #include "qemu-timer.h"
155 #include "qemu-char.h"
156 #include "cache-utils.h"
159 #include "audio/audio.h"
160 #include "migration.h"
163 #include "qemu-option.h"
167 #include "exec-all.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 /* Max number of USB devices that can be specified on the commandline. */
179 #define MAX_USB_CMDLINE 8
181 /* Max number of bluetooth switches on the commandline. */
182 #define MAX_BT_CMDLINE 10
184 static const char *data_dir
;
185 const char *bios_name
= NULL
;
186 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
187 to store the VM snapshots */
188 DriveInfo drives_table
[MAX_DRIVES
+1];
190 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
191 static DisplayState
*display_state
;
192 DisplayType display_type
= DT_DEFAULT
;
193 const char* keyboard_layout
= NULL
;
194 int64_t ticks_per_sec
;
197 NICInfo nd_table
[MAX_NICS
];
200 static int rtc_utc
= 1;
201 static int rtc_date_offset
= -1; /* -1 means no change */
202 int cirrus_vga_enabled
= 1;
203 int std_vga_enabled
= 0;
204 int vmsvga_enabled
= 0;
205 int xenfb_enabled
= 0;
207 int graphic_width
= 1024;
208 int graphic_height
= 768;
209 int graphic_depth
= 8;
211 int graphic_width
= 800;
212 int graphic_height
= 600;
213 int graphic_depth
= 15;
215 static int full_screen
= 0;
217 static int no_frame
= 0;
220 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
221 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
222 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
224 int win2k_install_hack
= 0;
230 const char *vnc_display
;
231 int acpi_enabled
= 1;
233 int virtio_balloon
= 1;
234 const char *virtio_balloon_devaddr
;
239 int graphic_rotate
= 0;
243 WatchdogTimerModel
*watchdog
= NULL
;
244 int watchdog_action
= WDT_RESET
;
245 const char *option_rom
[MAX_OPTION_ROMS
];
247 int semihosting_enabled
= 0;
251 const char *qemu_name
;
253 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
254 unsigned int nb_prom_envs
= 0;
255 const char *prom_envs
[MAX_PROM_ENVS
];
258 struct drive_opt drives_opt
[MAX_DRIVES
];
262 uint64_t node_mem
[MAX_NODES
];
263 uint64_t node_cpumask
[MAX_NODES
];
265 static CPUState
*cur_cpu
;
266 static CPUState
*next_cpu
;
267 static int timer_alarm_pending
= 1;
268 /* Conversion factor from emulated instructions to virtual clock ticks. */
269 static int icount_time_shift
;
270 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
271 #define MAX_ICOUNT_SHIFT 10
272 /* Compensate for varying guest execution speed. */
273 static int64_t qemu_icount_bias
;
274 static QEMUTimer
*icount_rt_timer
;
275 static QEMUTimer
*icount_vm_timer
;
276 static QEMUTimer
*nographic_timer
;
278 uint8_t qemu_uuid
[16];
280 static QEMUBootSetHandler
*boot_set_handler
;
281 static void *boot_set_opaque
;
283 /***********************************************************/
284 /* x86 ISA bus support */
286 target_phys_addr_t isa_mem_base
= 0;
289 /***********************************************************/
290 void hw_error(const char *fmt
, ...)
296 fprintf(stderr
, "qemu: hardware error: ");
297 vfprintf(stderr
, fmt
, ap
);
298 fprintf(stderr
, "\n");
299 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
300 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
302 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
304 cpu_dump_state(env
, stderr
, fprintf
, 0);
311 static void set_proc_name(const char *s
)
317 name
[sizeof(name
) - 1] = 0;
318 strncpy(name
, s
, sizeof(name
));
319 /* Could rewrite argv[0] too, but that's a bit more complicated.
320 This simple way is enough for `top'. */
321 prctl(PR_SET_NAME
, name
);
328 static QEMUBalloonEvent
*qemu_balloon_event
;
329 void *qemu_balloon_event_opaque
;
331 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
333 qemu_balloon_event
= func
;
334 qemu_balloon_event_opaque
= opaque
;
337 void qemu_balloon(ram_addr_t target
)
339 if (qemu_balloon_event
)
340 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
343 ram_addr_t
qemu_balloon_status(void)
345 if (qemu_balloon_event
)
346 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
350 /***********************************************************/
353 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
354 static void *qemu_put_kbd_event_opaque
;
355 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
356 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
358 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
360 qemu_put_kbd_event_opaque
= opaque
;
361 qemu_put_kbd_event
= func
;
364 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
365 void *opaque
, int absolute
,
368 QEMUPutMouseEntry
*s
, *cursor
;
370 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
372 s
->qemu_put_mouse_event
= func
;
373 s
->qemu_put_mouse_event_opaque
= opaque
;
374 s
->qemu_put_mouse_event_absolute
= absolute
;
375 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
378 if (!qemu_put_mouse_event_head
) {
379 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
383 cursor
= qemu_put_mouse_event_head
;
384 while (cursor
->next
!= NULL
)
385 cursor
= cursor
->next
;
388 qemu_put_mouse_event_current
= s
;
393 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
395 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
397 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
400 cursor
= qemu_put_mouse_event_head
;
401 while (cursor
!= NULL
&& cursor
!= entry
) {
403 cursor
= cursor
->next
;
406 if (cursor
== NULL
) // does not exist or list empty
408 else if (prev
== NULL
) { // entry is head
409 qemu_put_mouse_event_head
= cursor
->next
;
410 if (qemu_put_mouse_event_current
== entry
)
411 qemu_put_mouse_event_current
= cursor
->next
;
412 qemu_free(entry
->qemu_put_mouse_event_name
);
417 prev
->next
= entry
->next
;
419 if (qemu_put_mouse_event_current
== entry
)
420 qemu_put_mouse_event_current
= prev
;
422 qemu_free(entry
->qemu_put_mouse_event_name
);
426 void kbd_put_keycode(int keycode
)
428 if (qemu_put_kbd_event
) {
429 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
433 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
435 QEMUPutMouseEvent
*mouse_event
;
436 void *mouse_event_opaque
;
439 if (!qemu_put_mouse_event_current
) {
444 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
446 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
449 if (graphic_rotate
) {
450 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
453 width
= graphic_width
- 1;
454 mouse_event(mouse_event_opaque
,
455 width
- dy
, dx
, dz
, buttons_state
);
457 mouse_event(mouse_event_opaque
,
458 dx
, dy
, dz
, buttons_state
);
462 int kbd_mouse_is_absolute(void)
464 if (!qemu_put_mouse_event_current
)
467 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
470 void do_info_mice(Monitor
*mon
)
472 QEMUPutMouseEntry
*cursor
;
475 if (!qemu_put_mouse_event_head
) {
476 monitor_printf(mon
, "No mouse devices connected\n");
480 monitor_printf(mon
, "Mouse devices available:\n");
481 cursor
= qemu_put_mouse_event_head
;
482 while (cursor
!= NULL
) {
483 monitor_printf(mon
, "%c Mouse #%d: %s\n",
484 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
485 index
, cursor
->qemu_put_mouse_event_name
);
487 cursor
= cursor
->next
;
491 void do_mouse_set(Monitor
*mon
, int index
)
493 QEMUPutMouseEntry
*cursor
;
496 if (!qemu_put_mouse_event_head
) {
497 monitor_printf(mon
, "No mouse devices connected\n");
501 cursor
= qemu_put_mouse_event_head
;
502 while (cursor
!= NULL
&& index
!= i
) {
504 cursor
= cursor
->next
;
508 qemu_put_mouse_event_current
= cursor
;
510 monitor_printf(mon
, "Mouse at given index not found\n");
513 /* compute with 96 bit intermediate result: (a*b)/c */
514 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
519 #ifdef WORDS_BIGENDIAN
529 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
530 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
533 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
537 /***********************************************************/
538 /* real time host monotonic timer */
540 #define QEMU_TIMER_BASE 1000000000LL
544 static int64_t clock_freq
;
546 static void init_get_clock(void)
550 ret
= QueryPerformanceFrequency(&freq
);
552 fprintf(stderr
, "Could not calibrate ticks\n");
555 clock_freq
= freq
.QuadPart
;
558 static int64_t get_clock(void)
561 QueryPerformanceCounter(&ti
);
562 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
567 static int use_rt_clock
;
569 static void init_get_clock(void)
572 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
573 || defined(__DragonFly__)
576 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
583 static int64_t get_clock(void)
585 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
586 || defined(__DragonFly__)
589 clock_gettime(CLOCK_MONOTONIC
, &ts
);
590 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
594 /* XXX: using gettimeofday leads to problems if the date
595 changes, so it should be avoided. */
597 gettimeofday(&tv
, NULL
);
598 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
603 /* Return the virtual CPU time, based on the instruction counter. */
604 static int64_t cpu_get_icount(void)
607 CPUState
*env
= cpu_single_env
;;
608 icount
= qemu_icount
;
611 fprintf(stderr
, "Bad clock read\n");
612 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
614 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
617 /***********************************************************/
618 /* guest cycle counter */
620 static int64_t cpu_ticks_prev
;
621 static int64_t cpu_ticks_offset
;
622 static int64_t cpu_clock_offset
;
623 static int cpu_ticks_enabled
;
625 /* return the host CPU cycle counter and handle stop/restart */
626 int64_t cpu_get_ticks(void)
629 return cpu_get_icount();
631 if (!cpu_ticks_enabled
) {
632 return cpu_ticks_offset
;
635 ticks
= cpu_get_real_ticks();
636 if (cpu_ticks_prev
> ticks
) {
637 /* Note: non increasing ticks may happen if the host uses
639 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
641 cpu_ticks_prev
= ticks
;
642 return ticks
+ cpu_ticks_offset
;
646 /* return the host CPU monotonic timer and handle stop/restart */
647 static int64_t cpu_get_clock(void)
650 if (!cpu_ticks_enabled
) {
651 return cpu_clock_offset
;
654 return ti
+ cpu_clock_offset
;
658 /* enable cpu_get_ticks() */
659 void cpu_enable_ticks(void)
661 if (!cpu_ticks_enabled
) {
662 cpu_ticks_offset
-= cpu_get_real_ticks();
663 cpu_clock_offset
-= get_clock();
664 cpu_ticks_enabled
= 1;
668 /* disable cpu_get_ticks() : the clock is stopped. You must not call
669 cpu_get_ticks() after that. */
670 void cpu_disable_ticks(void)
672 if (cpu_ticks_enabled
) {
673 cpu_ticks_offset
= cpu_get_ticks();
674 cpu_clock_offset
= cpu_get_clock();
675 cpu_ticks_enabled
= 0;
679 /***********************************************************/
682 #define QEMU_TIMER_REALTIME 0
683 #define QEMU_TIMER_VIRTUAL 1
687 /* XXX: add frequency */
695 struct QEMUTimer
*next
;
698 struct qemu_alarm_timer
{
702 int (*start
)(struct qemu_alarm_timer
*t
);
703 void (*stop
)(struct qemu_alarm_timer
*t
);
704 void (*rearm
)(struct qemu_alarm_timer
*t
);
708 #define ALARM_FLAG_DYNTICKS 0x1
709 #define ALARM_FLAG_EXPIRED 0x2
711 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
713 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
716 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
718 if (!alarm_has_dynticks(t
))
724 /* TODO: MIN_TIMER_REARM_US should be optimized */
725 #define MIN_TIMER_REARM_US 250
727 static struct qemu_alarm_timer
*alarm_timer
;
731 struct qemu_alarm_win32
{
734 } alarm_win32_data
= {0, -1};
736 static int win32_start_timer(struct qemu_alarm_timer
*t
);
737 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
738 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
742 static int unix_start_timer(struct qemu_alarm_timer
*t
);
743 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
747 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
748 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
749 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
751 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
752 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
754 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
755 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
757 #endif /* __linux__ */
761 /* Correlation between real and virtual time is always going to be
762 fairly approximate, so ignore small variation.
763 When the guest is idle real and virtual time will be aligned in
765 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
767 static void icount_adjust(void)
772 static int64_t last_delta
;
773 /* If the VM is not running, then do nothing. */
777 cur_time
= cpu_get_clock();
778 cur_icount
= qemu_get_clock(vm_clock
);
779 delta
= cur_icount
- cur_time
;
780 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
782 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
783 && icount_time_shift
> 0) {
784 /* The guest is getting too far ahead. Slow time down. */
788 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
789 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
790 /* The guest is getting too far behind. Speed time up. */
794 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
797 static void icount_adjust_rt(void * opaque
)
799 qemu_mod_timer(icount_rt_timer
,
800 qemu_get_clock(rt_clock
) + 1000);
804 static void icount_adjust_vm(void * opaque
)
806 qemu_mod_timer(icount_vm_timer
,
807 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
811 static void init_icount_adjust(void)
813 /* Have both realtime and virtual time triggers for speed adjustment.
814 The realtime trigger catches emulated time passing too slowly,
815 the virtual time trigger catches emulated time passing too fast.
816 Realtime triggers occur even when idle, so use them less frequently
818 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
819 qemu_mod_timer(icount_rt_timer
,
820 qemu_get_clock(rt_clock
) + 1000);
821 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
822 qemu_mod_timer(icount_vm_timer
,
823 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
826 static struct qemu_alarm_timer alarm_timers
[] = {
829 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
830 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
831 /* HPET - if available - is preferred */
832 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
833 /* ...otherwise try RTC */
834 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
836 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
838 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
839 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
840 {"win32", 0, win32_start_timer
,
841 win32_stop_timer
, NULL
, &alarm_win32_data
},
846 static void show_available_alarms(void)
850 printf("Available alarm timers, in order of precedence:\n");
851 for (i
= 0; alarm_timers
[i
].name
; i
++)
852 printf("%s\n", alarm_timers
[i
].name
);
855 static void configure_alarms(char const *opt
)
859 int count
= ARRAY_SIZE(alarm_timers
) - 1;
862 struct qemu_alarm_timer tmp
;
864 if (!strcmp(opt
, "?")) {
865 show_available_alarms();
871 /* Reorder the array */
872 name
= strtok(arg
, ",");
874 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
875 if (!strcmp(alarm_timers
[i
].name
, name
))
880 fprintf(stderr
, "Unknown clock %s\n", name
);
889 tmp
= alarm_timers
[i
];
890 alarm_timers
[i
] = alarm_timers
[cur
];
891 alarm_timers
[cur
] = tmp
;
895 name
= strtok(NULL
, ",");
901 /* Disable remaining timers */
902 for (i
= cur
; i
< count
; i
++)
903 alarm_timers
[i
].name
= NULL
;
905 show_available_alarms();
913 static QEMUTimer
*active_timers
[2];
915 static QEMUClock
*qemu_new_clock(int type
)
918 clock
= qemu_mallocz(sizeof(QEMUClock
));
923 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
927 ts
= qemu_mallocz(sizeof(QEMUTimer
));
934 void qemu_free_timer(QEMUTimer
*ts
)
939 /* stop a timer, but do not dealloc it */
940 void qemu_del_timer(QEMUTimer
*ts
)
944 /* NOTE: this code must be signal safe because
945 qemu_timer_expired() can be called from a signal. */
946 pt
= &active_timers
[ts
->clock
->type
];
959 /* modify the current timer so that it will be fired when current_time
960 >= expire_time. The corresponding callback will be called. */
961 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
967 /* add the timer in the sorted list */
968 /* NOTE: this code must be signal safe because
969 qemu_timer_expired() can be called from a signal. */
970 pt
= &active_timers
[ts
->clock
->type
];
975 if (t
->expire_time
> expire_time
)
979 ts
->expire_time
= expire_time
;
983 /* Rearm if necessary */
984 if (pt
== &active_timers
[ts
->clock
->type
]) {
985 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
986 qemu_rearm_alarm_timer(alarm_timer
);
988 /* Interrupt execution to force deadline recalculation. */
994 int qemu_timer_pending(QEMUTimer
*ts
)
997 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1004 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1008 return (timer_head
->expire_time
<= current_time
);
1011 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1017 if (!ts
|| ts
->expire_time
> current_time
)
1019 /* remove timer from the list before calling the callback */
1020 *ptimer_head
= ts
->next
;
1023 /* run the callback (the timer list can be modified) */
1028 int64_t qemu_get_clock(QEMUClock
*clock
)
1030 switch(clock
->type
) {
1031 case QEMU_TIMER_REALTIME
:
1032 return get_clock() / 1000000;
1034 case QEMU_TIMER_VIRTUAL
:
1036 return cpu_get_icount();
1038 return cpu_get_clock();
1043 static void init_timers(void)
1046 ticks_per_sec
= QEMU_TIMER_BASE
;
1047 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1048 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1052 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1054 uint64_t expire_time
;
1056 if (qemu_timer_pending(ts
)) {
1057 expire_time
= ts
->expire_time
;
1061 qemu_put_be64(f
, expire_time
);
1064 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1066 uint64_t expire_time
;
1068 expire_time
= qemu_get_be64(f
);
1069 if (expire_time
!= -1) {
1070 qemu_mod_timer(ts
, expire_time
);
1076 static void timer_save(QEMUFile
*f
, void *opaque
)
1078 if (cpu_ticks_enabled
) {
1079 hw_error("cannot save state if virtual timers are running");
1081 qemu_put_be64(f
, cpu_ticks_offset
);
1082 qemu_put_be64(f
, ticks_per_sec
);
1083 qemu_put_be64(f
, cpu_clock_offset
);
1086 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1088 if (version_id
!= 1 && version_id
!= 2)
1090 if (cpu_ticks_enabled
) {
1093 cpu_ticks_offset
=qemu_get_be64(f
);
1094 ticks_per_sec
=qemu_get_be64(f
);
1095 if (version_id
== 2) {
1096 cpu_clock_offset
=qemu_get_be64(f
);
1101 static void qemu_event_increment(void);
1104 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1105 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1108 static void host_alarm_handler(int host_signum
)
1112 #define DISP_FREQ 1000
1114 static int64_t delta_min
= INT64_MAX
;
1115 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1117 ti
= qemu_get_clock(vm_clock
);
1118 if (last_clock
!= 0) {
1119 delta
= ti
- last_clock
;
1120 if (delta
< delta_min
)
1122 if (delta
> delta_max
)
1125 if (++count
== DISP_FREQ
) {
1126 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1127 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1128 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1129 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1130 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1132 delta_min
= INT64_MAX
;
1140 if (alarm_has_dynticks(alarm_timer
) ||
1142 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1143 qemu_get_clock(vm_clock
))) ||
1144 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1145 qemu_get_clock(rt_clock
))) {
1146 qemu_event_increment();
1147 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1149 #ifndef CONFIG_IOTHREAD
1151 /* stop the currently executing cpu because a timer occured */
1154 if (next_cpu
->kqemu_enabled
) {
1155 kqemu_cpu_interrupt(next_cpu
);
1160 timer_alarm_pending
= 1;
1161 qemu_notify_event();
1165 static int64_t qemu_next_deadline(void)
1169 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1170 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1171 qemu_get_clock(vm_clock
);
1173 /* To avoid problems with overflow limit this to 2^32. */
1183 #if defined(__linux__) || defined(_WIN32)
1184 static uint64_t qemu_next_deadline_dyntick(void)
1192 delta
= (qemu_next_deadline() + 999) / 1000;
1194 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1195 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1196 qemu_get_clock(rt_clock
))*1000;
1197 if (rtdelta
< delta
)
1201 if (delta
< MIN_TIMER_REARM_US
)
1202 delta
= MIN_TIMER_REARM_US
;
1210 /* Sets a specific flag */
1211 static int fcntl_setfl(int fd
, int flag
)
1215 flags
= fcntl(fd
, F_GETFL
);
1219 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1225 #if defined(__linux__)
1227 #define RTC_FREQ 1024
1229 static void enable_sigio_timer(int fd
)
1231 struct sigaction act
;
1234 sigfillset(&act
.sa_mask
);
1236 act
.sa_handler
= host_alarm_handler
;
1238 sigaction(SIGIO
, &act
, NULL
);
1239 fcntl_setfl(fd
, O_ASYNC
);
1240 fcntl(fd
, F_SETOWN
, getpid());
1243 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1245 struct hpet_info info
;
1248 fd
= open("/dev/hpet", O_RDONLY
);
1253 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1255 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1256 "error, but for better emulation accuracy type:\n"
1257 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1261 /* Check capabilities */
1262 r
= ioctl(fd
, HPET_INFO
, &info
);
1266 /* Enable periodic mode */
1267 r
= ioctl(fd
, HPET_EPI
, 0);
1268 if (info
.hi_flags
&& (r
< 0))
1271 /* Enable interrupt */
1272 r
= ioctl(fd
, HPET_IE_ON
, 0);
1276 enable_sigio_timer(fd
);
1277 t
->priv
= (void *)(long)fd
;
1285 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1287 int fd
= (long)t
->priv
;
1292 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1295 unsigned long current_rtc_freq
= 0;
1297 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1300 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1301 if (current_rtc_freq
!= RTC_FREQ
&&
1302 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1303 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1304 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1305 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1308 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1314 enable_sigio_timer(rtc_fd
);
1316 t
->priv
= (void *)(long)rtc_fd
;
1321 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1323 int rtc_fd
= (long)t
->priv
;
1328 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1332 struct sigaction act
;
1334 sigfillset(&act
.sa_mask
);
1336 act
.sa_handler
= host_alarm_handler
;
1338 sigaction(SIGALRM
, &act
, NULL
);
1341 * Initialize ev struct to 0 to avoid valgrind complaining
1342 * about uninitialized data in timer_create call
1344 memset(&ev
, 0, sizeof(ev
));
1345 ev
.sigev_value
.sival_int
= 0;
1346 ev
.sigev_notify
= SIGEV_SIGNAL
;
1347 ev
.sigev_signo
= SIGALRM
;
1349 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1350 perror("timer_create");
1352 /* disable dynticks */
1353 fprintf(stderr
, "Dynamic Ticks disabled\n");
1358 t
->priv
= (void *)(long)host_timer
;
1363 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1365 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1367 timer_delete(host_timer
);
1370 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1372 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1373 struct itimerspec timeout
;
1374 int64_t nearest_delta_us
= INT64_MAX
;
1377 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1378 !active_timers
[QEMU_TIMER_VIRTUAL
])
1381 nearest_delta_us
= qemu_next_deadline_dyntick();
1383 /* check whether a timer is already running */
1384 if (timer_gettime(host_timer
, &timeout
)) {
1386 fprintf(stderr
, "Internal timer error: aborting\n");
1389 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1390 if (current_us
&& current_us
<= nearest_delta_us
)
1393 timeout
.it_interval
.tv_sec
= 0;
1394 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1395 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1396 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1397 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1399 fprintf(stderr
, "Internal timer error: aborting\n");
1404 #endif /* defined(__linux__) */
1406 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1408 struct sigaction act
;
1409 struct itimerval itv
;
1413 sigfillset(&act
.sa_mask
);
1415 act
.sa_handler
= host_alarm_handler
;
1417 sigaction(SIGALRM
, &act
, NULL
);
1419 itv
.it_interval
.tv_sec
= 0;
1420 /* for i386 kernel 2.6 to get 1 ms */
1421 itv
.it_interval
.tv_usec
= 999;
1422 itv
.it_value
.tv_sec
= 0;
1423 itv
.it_value
.tv_usec
= 10 * 1000;
1425 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1432 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1434 struct itimerval itv
;
1436 memset(&itv
, 0, sizeof(itv
));
1437 setitimer(ITIMER_REAL
, &itv
, NULL
);
1440 #endif /* !defined(_WIN32) */
1445 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1448 struct qemu_alarm_win32
*data
= t
->priv
;
1451 memset(&tc
, 0, sizeof(tc
));
1452 timeGetDevCaps(&tc
, sizeof(tc
));
1454 if (data
->period
< tc
.wPeriodMin
)
1455 data
->period
= tc
.wPeriodMin
;
1457 timeBeginPeriod(data
->period
);
1459 flags
= TIME_CALLBACK_FUNCTION
;
1460 if (alarm_has_dynticks(t
))
1461 flags
|= TIME_ONESHOT
;
1463 flags
|= TIME_PERIODIC
;
1465 data
->timerId
= timeSetEvent(1, // interval (ms)
1466 data
->period
, // resolution
1467 host_alarm_handler
, // function
1468 (DWORD
)t
, // parameter
1471 if (!data
->timerId
) {
1472 perror("Failed to initialize win32 alarm timer");
1473 timeEndPeriod(data
->period
);
1480 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1482 struct qemu_alarm_win32
*data
= t
->priv
;
1484 timeKillEvent(data
->timerId
);
1485 timeEndPeriod(data
->period
);
1488 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1490 struct qemu_alarm_win32
*data
= t
->priv
;
1491 uint64_t nearest_delta_us
;
1493 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1494 !active_timers
[QEMU_TIMER_VIRTUAL
])
1497 nearest_delta_us
= qemu_next_deadline_dyntick();
1498 nearest_delta_us
/= 1000;
1500 timeKillEvent(data
->timerId
);
1502 data
->timerId
= timeSetEvent(1,
1506 TIME_ONESHOT
| TIME_PERIODIC
);
1508 if (!data
->timerId
) {
1509 perror("Failed to re-arm win32 alarm timer");
1511 timeEndPeriod(data
->period
);
1518 static int init_timer_alarm(void)
1520 struct qemu_alarm_timer
*t
= NULL
;
1523 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1524 t
= &alarm_timers
[i
];
1544 static void quit_timers(void)
1546 alarm_timer
->stop(alarm_timer
);
1550 /***********************************************************/
1551 /* host time/date access */
1552 void qemu_get_timedate(struct tm
*tm
, int offset
)
1559 if (rtc_date_offset
== -1) {
1563 ret
= localtime(&ti
);
1565 ti
-= rtc_date_offset
;
1569 memcpy(tm
, ret
, sizeof(struct tm
));
1572 int qemu_timedate_diff(struct tm
*tm
)
1576 if (rtc_date_offset
== -1)
1578 seconds
= mktimegm(tm
);
1580 seconds
= mktime(tm
);
1582 seconds
= mktimegm(tm
) + rtc_date_offset
;
1584 return seconds
- time(NULL
);
1588 static void socket_cleanup(void)
1593 static int socket_init(void)
1598 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1600 err
= WSAGetLastError();
1601 fprintf(stderr
, "WSAStartup: %d\n", err
);
1604 atexit(socket_cleanup
);
1609 int get_next_param_value(char *buf
, int buf_size
,
1610 const char *tag
, const char **pstr
)
1617 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1621 if (!strcmp(tag
, option
)) {
1622 *pstr
= get_opt_value(buf
, buf_size
, p
);
1623 if (**pstr
== ',') {
1628 p
= get_opt_value(NULL
, 0, p
);
1637 int get_param_value(char *buf
, int buf_size
,
1638 const char *tag
, const char *str
)
1640 return get_next_param_value(buf
, buf_size
, tag
, &str
);
1643 int check_params(char *buf
, int buf_size
,
1644 const char * const *params
, const char *str
)
1650 while (*p
!= '\0') {
1651 p
= get_opt_name(buf
, buf_size
, p
, '=');
1656 for (i
= 0; params
[i
] != NULL
; i
++) {
1657 if (!strcmp(params
[i
], buf
)) {
1661 if (params
[i
] == NULL
) {
1664 p
= get_opt_value(NULL
, 0, p
);
1673 /***********************************************************/
1674 /* Bluetooth support */
1677 static struct HCIInfo
*hci_table
[MAX_NICS
];
1679 static struct bt_vlan_s
{
1680 struct bt_scatternet_s net
;
1682 struct bt_vlan_s
*next
;
1685 /* find or alloc a new bluetooth "VLAN" */
1686 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1688 struct bt_vlan_s
**pvlan
, *vlan
;
1689 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1693 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1695 pvlan
= &first_bt_vlan
;
1696 while (*pvlan
!= NULL
)
1697 pvlan
= &(*pvlan
)->next
;
1702 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1706 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1711 static struct HCIInfo null_hci
= {
1712 .cmd_send
= null_hci_send
,
1713 .sco_send
= null_hci_send
,
1714 .acl_send
= null_hci_send
,
1715 .bdaddr_set
= null_hci_addr_set
,
1718 struct HCIInfo
*qemu_next_hci(void)
1720 if (cur_hci
== nb_hcis
)
1723 return hci_table
[cur_hci
++];
1726 static struct HCIInfo
*hci_init(const char *str
)
1729 struct bt_scatternet_s
*vlan
= 0;
1731 if (!strcmp(str
, "null"))
1734 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1736 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1737 else if (!strncmp(str
, "hci", 3)) {
1740 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1741 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1746 vlan
= qemu_find_bt_vlan(0);
1748 return bt_new_hci(vlan
);
1751 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1756 static int bt_hci_parse(const char *str
)
1758 struct HCIInfo
*hci
;
1761 if (nb_hcis
>= MAX_NICS
) {
1762 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1766 hci
= hci_init(str
);
1775 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1776 hci
->bdaddr_set(hci
, bdaddr
.b
);
1778 hci_table
[nb_hcis
++] = hci
;
1783 static void bt_vhci_add(int vlan_id
)
1785 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1788 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1789 "an empty scatternet %i\n", vlan_id
);
1791 bt_vhci_init(bt_new_hci(vlan
));
1794 static struct bt_device_s
*bt_device_add(const char *opt
)
1796 struct bt_scatternet_s
*vlan
;
1798 char *endp
= strstr(opt
, ",vlan=");
1799 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1802 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1805 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1807 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1812 vlan
= qemu_find_bt_vlan(vlan_id
);
1815 fprintf(stderr
, "qemu: warning: adding a slave device to "
1816 "an empty scatternet %i\n", vlan_id
);
1818 if (!strcmp(devname
, "keyboard"))
1819 return bt_keyboard_init(vlan
);
1821 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1825 static int bt_parse(const char *opt
)
1827 const char *endp
, *p
;
1830 if (strstart(opt
, "hci", &endp
)) {
1831 if (!*endp
|| *endp
== ',') {
1833 if (!strstart(endp
, ",vlan=", 0))
1836 return bt_hci_parse(opt
);
1838 } else if (strstart(opt
, "vhci", &endp
)) {
1839 if (!*endp
|| *endp
== ',') {
1841 if (strstart(endp
, ",vlan=", &p
)) {
1842 vlan
= strtol(p
, (char **) &endp
, 0);
1844 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1848 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1857 } else if (strstart(opt
, "device:", &endp
))
1858 return !bt_device_add(endp
);
1860 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1864 /***********************************************************/
1865 /* QEMU Block devices */
1867 #define HD_ALIAS "index=%d,media=disk"
1868 #define CDROM_ALIAS "index=2,media=cdrom"
1869 #define FD_ALIAS "index=%d,if=floppy"
1870 #define PFLASH_ALIAS "if=pflash"
1871 #define MTD_ALIAS "if=mtd"
1872 #define SD_ALIAS "index=0,if=sd"
1874 static int drive_opt_get_free_idx(void)
1878 for (index
= 0; index
< MAX_DRIVES
; index
++)
1879 if (!drives_opt
[index
].used
) {
1880 drives_opt
[index
].used
= 1;
1887 static int drive_get_free_idx(void)
1891 for (index
= 0; index
< MAX_DRIVES
; index
++)
1892 if (!drives_table
[index
].used
) {
1893 drives_table
[index
].used
= 1;
1900 int drive_add(const char *file
, const char *fmt
, ...)
1903 int index
= drive_opt_get_free_idx();
1905 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
1906 fprintf(stderr
, "qemu: too many drives\n");
1910 drives_opt
[index
].file
= file
;
1912 vsnprintf(drives_opt
[index
].opt
,
1913 sizeof(drives_opt
[0].opt
), fmt
, ap
);
1920 void drive_remove(int index
)
1922 drives_opt
[index
].used
= 0;
1926 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
1930 /* seek interface, bus and unit */
1932 for (index
= 0; index
< MAX_DRIVES
; index
++)
1933 if (drives_table
[index
].type
== type
&&
1934 drives_table
[index
].bus
== bus
&&
1935 drives_table
[index
].unit
== unit
&&
1936 drives_table
[index
].used
)
1942 int drive_get_max_bus(BlockInterfaceType type
)
1948 for (index
= 0; index
< nb_drives
; index
++) {
1949 if(drives_table
[index
].type
== type
&&
1950 drives_table
[index
].bus
> max_bus
)
1951 max_bus
= drives_table
[index
].bus
;
1956 const char *drive_get_serial(BlockDriverState
*bdrv
)
1960 for (index
= 0; index
< nb_drives
; index
++)
1961 if (drives_table
[index
].bdrv
== bdrv
)
1962 return drives_table
[index
].serial
;
1967 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1971 for (index
= 0; index
< nb_drives
; index
++)
1972 if (drives_table
[index
].bdrv
== bdrv
)
1973 return drives_table
[index
].onerror
;
1975 return BLOCK_ERR_STOP_ENOSPC
;
1978 static void bdrv_format_print(void *opaque
, const char *name
)
1980 fprintf(stderr
, " %s", name
);
1983 void drive_uninit(BlockDriverState
*bdrv
)
1987 for (i
= 0; i
< MAX_DRIVES
; i
++)
1988 if (drives_table
[i
].bdrv
== bdrv
) {
1989 drives_table
[i
].bdrv
= NULL
;
1990 drives_table
[i
].used
= 0;
1991 drive_remove(drives_table
[i
].drive_opt_idx
);
1997 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2003 const char *mediastr
= "";
2004 BlockInterfaceType type
;
2005 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2006 int bus_id
, unit_id
;
2007 int cyls
, heads
, secs
, translation
;
2008 BlockDriverState
*bdrv
;
2009 BlockDriver
*drv
= NULL
;
2010 QEMUMachine
*machine
= opaque
;
2014 int bdrv_flags
, onerror
;
2015 const char *devaddr
;
2016 int drives_table_idx
;
2017 char *str
= arg
->opt
;
2018 static const char * const params
[] = { "bus", "unit", "if", "index",
2019 "cyls", "heads", "secs", "trans",
2020 "media", "snapshot", "file",
2021 "cache", "format", "serial",
2025 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2026 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2032 cyls
= heads
= secs
= 0;
2035 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2039 if (machine
->use_scsi
) {
2041 max_devs
= MAX_SCSI_DEVS
;
2042 pstrcpy(devname
, sizeof(devname
), "scsi");
2045 max_devs
= MAX_IDE_DEVS
;
2046 pstrcpy(devname
, sizeof(devname
), "ide");
2050 /* extract parameters */
2052 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2053 bus_id
= strtol(buf
, NULL
, 0);
2055 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2060 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2061 unit_id
= strtol(buf
, NULL
, 0);
2063 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2068 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2069 pstrcpy(devname
, sizeof(devname
), buf
);
2070 if (!strcmp(buf
, "ide")) {
2072 max_devs
= MAX_IDE_DEVS
;
2073 } else if (!strcmp(buf
, "scsi")) {
2075 max_devs
= MAX_SCSI_DEVS
;
2076 } else if (!strcmp(buf
, "floppy")) {
2079 } else if (!strcmp(buf
, "pflash")) {
2082 } else if (!strcmp(buf
, "mtd")) {
2085 } else if (!strcmp(buf
, "sd")) {
2088 } else if (!strcmp(buf
, "virtio")) {
2091 } else if (!strcmp(buf
, "xen")) {
2095 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2100 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2101 index
= strtol(buf
, NULL
, 0);
2103 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2108 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2109 cyls
= strtol(buf
, NULL
, 0);
2112 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2113 heads
= strtol(buf
, NULL
, 0);
2116 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2117 secs
= strtol(buf
, NULL
, 0);
2120 if (cyls
|| heads
|| secs
) {
2121 if (cyls
< 1 || cyls
> 16383) {
2122 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2125 if (heads
< 1 || heads
> 16) {
2126 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2129 if (secs
< 1 || secs
> 63) {
2130 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2135 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2138 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2142 if (!strcmp(buf
, "none"))
2143 translation
= BIOS_ATA_TRANSLATION_NONE
;
2144 else if (!strcmp(buf
, "lba"))
2145 translation
= BIOS_ATA_TRANSLATION_LBA
;
2146 else if (!strcmp(buf
, "auto"))
2147 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2149 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2154 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2155 if (!strcmp(buf
, "disk")) {
2157 } else if (!strcmp(buf
, "cdrom")) {
2158 if (cyls
|| secs
|| heads
) {
2160 "qemu: '%s' invalid physical CHS format\n", str
);
2163 media
= MEDIA_CDROM
;
2165 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2170 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2171 if (!strcmp(buf
, "on"))
2173 else if (!strcmp(buf
, "off"))
2176 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2181 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2182 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2184 else if (!strcmp(buf
, "writethrough"))
2186 else if (!strcmp(buf
, "writeback"))
2189 fprintf(stderr
, "qemu: invalid cache option\n");
2194 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2195 if (strcmp(buf
, "?") == 0) {
2196 fprintf(stderr
, "qemu: Supported formats:");
2197 bdrv_iterate_format(bdrv_format_print
, NULL
);
2198 fprintf(stderr
, "\n");
2201 drv
= bdrv_find_format(buf
);
2203 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2208 if (arg
->file
== NULL
)
2209 get_param_value(file
, sizeof(file
), "file", str
);
2211 pstrcpy(file
, sizeof(file
), arg
->file
);
2213 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2214 memset(serial
, 0, sizeof(serial
));
2216 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2217 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2218 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2219 fprintf(stderr
, "werror is no supported by this format\n");
2222 if (!strcmp(buf
, "ignore"))
2223 onerror
= BLOCK_ERR_IGNORE
;
2224 else if (!strcmp(buf
, "enospc"))
2225 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2226 else if (!strcmp(buf
, "stop"))
2227 onerror
= BLOCK_ERR_STOP_ANY
;
2228 else if (!strcmp(buf
, "report"))
2229 onerror
= BLOCK_ERR_REPORT
;
2231 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2237 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2238 if (type
!= IF_VIRTIO
) {
2239 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2242 devaddr
= strdup(buf
);
2245 /* compute bus and unit according index */
2248 if (bus_id
!= 0 || unit_id
!= -1) {
2250 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2258 unit_id
= index
% max_devs
;
2259 bus_id
= index
/ max_devs
;
2263 /* if user doesn't specify a unit_id,
2264 * try to find the first free
2267 if (unit_id
== -1) {
2269 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2271 if (max_devs
&& unit_id
>= max_devs
) {
2272 unit_id
-= max_devs
;
2280 if (max_devs
&& unit_id
>= max_devs
) {
2281 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2282 str
, unit_id
, max_devs
- 1);
2287 * ignore multiple definitions
2290 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2295 if (type
== IF_IDE
|| type
== IF_SCSI
)
2296 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2298 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2299 devname
, bus_id
, mediastr
, unit_id
);
2301 snprintf(buf
, sizeof(buf
), "%s%s%i",
2302 devname
, mediastr
, unit_id
);
2303 bdrv
= bdrv_new(buf
);
2304 drives_table_idx
= drive_get_free_idx();
2305 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2306 drives_table
[drives_table_idx
].devaddr
= devaddr
;
2307 drives_table
[drives_table_idx
].type
= type
;
2308 drives_table
[drives_table_idx
].bus
= bus_id
;
2309 drives_table
[drives_table_idx
].unit
= unit_id
;
2310 drives_table
[drives_table_idx
].onerror
= onerror
;
2311 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2312 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2322 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2323 bdrv_set_translation_hint(bdrv
, translation
);
2327 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2332 /* FIXME: This isn't really a floppy, but it's a reasonable
2335 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2348 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2349 cache
= 2; /* always use write-back with snapshot */
2351 if (cache
== 0) /* no caching */
2352 bdrv_flags
|= BDRV_O_NOCACHE
;
2353 else if (cache
== 2) /* write-back */
2354 bdrv_flags
|= BDRV_O_CACHE_WB
;
2355 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2356 fprintf(stderr
, "qemu: could not open disk image %s\n",
2360 if (bdrv_key_required(bdrv
))
2362 return drives_table_idx
;
2365 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2367 boot_set_handler
= func
;
2368 boot_set_opaque
= opaque
;
2371 int qemu_boot_set(const char *boot_devices
)
2373 if (!boot_set_handler
) {
2376 return boot_set_handler(boot_set_opaque
, boot_devices
);
2379 static int parse_bootdevices(char *devices
)
2381 /* We just do some generic consistency checks */
2385 for (p
= devices
; *p
!= '\0'; p
++) {
2386 /* Allowed boot devices are:
2387 * a-b: floppy disk drives
2388 * c-f: IDE disk drives
2389 * g-m: machine implementation dependant drives
2390 * n-p: network devices
2391 * It's up to each machine implementation to check if the given boot
2392 * devices match the actual hardware implementation and firmware
2395 if (*p
< 'a' || *p
> 'p') {
2396 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2399 if (bitmap
& (1 << (*p
- 'a'))) {
2400 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2403 bitmap
|= 1 << (*p
- 'a');
2408 static void restore_boot_devices(void *opaque
)
2410 char *standard_boot_devices
= opaque
;
2412 qemu_boot_set(standard_boot_devices
);
2414 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2415 qemu_free(standard_boot_devices
);
2418 static void numa_add(const char *optarg
)
2422 unsigned long long value
, endvalue
;
2425 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2426 if (!strcmp(option
, "node")) {
2427 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2428 nodenr
= nb_numa_nodes
;
2430 nodenr
= strtoull(option
, NULL
, 10);
2433 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2434 node_mem
[nodenr
] = 0;
2436 value
= strtoull(option
, &endptr
, 0);
2438 case 0: case 'M': case 'm':
2445 node_mem
[nodenr
] = value
;
2447 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2448 node_cpumask
[nodenr
] = 0;
2450 value
= strtoull(option
, &endptr
, 10);
2453 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2455 if (*endptr
== '-') {
2456 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2457 if (endvalue
>= 63) {
2460 "only 63 CPUs in NUMA mode supported.\n");
2462 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2467 node_cpumask
[nodenr
] = value
;
2474 /***********************************************************/
2477 static USBPort
*used_usb_ports
;
2478 static USBPort
*free_usb_ports
;
2480 /* ??? Maybe change this to register a hub to keep track of the topology. */
2481 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2482 usb_attachfn attach
)
2484 port
->opaque
= opaque
;
2485 port
->index
= index
;
2486 port
->attach
= attach
;
2487 port
->next
= free_usb_ports
;
2488 free_usb_ports
= port
;
2491 int usb_device_add_dev(USBDevice
*dev
)
2495 /* Find a USB port to add the device to. */
2496 port
= free_usb_ports
;
2500 /* Create a new hub and chain it on. */
2501 free_usb_ports
= NULL
;
2502 port
->next
= used_usb_ports
;
2503 used_usb_ports
= port
;
2505 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2506 usb_attach(port
, hub
);
2507 port
= free_usb_ports
;
2510 free_usb_ports
= port
->next
;
2511 port
->next
= used_usb_ports
;
2512 used_usb_ports
= port
;
2513 usb_attach(port
, dev
);
2517 static void usb_msd_password_cb(void *opaque
, int err
)
2519 USBDevice
*dev
= opaque
;
2522 usb_device_add_dev(dev
);
2524 dev
->handle_destroy(dev
);
2527 static int usb_device_add(const char *devname
, int is_hotplug
)
2532 if (!free_usb_ports
)
2535 if (strstart(devname
, "host:", &p
)) {
2536 dev
= usb_host_device_open(p
);
2537 } else if (!strcmp(devname
, "mouse")) {
2538 dev
= usb_mouse_init();
2539 } else if (!strcmp(devname
, "tablet")) {
2540 dev
= usb_tablet_init();
2541 } else if (!strcmp(devname
, "keyboard")) {
2542 dev
= usb_keyboard_init();
2543 } else if (strstart(devname
, "disk:", &p
)) {
2544 BlockDriverState
*bs
;
2546 dev
= usb_msd_init(p
);
2549 bs
= usb_msd_get_bdrv(dev
);
2550 if (bdrv_key_required(bs
)) {
2553 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2558 } else if (!strcmp(devname
, "wacom-tablet")) {
2559 dev
= usb_wacom_init();
2560 } else if (strstart(devname
, "serial:", &p
)) {
2561 dev
= usb_serial_init(p
);
2562 #ifdef CONFIG_BRLAPI
2563 } else if (!strcmp(devname
, "braille")) {
2564 dev
= usb_baum_init();
2566 } else if (strstart(devname
, "net:", &p
)) {
2569 if (net_client_init(NULL
, "nic", p
) < 0)
2571 nd_table
[nic
].model
= "usb";
2572 dev
= usb_net_init(&nd_table
[nic
]);
2573 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2574 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2575 bt_new_hci(qemu_find_bt_vlan(0)));
2582 return usb_device_add_dev(dev
);
2585 int usb_device_del_addr(int bus_num
, int addr
)
2591 if (!used_usb_ports
)
2597 lastp
= &used_usb_ports
;
2598 port
= used_usb_ports
;
2599 while (port
&& port
->dev
->addr
!= addr
) {
2600 lastp
= &port
->next
;
2608 *lastp
= port
->next
;
2609 usb_attach(port
, NULL
);
2610 dev
->handle_destroy(dev
);
2611 port
->next
= free_usb_ports
;
2612 free_usb_ports
= port
;
2616 static int usb_device_del(const char *devname
)
2621 if (strstart(devname
, "host:", &p
))
2622 return usb_host_device_close(p
);
2624 if (!used_usb_ports
)
2627 p
= strchr(devname
, '.');
2630 bus_num
= strtoul(devname
, NULL
, 0);
2631 addr
= strtoul(p
+ 1, NULL
, 0);
2633 return usb_device_del_addr(bus_num
, addr
);
2636 void do_usb_add(Monitor
*mon
, const char *devname
)
2638 usb_device_add(devname
, 1);
2641 void do_usb_del(Monitor
*mon
, const char *devname
)
2643 usb_device_del(devname
);
2646 void usb_info(Monitor
*mon
)
2650 const char *speed_str
;
2653 monitor_printf(mon
, "USB support not enabled\n");
2657 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2661 switch(dev
->speed
) {
2665 case USB_SPEED_FULL
:
2668 case USB_SPEED_HIGH
:
2675 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2676 0, dev
->addr
, speed_str
, dev
->devname
);
2680 /***********************************************************/
2681 /* PCMCIA/Cardbus */
2683 static struct pcmcia_socket_entry_s
{
2684 PCMCIASocket
*socket
;
2685 struct pcmcia_socket_entry_s
*next
;
2686 } *pcmcia_sockets
= 0;
2688 void pcmcia_socket_register(PCMCIASocket
*socket
)
2690 struct pcmcia_socket_entry_s
*entry
;
2692 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2693 entry
->socket
= socket
;
2694 entry
->next
= pcmcia_sockets
;
2695 pcmcia_sockets
= entry
;
2698 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2700 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2702 ptr
= &pcmcia_sockets
;
2703 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2704 if (entry
->socket
== socket
) {
2710 void pcmcia_info(Monitor
*mon
)
2712 struct pcmcia_socket_entry_s
*iter
;
2714 if (!pcmcia_sockets
)
2715 monitor_printf(mon
, "No PCMCIA sockets\n");
2717 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2718 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2719 iter
->socket
->attached
? iter
->socket
->card_string
:
2723 /***********************************************************/
2724 /* register display */
2726 struct DisplayAllocator default_allocator
= {
2727 defaultallocator_create_displaysurface
,
2728 defaultallocator_resize_displaysurface
,
2729 defaultallocator_free_displaysurface
2732 void register_displaystate(DisplayState
*ds
)
2742 DisplayState
*get_displaystate(void)
2744 return display_state
;
2747 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2749 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2750 return ds
->allocator
;
2755 static void dumb_display_init(void)
2757 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2758 ds
->allocator
= &default_allocator
;
2759 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2760 register_displaystate(ds
);
2763 /***********************************************************/
2766 typedef struct IOHandlerRecord
{
2768 IOCanRWHandler
*fd_read_poll
;
2770 IOHandler
*fd_write
;
2773 /* temporary data */
2775 struct IOHandlerRecord
*next
;
2778 static IOHandlerRecord
*first_io_handler
;
2780 /* XXX: fd_read_poll should be suppressed, but an API change is
2781 necessary in the character devices to suppress fd_can_read(). */
2782 int qemu_set_fd_handler2(int fd
,
2783 IOCanRWHandler
*fd_read_poll
,
2785 IOHandler
*fd_write
,
2788 IOHandlerRecord
**pioh
, *ioh
;
2790 if (!fd_read
&& !fd_write
) {
2791 pioh
= &first_io_handler
;
2796 if (ioh
->fd
== fd
) {
2803 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2807 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2808 ioh
->next
= first_io_handler
;
2809 first_io_handler
= ioh
;
2812 ioh
->fd_read_poll
= fd_read_poll
;
2813 ioh
->fd_read
= fd_read
;
2814 ioh
->fd_write
= fd_write
;
2815 ioh
->opaque
= opaque
;
2821 int qemu_set_fd_handler(int fd
,
2823 IOHandler
*fd_write
,
2826 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2830 /***********************************************************/
2831 /* Polling handling */
2833 typedef struct PollingEntry
{
2836 struct PollingEntry
*next
;
2839 static PollingEntry
*first_polling_entry
;
2841 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2843 PollingEntry
**ppe
, *pe
;
2844 pe
= qemu_mallocz(sizeof(PollingEntry
));
2846 pe
->opaque
= opaque
;
2847 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2852 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2854 PollingEntry
**ppe
, *pe
;
2855 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2857 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2865 /***********************************************************/
2866 /* Wait objects support */
2867 typedef struct WaitObjects
{
2869 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2870 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2871 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2874 static WaitObjects wait_objects
= {0};
2876 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2878 WaitObjects
*w
= &wait_objects
;
2880 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2882 w
->events
[w
->num
] = handle
;
2883 w
->func
[w
->num
] = func
;
2884 w
->opaque
[w
->num
] = opaque
;
2889 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2892 WaitObjects
*w
= &wait_objects
;
2895 for (i
= 0; i
< w
->num
; i
++) {
2896 if (w
->events
[i
] == handle
)
2899 w
->events
[i
] = w
->events
[i
+ 1];
2900 w
->func
[i
] = w
->func
[i
+ 1];
2901 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2909 /***********************************************************/
2910 /* ram save/restore */
2912 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2916 v
= qemu_get_byte(f
);
2919 if (qemu_get_buffer(f
, buf
, len
) != len
)
2923 v
= qemu_get_byte(f
);
2924 memset(buf
, v
, len
);
2930 if (qemu_file_has_error(f
))
2936 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2941 if (qemu_get_be32(f
) != last_ram_offset
)
2943 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2944 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2951 #define BDRV_HASH_BLOCK_SIZE 1024
2952 #define IOBUF_SIZE 4096
2953 #define RAM_CBLOCK_MAGIC 0xfabe
2955 typedef struct RamDecompressState
{
2958 uint8_t buf
[IOBUF_SIZE
];
2959 } RamDecompressState
;
2961 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2964 memset(s
, 0, sizeof(*s
));
2966 ret
= inflateInit(&s
->zstream
);
2972 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2976 s
->zstream
.avail_out
= len
;
2977 s
->zstream
.next_out
= buf
;
2978 while (s
->zstream
.avail_out
> 0) {
2979 if (s
->zstream
.avail_in
== 0) {
2980 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2982 clen
= qemu_get_be16(s
->f
);
2983 if (clen
> IOBUF_SIZE
)
2985 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2986 s
->zstream
.avail_in
= clen
;
2987 s
->zstream
.next_in
= s
->buf
;
2989 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2990 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2997 static void ram_decompress_close(RamDecompressState
*s
)
2999 inflateEnd(&s
->zstream
);
3002 #define RAM_SAVE_FLAG_FULL 0x01
3003 #define RAM_SAVE_FLAG_COMPRESS 0x02
3004 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3005 #define RAM_SAVE_FLAG_PAGE 0x08
3006 #define RAM_SAVE_FLAG_EOS 0x10
3008 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3010 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3011 uint32_t *array
= (uint32_t *)page
;
3014 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3015 if (array
[i
] != val
)
3022 static int ram_save_block(QEMUFile
*f
)
3024 static ram_addr_t current_addr
= 0;
3025 ram_addr_t saved_addr
= current_addr
;
3026 ram_addr_t addr
= 0;
3029 while (addr
< last_ram_offset
) {
3030 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3033 cpu_physical_memory_reset_dirty(current_addr
,
3034 current_addr
+ TARGET_PAGE_SIZE
,
3035 MIGRATION_DIRTY_FLAG
);
3037 p
= qemu_get_ram_ptr(current_addr
);
3039 if (is_dup_page(p
, *p
)) {
3040 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3041 qemu_put_byte(f
, *p
);
3043 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3044 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3050 addr
+= TARGET_PAGE_SIZE
;
3051 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3057 static uint64_t bytes_transferred
= 0;
3059 static ram_addr_t
ram_save_remaining(void)
3062 ram_addr_t count
= 0;
3064 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3065 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3072 uint64_t ram_bytes_remaining(void)
3074 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3077 uint64_t ram_bytes_transferred(void)
3079 return bytes_transferred
;
3082 uint64_t ram_bytes_total(void)
3084 return last_ram_offset
;
3087 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3090 uint64_t bytes_transferred_last
;
3092 uint64_t expected_time
= 0;
3094 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3095 qemu_file_set_error(f
);
3100 /* Make sure all dirty bits are set */
3101 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3102 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3103 cpu_physical_memory_set_dirty(addr
);
3106 /* Enable dirty memory tracking */
3107 cpu_physical_memory_set_dirty_tracking(1);
3109 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3112 bytes_transferred_last
= bytes_transferred
;
3113 bwidth
= get_clock();
3115 while (!qemu_file_rate_limit(f
)) {
3118 ret
= ram_save_block(f
);
3119 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3120 if (ret
== 0) /* no more blocks */
3124 bwidth
= get_clock() - bwidth
;
3125 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3127 /* if we haven't transferred anything this round, force expected_time to a
3128 * a very high value, but without crashing */
3132 /* try transferring iterative blocks of memory */
3136 /* flush all remaining blocks regardless of rate limiting */
3137 while (ram_save_block(f
) != 0) {
3138 bytes_transferred
+= TARGET_PAGE_SIZE
;
3140 cpu_physical_memory_set_dirty_tracking(0);
3143 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3145 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3147 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3150 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3152 RamDecompressState s1
, *s
= &s1
;
3156 if (ram_decompress_open(s
, f
) < 0)
3158 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3159 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3160 fprintf(stderr
, "Error while reading ram block header\n");
3164 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3165 BDRV_HASH_BLOCK_SIZE
) < 0) {
3166 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3171 printf("Error block header\n");
3175 ram_decompress_close(s
);
3180 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3185 if (version_id
== 1)
3186 return ram_load_v1(f
, opaque
);
3188 if (version_id
== 2) {
3189 if (qemu_get_be32(f
) != last_ram_offset
)
3191 return ram_load_dead(f
, opaque
);
3194 if (version_id
!= 3)
3198 addr
= qemu_get_be64(f
);
3200 flags
= addr
& ~TARGET_PAGE_MASK
;
3201 addr
&= TARGET_PAGE_MASK
;
3203 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3204 if (addr
!= last_ram_offset
)
3208 if (flags
& RAM_SAVE_FLAG_FULL
) {
3209 if (ram_load_dead(f
, opaque
) < 0)
3213 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3214 uint8_t ch
= qemu_get_byte(f
);
3215 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3218 (!kvm_enabled() || kvm_has_sync_mmu())) {
3219 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3222 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3223 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3224 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3229 void qemu_service_io(void)
3231 qemu_notify_event();
3234 /***********************************************************/
3235 /* bottom halves (can be seen as timers which expire ASAP) */
3246 static QEMUBH
*first_bh
= NULL
;
3248 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3251 bh
= qemu_mallocz(sizeof(QEMUBH
));
3253 bh
->opaque
= opaque
;
3254 bh
->next
= first_bh
;
3259 int qemu_bh_poll(void)
3265 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3266 if (!bh
->deleted
&& bh
->scheduled
) {
3275 /* remove deleted bhs */
3289 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3297 void qemu_bh_schedule(QEMUBH
*bh
)
3303 /* stop the currently executing CPU to execute the BH ASAP */
3304 qemu_notify_event();
3307 void qemu_bh_cancel(QEMUBH
*bh
)
3312 void qemu_bh_delete(QEMUBH
*bh
)
3318 static void qemu_bh_update_timeout(int *timeout
)
3322 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3323 if (!bh
->deleted
&& bh
->scheduled
) {
3325 /* idle bottom halves will be polled at least
3327 *timeout
= MIN(10, *timeout
);
3329 /* non-idle bottom halves will be executed
3338 /***********************************************************/
3339 /* machine registration */
3341 static QEMUMachine
*first_machine
= NULL
;
3342 QEMUMachine
*current_machine
= NULL
;
3344 int qemu_register_machine(QEMUMachine
*m
)
3347 pm
= &first_machine
;
3355 static QEMUMachine
*find_machine(const char *name
)
3359 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3360 if (!strcmp(m
->name
, name
))
3362 if (m
->alias
&& !strcmp(m
->alias
, name
))
3368 static QEMUMachine
*find_default_machine(void)
3372 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3373 if (m
->is_default
) {
3380 /***********************************************************/
3381 /* main execution loop */
3383 static void gui_update(void *opaque
)
3385 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3386 DisplayState
*ds
= opaque
;
3387 DisplayChangeListener
*dcl
= ds
->listeners
;
3391 while (dcl
!= NULL
) {
3392 if (dcl
->gui_timer_interval
&&
3393 dcl
->gui_timer_interval
< interval
)
3394 interval
= dcl
->gui_timer_interval
;
3397 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3400 static void nographic_update(void *opaque
)
3402 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3404 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3407 struct vm_change_state_entry
{
3408 VMChangeStateHandler
*cb
;
3410 LIST_ENTRY (vm_change_state_entry
) entries
;
3413 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3415 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3418 VMChangeStateEntry
*e
;
3420 e
= qemu_mallocz(sizeof (*e
));
3424 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3428 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3430 LIST_REMOVE (e
, entries
);
3434 static void vm_state_notify(int running
, int reason
)
3436 VMChangeStateEntry
*e
;
3438 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3439 e
->cb(e
->opaque
, running
, reason
);
3443 static void resume_all_vcpus(void);
3444 static void pause_all_vcpus(void);
3451 vm_state_notify(1, 0);
3452 qemu_rearm_alarm_timer(alarm_timer
);
3457 /* reset/shutdown handler */
3459 typedef struct QEMUResetEntry
{
3460 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3461 QEMUResetHandler
*func
;
3465 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3466 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3467 static int reset_requested
;
3468 static int shutdown_requested
;
3469 static int powerdown_requested
;
3470 static int debug_requested
;
3471 static int vmstop_requested
;
3473 int qemu_shutdown_requested(void)
3475 int r
= shutdown_requested
;
3476 shutdown_requested
= 0;
3480 int qemu_reset_requested(void)
3482 int r
= reset_requested
;
3483 reset_requested
= 0;
3487 int qemu_powerdown_requested(void)
3489 int r
= powerdown_requested
;
3490 powerdown_requested
= 0;
3494 static int qemu_debug_requested(void)
3496 int r
= debug_requested
;
3497 debug_requested
= 0;
3501 static int qemu_vmstop_requested(void)
3503 int r
= vmstop_requested
;
3504 vmstop_requested
= 0;
3508 static void do_vm_stop(int reason
)
3511 cpu_disable_ticks();
3514 vm_state_notify(0, reason
);
3518 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3520 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3523 re
->opaque
= opaque
;
3524 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3527 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3531 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3532 if (re
->func
== func
&& re
->opaque
== opaque
) {
3533 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3540 void qemu_system_reset(void)
3542 QEMUResetEntry
*re
, *nre
;
3544 /* reset all devices */
3545 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3546 re
->func(re
->opaque
);
3550 void qemu_system_reset_request(void)
3553 shutdown_requested
= 1;
3555 reset_requested
= 1;
3557 qemu_notify_event();
3560 void qemu_system_shutdown_request(void)
3562 shutdown_requested
= 1;
3563 qemu_notify_event();
3566 void qemu_system_powerdown_request(void)
3568 powerdown_requested
= 1;
3569 qemu_notify_event();
3572 #ifdef CONFIG_IOTHREAD
3573 static void qemu_system_vmstop_request(int reason
)
3575 vmstop_requested
= reason
;
3576 qemu_notify_event();
3581 static int io_thread_fd
= -1;
3583 static void qemu_event_increment(void)
3585 static const char byte
= 0;
3587 if (io_thread_fd
== -1)
3590 write(io_thread_fd
, &byte
, sizeof(byte
));
3593 static void qemu_event_read(void *opaque
)
3595 int fd
= (unsigned long)opaque
;
3598 /* Drain the notify pipe */
3601 len
= read(fd
, buffer
, sizeof(buffer
));
3602 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3605 static int qemu_event_init(void)
3614 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3618 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3622 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3623 (void *)(unsigned long)fds
[0]);
3625 io_thread_fd
= fds
[1];
3634 HANDLE qemu_event_handle
;
3636 static void dummy_event_handler(void *opaque
)
3640 static int qemu_event_init(void)
3642 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3643 if (!qemu_event_handle
) {
3644 perror("Failed CreateEvent");
3647 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3651 static void qemu_event_increment(void)
3653 SetEvent(qemu_event_handle
);
3657 static int cpu_can_run(CPUState
*env
)
3666 #ifndef CONFIG_IOTHREAD
3667 static int qemu_init_main_loop(void)
3669 return qemu_event_init();
3672 void qemu_init_vcpu(void *_env
)
3674 CPUState
*env
= _env
;
3681 int qemu_cpu_self(void *env
)
3686 static void resume_all_vcpus(void)
3690 static void pause_all_vcpus(void)
3694 void qemu_cpu_kick(void *env
)
3699 void qemu_notify_event(void)
3701 CPUState
*env
= cpu_single_env
;
3706 if (env
->kqemu_enabled
)
3707 kqemu_cpu_interrupt(env
);
3712 #define qemu_mutex_lock_iothread() do { } while (0)
3713 #define qemu_mutex_unlock_iothread() do { } while (0)
3715 void vm_stop(int reason
)
3720 #else /* CONFIG_IOTHREAD */
3722 #include "qemu-thread.h"
3724 QemuMutex qemu_global_mutex
;
3725 static QemuMutex qemu_fair_mutex
;
3727 static QemuThread io_thread
;
3729 static QemuThread
*tcg_cpu_thread
;
3730 static QemuCond
*tcg_halt_cond
;
3732 static int qemu_system_ready
;
3734 static QemuCond qemu_cpu_cond
;
3736 static QemuCond qemu_system_cond
;
3737 static QemuCond qemu_pause_cond
;
3739 static void block_io_signals(void);
3740 static void unblock_io_signals(void);
3741 static int tcg_has_work(void);
3743 static int qemu_init_main_loop(void)
3747 ret
= qemu_event_init();
3751 qemu_cond_init(&qemu_pause_cond
);
3752 qemu_mutex_init(&qemu_fair_mutex
);
3753 qemu_mutex_init(&qemu_global_mutex
);
3754 qemu_mutex_lock(&qemu_global_mutex
);
3756 unblock_io_signals();
3757 qemu_thread_self(&io_thread
);
3762 static void qemu_wait_io_event(CPUState
*env
)
3764 while (!tcg_has_work())
3765 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3767 qemu_mutex_unlock(&qemu_global_mutex
);
3770 * Users of qemu_global_mutex can be starved, having no chance
3771 * to acquire it since this path will get to it first.
3772 * So use another lock to provide fairness.
3774 qemu_mutex_lock(&qemu_fair_mutex
);
3775 qemu_mutex_unlock(&qemu_fair_mutex
);
3777 qemu_mutex_lock(&qemu_global_mutex
);
3781 qemu_cond_signal(&qemu_pause_cond
);
3785 static int qemu_cpu_exec(CPUState
*env
);
3787 static void *kvm_cpu_thread_fn(void *arg
)
3789 CPUState
*env
= arg
;
3792 qemu_thread_self(env
->thread
);
3794 /* signal CPU creation */
3795 qemu_mutex_lock(&qemu_global_mutex
);
3797 qemu_cond_signal(&qemu_cpu_cond
);
3799 /* and wait for machine initialization */
3800 while (!qemu_system_ready
)
3801 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3804 if (cpu_can_run(env
))
3806 qemu_wait_io_event(env
);
3812 static void tcg_cpu_exec(void);
3814 static void *tcg_cpu_thread_fn(void *arg
)
3816 CPUState
*env
= arg
;
3819 qemu_thread_self(env
->thread
);
3821 /* signal CPU creation */
3822 qemu_mutex_lock(&qemu_global_mutex
);
3823 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3825 qemu_cond_signal(&qemu_cpu_cond
);
3827 /* and wait for machine initialization */
3828 while (!qemu_system_ready
)
3829 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3833 qemu_wait_io_event(cur_cpu
);
3839 void qemu_cpu_kick(void *_env
)
3841 CPUState
*env
= _env
;
3842 qemu_cond_broadcast(env
->halt_cond
);
3844 qemu_thread_signal(env
->thread
, SIGUSR1
);
3847 int qemu_cpu_self(void *env
)
3849 return (cpu_single_env
!= NULL
);
3852 static void cpu_signal(int sig
)
3855 cpu_exit(cpu_single_env
);
3858 static void block_io_signals(void)
3861 struct sigaction sigact
;
3864 sigaddset(&set
, SIGUSR2
);
3865 sigaddset(&set
, SIGIO
);
3866 sigaddset(&set
, SIGALRM
);
3867 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3870 sigaddset(&set
, SIGUSR1
);
3871 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3873 memset(&sigact
, 0, sizeof(sigact
));
3874 sigact
.sa_handler
= cpu_signal
;
3875 sigaction(SIGUSR1
, &sigact
, NULL
);
3878 static void unblock_io_signals(void)
3883 sigaddset(&set
, SIGUSR2
);
3884 sigaddset(&set
, SIGIO
);
3885 sigaddset(&set
, SIGALRM
);
3886 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3889 sigaddset(&set
, SIGUSR1
);
3890 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3893 static void qemu_signal_lock(unsigned int msecs
)
3895 qemu_mutex_lock(&qemu_fair_mutex
);
3897 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3898 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3899 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3902 qemu_mutex_unlock(&qemu_fair_mutex
);
3905 static void qemu_mutex_lock_iothread(void)
3907 if (kvm_enabled()) {
3908 qemu_mutex_lock(&qemu_fair_mutex
);
3909 qemu_mutex_lock(&qemu_global_mutex
);
3910 qemu_mutex_unlock(&qemu_fair_mutex
);
3912 qemu_signal_lock(100);
3915 static void qemu_mutex_unlock_iothread(void)
3917 qemu_mutex_unlock(&qemu_global_mutex
);
3920 static int all_vcpus_paused(void)
3922 CPUState
*penv
= first_cpu
;
3927 penv
= (CPUState
*)penv
->next_cpu
;
3933 static void pause_all_vcpus(void)
3935 CPUState
*penv
= first_cpu
;
3939 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3940 qemu_cpu_kick(penv
);
3941 penv
= (CPUState
*)penv
->next_cpu
;
3944 while (!all_vcpus_paused()) {
3945 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3948 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3949 penv
= (CPUState
*)penv
->next_cpu
;
3954 static void resume_all_vcpus(void)
3956 CPUState
*penv
= first_cpu
;
3961 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3962 qemu_cpu_kick(penv
);
3963 penv
= (CPUState
*)penv
->next_cpu
;
3967 static void tcg_init_vcpu(void *_env
)
3969 CPUState
*env
= _env
;
3970 /* share a single thread for all cpus with TCG */
3971 if (!tcg_cpu_thread
) {
3972 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3973 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3974 qemu_cond_init(env
->halt_cond
);
3975 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3976 while (env
->created
== 0)
3977 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3978 tcg_cpu_thread
= env
->thread
;
3979 tcg_halt_cond
= env
->halt_cond
;
3981 env
->thread
= tcg_cpu_thread
;
3982 env
->halt_cond
= tcg_halt_cond
;
3986 static void kvm_start_vcpu(CPUState
*env
)
3989 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3990 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3991 qemu_cond_init(env
->halt_cond
);
3992 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3993 while (env
->created
== 0)
3994 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3997 void qemu_init_vcpu(void *_env
)
3999 CPUState
*env
= _env
;
4002 kvm_start_vcpu(env
);
4007 void qemu_notify_event(void)
4009 qemu_event_increment();
4012 void vm_stop(int reason
)
4015 qemu_thread_self(&me
);
4017 if (!qemu_thread_equal(&me
, &io_thread
)) {
4018 qemu_system_vmstop_request(reason
);
4020 * FIXME: should not return to device code in case
4021 * vm_stop() has been requested.
4023 if (cpu_single_env
) {
4024 cpu_exit(cpu_single_env
);
4025 cpu_single_env
->stop
= 1;
4036 static void host_main_loop_wait(int *timeout
)
4042 /* XXX: need to suppress polling by better using win32 events */
4044 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4045 ret
|= pe
->func(pe
->opaque
);
4049 WaitObjects
*w
= &wait_objects
;
4051 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4052 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4053 if (w
->func
[ret
- WAIT_OBJECT_0
])
4054 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4056 /* Check for additional signaled events */
4057 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4059 /* Check if event is signaled */
4060 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4061 if(ret2
== WAIT_OBJECT_0
) {
4063 w
->func
[i
](w
->opaque
[i
]);
4064 } else if (ret2
== WAIT_TIMEOUT
) {
4066 err
= GetLastError();
4067 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4070 } else if (ret
== WAIT_TIMEOUT
) {
4072 err
= GetLastError();
4073 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4080 static void host_main_loop_wait(int *timeout
)
4085 void main_loop_wait(int timeout
)
4087 IOHandlerRecord
*ioh
;
4088 fd_set rfds
, wfds
, xfds
;
4092 qemu_bh_update_timeout(&timeout
);
4094 host_main_loop_wait(&timeout
);
4096 /* poll any events */
4097 /* XXX: separate device handlers from system ones */
4102 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4106 (!ioh
->fd_read_poll
||
4107 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4108 FD_SET(ioh
->fd
, &rfds
);
4112 if (ioh
->fd_write
) {
4113 FD_SET(ioh
->fd
, &wfds
);
4119 tv
.tv_sec
= timeout
/ 1000;
4120 tv
.tv_usec
= (timeout
% 1000) * 1000;
4122 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4124 qemu_mutex_unlock_iothread();
4125 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4126 qemu_mutex_lock_iothread();
4128 IOHandlerRecord
**pioh
;
4130 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4131 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4132 ioh
->fd_read(ioh
->opaque
);
4134 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4135 ioh
->fd_write(ioh
->opaque
);
4139 /* remove deleted IO handlers */
4140 pioh
= &first_io_handler
;
4151 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4153 /* rearm timer, if not periodic */
4154 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4155 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4156 qemu_rearm_alarm_timer(alarm_timer
);
4159 /* vm time timers */
4161 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4162 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4163 qemu_get_clock(vm_clock
));
4166 /* real time timers */
4167 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4168 qemu_get_clock(rt_clock
));
4170 /* Check bottom-halves last in case any of the earlier events triggered
4176 static int qemu_cpu_exec(CPUState
*env
)
4179 #ifdef CONFIG_PROFILER
4183 #ifdef CONFIG_PROFILER
4184 ti
= profile_getclock();
4189 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4190 env
->icount_decr
.u16
.low
= 0;
4191 env
->icount_extra
= 0;
4192 count
= qemu_next_deadline();
4193 count
= (count
+ (1 << icount_time_shift
) - 1)
4194 >> icount_time_shift
;
4195 qemu_icount
+= count
;
4196 decr
= (count
> 0xffff) ? 0xffff : count
;
4198 env
->icount_decr
.u16
.low
= decr
;
4199 env
->icount_extra
= count
;
4201 ret
= cpu_exec(env
);
4202 #ifdef CONFIG_PROFILER
4203 qemu_time
+= profile_getclock() - ti
;
4206 /* Fold pending instructions back into the
4207 instruction counter, and clear the interrupt flag. */
4208 qemu_icount
-= (env
->icount_decr
.u16
.low
4209 + env
->icount_extra
);
4210 env
->icount_decr
.u32
= 0;
4211 env
->icount_extra
= 0;
4216 static void tcg_cpu_exec(void)
4220 if (next_cpu
== NULL
)
4221 next_cpu
= first_cpu
;
4222 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4223 CPUState
*env
= cur_cpu
= next_cpu
;
4227 if (timer_alarm_pending
) {
4228 timer_alarm_pending
= 0;
4231 if (cpu_can_run(env
))
4232 ret
= qemu_cpu_exec(env
);
4233 if (ret
== EXCP_DEBUG
) {
4234 gdb_set_stop_cpu(env
);
4235 debug_requested
= 1;
4241 static int cpu_has_work(CPUState
*env
)
4249 if (qemu_cpu_has_work(env
))
4254 static int tcg_has_work(void)
4258 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4259 if (cpu_has_work(env
))
4264 static int qemu_calculate_timeout(void)
4266 #ifndef CONFIG_IOTHREAD
4271 else if (tcg_has_work())
4273 else if (!use_icount
)
4276 /* XXX: use timeout computed from timers */
4279 /* Advance virtual time to the next event. */
4280 if (use_icount
== 1) {
4281 /* When not using an adaptive execution frequency
4282 we tend to get badly out of sync with real time,
4283 so just delay for a reasonable amount of time. */
4286 delta
= cpu_get_icount() - cpu_get_clock();
4289 /* If virtual time is ahead of real time then just
4291 timeout
= (delta
/ 1000000) + 1;
4293 /* Wait for either IO to occur or the next
4295 add
= qemu_next_deadline();
4296 /* We advance the timer before checking for IO.
4297 Limit the amount we advance so that early IO
4298 activity won't get the guest too far ahead. */
4302 add
= (add
+ (1 << icount_time_shift
) - 1)
4303 >> icount_time_shift
;
4305 timeout
= delta
/ 1000000;
4312 #else /* CONFIG_IOTHREAD */
4317 static int vm_can_run(void)
4319 if (powerdown_requested
)
4321 if (reset_requested
)
4323 if (shutdown_requested
)
4325 if (debug_requested
)
4330 static void main_loop(void)
4334 #ifdef CONFIG_IOTHREAD
4335 qemu_system_ready
= 1;
4336 qemu_cond_broadcast(&qemu_system_cond
);
4341 #ifdef CONFIG_PROFILER
4344 #ifndef CONFIG_IOTHREAD
4347 #ifdef CONFIG_PROFILER
4348 ti
= profile_getclock();
4350 main_loop_wait(qemu_calculate_timeout());
4351 #ifdef CONFIG_PROFILER
4352 dev_time
+= profile_getclock() - ti
;
4354 } while (vm_can_run());
4356 if (qemu_debug_requested())
4357 vm_stop(EXCP_DEBUG
);
4358 if (qemu_shutdown_requested()) {
4365 if (qemu_reset_requested()) {
4367 qemu_system_reset();
4370 if (qemu_powerdown_requested())
4371 qemu_system_powerdown();
4372 if ((r
= qemu_vmstop_requested()))
4378 static void version(void)
4380 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4383 static void help(int exitcode
)
4386 printf("usage: %s [options] [disk_image]\n"
4388 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4390 #define DEF(option, opt_arg, opt_enum, opt_help) \
4392 #define DEFHEADING(text) stringify(text) "\n"
4393 #include "qemu-options.h"
4398 "During emulation, the following keys are useful:\n"
4399 "ctrl-alt-f toggle full screen\n"
4400 "ctrl-alt-n switch to virtual console 'n'\n"
4401 "ctrl-alt toggle mouse and keyboard grab\n"
4403 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4408 DEFAULT_NETWORK_SCRIPT
,
4409 DEFAULT_NETWORK_DOWN_SCRIPT
,
4411 DEFAULT_GDBSTUB_PORT
,
4416 #define HAS_ARG 0x0001
4419 #define DEF(option, opt_arg, opt_enum, opt_help) \
4421 #define DEFHEADING(text)
4422 #include "qemu-options.h"
4428 typedef struct QEMUOption
{
4434 static const QEMUOption qemu_options
[] = {
4435 { "h", 0, QEMU_OPTION_h
},
4436 #define DEF(option, opt_arg, opt_enum, opt_help) \
4437 { option, opt_arg, opt_enum },
4438 #define DEFHEADING(text)
4439 #include "qemu-options.h"
4447 struct soundhw soundhw
[] = {
4448 #ifdef HAS_AUDIO_CHOICE
4449 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4455 { .init_isa
= pcspk_audio_init
}
4462 "Creative Sound Blaster 16",
4465 { .init_isa
= SB16_init
}
4469 #ifdef CONFIG_CS4231A
4475 { .init_isa
= cs4231a_init
}
4483 "Yamaha YMF262 (OPL3)",
4485 "Yamaha YM3812 (OPL2)",
4489 { .init_isa
= Adlib_init
}
4496 "Gravis Ultrasound GF1",
4499 { .init_isa
= GUS_init
}
4506 "Intel 82801AA AC97 Audio",
4509 { .init_pci
= ac97_init
}
4513 #ifdef CONFIG_ES1370
4516 "ENSONIQ AudioPCI ES1370",
4519 { .init_pci
= es1370_init
}
4523 #endif /* HAS_AUDIO_CHOICE */
4525 { NULL
, NULL
, 0, 0, { NULL
} }
4528 static void select_soundhw (const char *optarg
)
4532 if (*optarg
== '?') {
4535 printf ("Valid sound card names (comma separated):\n");
4536 for (c
= soundhw
; c
->name
; ++c
) {
4537 printf ("%-11s %s\n", c
->name
, c
->descr
);
4539 printf ("\n-soundhw all will enable all of the above\n");
4540 exit (*optarg
!= '?');
4548 if (!strcmp (optarg
, "all")) {
4549 for (c
= soundhw
; c
->name
; ++c
) {
4557 e
= strchr (p
, ',');
4558 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4560 for (c
= soundhw
; c
->name
; ++c
) {
4561 if (!strncmp (c
->name
, p
, l
)) {
4570 "Unknown sound card name (too big to show)\n");
4573 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4578 p
+= l
+ (e
!= NULL
);
4582 goto show_valid_cards
;
4587 static void select_vgahw (const char *p
)
4591 cirrus_vga_enabled
= 0;
4592 std_vga_enabled
= 0;
4595 if (strstart(p
, "std", &opts
)) {
4596 std_vga_enabled
= 1;
4597 } else if (strstart(p
, "cirrus", &opts
)) {
4598 cirrus_vga_enabled
= 1;
4599 } else if (strstart(p
, "vmware", &opts
)) {
4601 } else if (strstart(p
, "xenfb", &opts
)) {
4603 } else if (!strstart(p
, "none", &opts
)) {
4605 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4609 const char *nextopt
;
4611 if (strstart(opts
, ",retrace=", &nextopt
)) {
4613 if (strstart(opts
, "dumb", &nextopt
))
4614 vga_retrace_method
= VGA_RETRACE_DUMB
;
4615 else if (strstart(opts
, "precise", &nextopt
))
4616 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4617 else goto invalid_vga
;
4618 } else goto invalid_vga
;
4624 static int balloon_parse(const char *arg
)
4629 if (!strcmp(arg
, "none")) {
4631 } else if (!strncmp(arg
, "virtio", 6)) {
4633 if (arg
[6] == ',') {
4635 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4636 virtio_balloon_devaddr
= strdup(buf
);
4647 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4649 exit(STATUS_CONTROL_C_EXIT
);
4654 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4658 if(strlen(str
) != 36)
4661 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4662 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4663 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4669 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4675 #define MAX_NET_CLIENTS 32
4679 static void termsig_handler(int signal
)
4681 qemu_system_shutdown_request();
4684 static void sigchld_handler(int signal
)
4686 waitpid(-1, NULL
, WNOHANG
);
4689 static void sighandler_setup(void)
4691 struct sigaction act
;
4693 memset(&act
, 0, sizeof(act
));
4694 act
.sa_handler
= termsig_handler
;
4695 sigaction(SIGINT
, &act
, NULL
);
4696 sigaction(SIGHUP
, &act
, NULL
);
4697 sigaction(SIGTERM
, &act
, NULL
);
4699 act
.sa_handler
= sigchld_handler
;
4700 act
.sa_flags
= SA_NOCLDSTOP
;
4701 sigaction(SIGCHLD
, &act
, NULL
);
4707 /* Look for support files in the same directory as the executable. */
4708 static char *find_datadir(const char *argv0
)
4714 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4721 while (p
!= buf
&& *p
!= '\\')
4724 if (access(buf
, R_OK
) == 0) {
4725 return qemu_strdup(buf
);
4731 /* Find a likely location for support files using the location of the binary.
4732 For installed binaries this will be "$bindir/../share/qemu". When
4733 running from the build tree this will be "$bindir/../pc-bios". */
4734 #define SHARE_SUFFIX "/share/qemu"
4735 #define BUILD_SUFFIX "/pc-bios"
4736 static char *find_datadir(const char *argv0
)
4746 #if defined(__linux__)
4749 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4755 #elif defined(__FreeBSD__)
4758 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4765 /* If we don't have any way of figuring out the actual executable
4766 location then try argv[0]. */
4771 p
= realpath(argv0
, p
);
4779 max_len
= strlen(dir
) +
4780 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4781 res
= qemu_mallocz(max_len
);
4782 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4783 if (access(res
, R_OK
)) {
4784 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4785 if (access(res
, R_OK
)) {
4799 char *qemu_find_file(int type
, const char *name
)
4805 /* If name contains path separators then try it as a straight path. */
4806 if ((strchr(name
, '/') || strchr(name
, '\\'))
4807 && access(name
, R_OK
) == 0) {
4808 return strdup(name
);
4811 case QEMU_FILE_TYPE_BIOS
:
4814 case QEMU_FILE_TYPE_KEYMAP
:
4815 subdir
= "keymaps/";
4820 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4821 buf
= qemu_mallocz(len
);
4822 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4823 if (access(buf
, R_OK
)) {
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 const char *bt_opts
[MAX_BT_CMDLINE
];
4848 const char *r
, *optarg
;
4849 CharDriverState
*monitor_hd
= NULL
;
4850 const char *monitor_device
;
4851 const char *serial_devices
[MAX_SERIAL_PORTS
];
4852 int serial_device_index
;
4853 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4854 int parallel_device_index
;
4855 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4856 int virtio_console_index
;
4857 const char *loadvm
= NULL
;
4858 QEMUMachine
*machine
;
4859 const char *cpu_model
;
4860 const char *usb_devices
[MAX_USB_CMDLINE
];
4861 int usb_devices_index
;
4866 const char *pid_file
= NULL
;
4867 const char *incoming
= NULL
;
4870 struct passwd
*pwd
= NULL
;
4871 const char *chroot_dir
= NULL
;
4872 const char *run_as
= NULL
;
4875 int show_vnc_port
= 0;
4877 qemu_cache_utils_init(envp
);
4879 LIST_INIT (&vm_change_state_head
);
4882 struct sigaction act
;
4883 sigfillset(&act
.sa_mask
);
4885 act
.sa_handler
= SIG_IGN
;
4886 sigaction(SIGPIPE
, &act
, NULL
);
4889 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4890 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4891 QEMU to run on a single CPU */
4896 h
= GetCurrentProcess();
4897 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4898 for(i
= 0; i
< 32; i
++) {
4899 if (mask
& (1 << i
))
4904 SetProcessAffinityMask(h
, mask
);
4910 module_call_init(MODULE_INIT_MACHINE
);
4911 machine
= find_default_machine();
4913 initrd_filename
= NULL
;
4916 kernel_filename
= NULL
;
4917 kernel_cmdline
= "";
4918 cyls
= heads
= secs
= 0;
4919 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4920 monitor_device
= "vc:80Cx24C";
4922 serial_devices
[0] = "vc:80Cx24C";
4923 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4924 serial_devices
[i
] = NULL
;
4925 serial_device_index
= 0;
4927 parallel_devices
[0] = "vc:80Cx24C";
4928 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4929 parallel_devices
[i
] = NULL
;
4930 parallel_device_index
= 0;
4932 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4933 virtio_consoles
[i
] = NULL
;
4934 virtio_console_index
= 0;
4936 for (i
= 0; i
< MAX_NODES
; i
++) {
4938 node_cpumask
[i
] = 0;
4941 usb_devices_index
= 0;
4955 register_watchdogs();
4963 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4965 const QEMUOption
*popt
;
4968 /* Treat --foo the same as -foo. */
4971 popt
= qemu_options
;
4974 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4978 if (!strcmp(popt
->name
, r
+ 1))
4982 if (popt
->flags
& HAS_ARG
) {
4983 if (optind
>= argc
) {
4984 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4988 optarg
= argv
[optind
++];
4993 switch(popt
->index
) {
4995 machine
= find_machine(optarg
);
4998 printf("Supported machines are:\n");
4999 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5001 printf("%-10s %s (alias of %s)\n",
5002 m
->alias
, m
->desc
, m
->name
);
5003 printf("%-10s %s%s\n",
5005 m
->is_default
? " (default)" : "");
5007 exit(*optarg
!= '?');
5010 case QEMU_OPTION_cpu
:
5011 /* hw initialization will check this */
5012 if (*optarg
== '?') {
5013 /* XXX: implement xxx_cpu_list for targets that still miss it */
5014 #if defined(cpu_list)
5015 cpu_list(stdout
, &fprintf
);
5022 case QEMU_OPTION_initrd
:
5023 initrd_filename
= optarg
;
5025 case QEMU_OPTION_hda
:
5027 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5029 hda_index
= drive_add(optarg
, HD_ALIAS
5030 ",cyls=%d,heads=%d,secs=%d%s",
5031 0, cyls
, heads
, secs
,
5032 translation
== BIOS_ATA_TRANSLATION_LBA
?
5034 translation
== BIOS_ATA_TRANSLATION_NONE
?
5035 ",trans=none" : "");
5037 case QEMU_OPTION_hdb
:
5038 case QEMU_OPTION_hdc
:
5039 case QEMU_OPTION_hdd
:
5040 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5042 case QEMU_OPTION_drive
:
5043 drive_add(NULL
, "%s", optarg
);
5045 case QEMU_OPTION_mtdblock
:
5046 drive_add(optarg
, MTD_ALIAS
);
5048 case QEMU_OPTION_sd
:
5049 drive_add(optarg
, SD_ALIAS
);
5051 case QEMU_OPTION_pflash
:
5052 drive_add(optarg
, PFLASH_ALIAS
);
5054 case QEMU_OPTION_snapshot
:
5057 case QEMU_OPTION_hdachs
:
5061 cyls
= strtol(p
, (char **)&p
, 0);
5062 if (cyls
< 1 || cyls
> 16383)
5067 heads
= strtol(p
, (char **)&p
, 0);
5068 if (heads
< 1 || heads
> 16)
5073 secs
= strtol(p
, (char **)&p
, 0);
5074 if (secs
< 1 || secs
> 63)
5078 if (!strcmp(p
, "none"))
5079 translation
= BIOS_ATA_TRANSLATION_NONE
;
5080 else if (!strcmp(p
, "lba"))
5081 translation
= BIOS_ATA_TRANSLATION_LBA
;
5082 else if (!strcmp(p
, "auto"))
5083 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5086 } else if (*p
!= '\0') {
5088 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5091 if (hda_index
!= -1)
5092 snprintf(drives_opt
[hda_index
].opt
,
5093 sizeof(drives_opt
[hda_index
].opt
),
5094 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5095 0, cyls
, heads
, secs
,
5096 translation
== BIOS_ATA_TRANSLATION_LBA
?
5098 translation
== BIOS_ATA_TRANSLATION_NONE
?
5099 ",trans=none" : "");
5102 case QEMU_OPTION_numa
:
5103 if (nb_numa_nodes
>= MAX_NODES
) {
5104 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5109 case QEMU_OPTION_nographic
:
5110 display_type
= DT_NOGRAPHIC
;
5112 #ifdef CONFIG_CURSES
5113 case QEMU_OPTION_curses
:
5114 display_type
= DT_CURSES
;
5117 case QEMU_OPTION_portrait
:
5120 case QEMU_OPTION_kernel
:
5121 kernel_filename
= optarg
;
5123 case QEMU_OPTION_append
:
5124 kernel_cmdline
= optarg
;
5126 case QEMU_OPTION_cdrom
:
5127 drive_add(optarg
, CDROM_ALIAS
);
5129 case QEMU_OPTION_boot
:
5131 static const char * const params
[] = {
5132 "order", "once", "menu", NULL
5134 char buf
[sizeof(boot_devices
)];
5135 char *standard_boot_devices
;
5138 if (!strchr(optarg
, '=')) {
5140 pstrcpy(buf
, sizeof(buf
), optarg
);
5141 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5143 "qemu: unknown boot parameter '%s' in '%s'\n",
5149 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5150 boot_devices_bitmap
= parse_bootdevices(buf
);
5151 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5154 if (get_param_value(buf
, sizeof(buf
),
5156 boot_devices_bitmap
|= parse_bootdevices(buf
);
5157 standard_boot_devices
= qemu_strdup(boot_devices
);
5158 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5159 qemu_register_reset(restore_boot_devices
,
5160 standard_boot_devices
);
5162 if (get_param_value(buf
, sizeof(buf
),
5164 if (!strcmp(buf
, "on")) {
5166 } else if (!strcmp(buf
, "off")) {
5170 "qemu: invalid option value '%s'\n",
5178 case QEMU_OPTION_fda
:
5179 case QEMU_OPTION_fdb
:
5180 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5183 case QEMU_OPTION_no_fd_bootchk
:
5187 case QEMU_OPTION_net
:
5188 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5189 fprintf(stderr
, "qemu: too many network clients\n");
5192 net_clients
[nb_net_clients
] = optarg
;
5196 case QEMU_OPTION_tftp
:
5197 legacy_tftp_prefix
= optarg
;
5199 case QEMU_OPTION_bootp
:
5200 legacy_bootp_filename
= optarg
;
5203 case QEMU_OPTION_smb
:
5204 net_slirp_smb(optarg
);
5207 case QEMU_OPTION_redir
:
5208 net_slirp_redir(optarg
);
5211 case QEMU_OPTION_bt
:
5212 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5213 fprintf(stderr
, "qemu: too many bluetooth options\n");
5216 bt_opts
[nb_bt_opts
++] = optarg
;
5219 case QEMU_OPTION_audio_help
:
5223 case QEMU_OPTION_soundhw
:
5224 select_soundhw (optarg
);
5230 case QEMU_OPTION_version
:
5234 case QEMU_OPTION_m
: {
5238 value
= strtoul(optarg
, &ptr
, 10);
5240 case 0: case 'M': case 'm':
5247 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5251 /* On 32-bit hosts, QEMU is limited by virtual address space */
5252 if (value
> (2047 << 20)
5253 #ifndef CONFIG_KQEMU
5254 && HOST_LONG_BITS
== 32
5257 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5260 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5261 fprintf(stderr
, "qemu: ram size too large\n");
5270 const CPULogItem
*item
;
5272 mask
= cpu_str_to_log_mask(optarg
);
5274 printf("Log items (comma separated):\n");
5275 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5276 printf("%-10s %s\n", item
->name
, item
->help
);
5284 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5286 case QEMU_OPTION_gdb
:
5287 gdbstub_dev
= optarg
;
5292 case QEMU_OPTION_bios
:
5295 case QEMU_OPTION_singlestep
:
5303 keyboard_layout
= optarg
;
5306 case QEMU_OPTION_localtime
:
5309 case QEMU_OPTION_vga
:
5310 select_vgahw (optarg
);
5312 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5318 w
= strtol(p
, (char **)&p
, 10);
5321 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5327 h
= strtol(p
, (char **)&p
, 10);
5332 depth
= strtol(p
, (char **)&p
, 10);
5333 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5334 depth
!= 24 && depth
!= 32)
5336 } else if (*p
== '\0') {
5337 depth
= graphic_depth
;
5344 graphic_depth
= depth
;
5348 case QEMU_OPTION_echr
:
5351 term_escape_char
= strtol(optarg
, &r
, 0);
5353 printf("Bad argument to echr\n");
5356 case QEMU_OPTION_monitor
:
5357 monitor_device
= optarg
;
5359 case QEMU_OPTION_serial
:
5360 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5361 fprintf(stderr
, "qemu: too many serial ports\n");
5364 serial_devices
[serial_device_index
] = optarg
;
5365 serial_device_index
++;
5367 case QEMU_OPTION_watchdog
:
5368 i
= select_watchdog(optarg
);
5370 exit (i
== 1 ? 1 : 0);
5372 case QEMU_OPTION_watchdog_action
:
5373 if (select_watchdog_action(optarg
) == -1) {
5374 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5378 case QEMU_OPTION_virtiocon
:
5379 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5380 fprintf(stderr
, "qemu: too many virtio consoles\n");
5383 virtio_consoles
[virtio_console_index
] = optarg
;
5384 virtio_console_index
++;
5386 case QEMU_OPTION_parallel
:
5387 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5388 fprintf(stderr
, "qemu: too many parallel ports\n");
5391 parallel_devices
[parallel_device_index
] = optarg
;
5392 parallel_device_index
++;
5394 case QEMU_OPTION_loadvm
:
5397 case QEMU_OPTION_full_screen
:
5401 case QEMU_OPTION_no_frame
:
5404 case QEMU_OPTION_alt_grab
:
5407 case QEMU_OPTION_no_quit
:
5410 case QEMU_OPTION_sdl
:
5411 display_type
= DT_SDL
;
5414 case QEMU_OPTION_pidfile
:
5418 case QEMU_OPTION_win2k_hack
:
5419 win2k_install_hack
= 1;
5421 case QEMU_OPTION_rtc_td_hack
:
5424 case QEMU_OPTION_acpitable
:
5425 if(acpi_table_add(optarg
) < 0) {
5426 fprintf(stderr
, "Wrong acpi table provided\n");
5430 case QEMU_OPTION_smbios
:
5431 if(smbios_entry_add(optarg
) < 0) {
5432 fprintf(stderr
, "Wrong smbios provided\n");
5438 case QEMU_OPTION_enable_kqemu
:
5441 case QEMU_OPTION_kernel_kqemu
:
5446 case QEMU_OPTION_enable_kvm
:
5453 case QEMU_OPTION_usb
:
5456 case QEMU_OPTION_usbdevice
:
5458 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5459 fprintf(stderr
, "Too many USB devices\n");
5462 usb_devices
[usb_devices_index
] = optarg
;
5463 usb_devices_index
++;
5465 case QEMU_OPTION_smp
:
5466 smp_cpus
= atoi(optarg
);
5468 fprintf(stderr
, "Invalid number of CPUs\n");
5472 case QEMU_OPTION_vnc
:
5473 display_type
= DT_VNC
;
5474 vnc_display
= optarg
;
5477 case QEMU_OPTION_no_acpi
:
5480 case QEMU_OPTION_no_hpet
:
5483 case QEMU_OPTION_balloon
:
5484 if (balloon_parse(optarg
) < 0) {
5485 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5490 case QEMU_OPTION_no_reboot
:
5493 case QEMU_OPTION_no_shutdown
:
5496 case QEMU_OPTION_show_cursor
:
5499 case QEMU_OPTION_uuid
:
5500 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5501 fprintf(stderr
, "Fail to parse UUID string."
5502 " Wrong format.\n");
5507 case QEMU_OPTION_daemonize
:
5511 case QEMU_OPTION_option_rom
:
5512 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5513 fprintf(stderr
, "Too many option ROMs\n");
5516 option_rom
[nb_option_roms
] = optarg
;
5519 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5520 case QEMU_OPTION_semihosting
:
5521 semihosting_enabled
= 1;
5524 case QEMU_OPTION_name
:
5525 qemu_name
= qemu_strdup(optarg
);
5527 char *p
= strchr(qemu_name
, ',');
5530 if (strncmp(p
, "process=", 8)) {
5531 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5539 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5540 case QEMU_OPTION_prom_env
:
5541 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5542 fprintf(stderr
, "Too many prom variables\n");
5545 prom_envs
[nb_prom_envs
] = optarg
;
5550 case QEMU_OPTION_old_param
:
5554 case QEMU_OPTION_clock
:
5555 configure_alarms(optarg
);
5557 case QEMU_OPTION_startdate
:
5560 time_t rtc_start_date
;
5561 if (!strcmp(optarg
, "now")) {
5562 rtc_date_offset
= -1;
5564 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5572 } else if (sscanf(optarg
, "%d-%d-%d",
5575 &tm
.tm_mday
) == 3) {
5584 rtc_start_date
= mktimegm(&tm
);
5585 if (rtc_start_date
== -1) {
5587 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5588 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5591 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5595 case QEMU_OPTION_tb_size
:
5596 tb_size
= strtol(optarg
, NULL
, 0);
5600 case QEMU_OPTION_icount
:
5602 if (strcmp(optarg
, "auto") == 0) {
5603 icount_time_shift
= -1;
5605 icount_time_shift
= strtol(optarg
, NULL
, 0);
5608 case QEMU_OPTION_incoming
:
5612 case QEMU_OPTION_chroot
:
5613 chroot_dir
= optarg
;
5615 case QEMU_OPTION_runas
:
5620 case QEMU_OPTION_xen_domid
:
5621 xen_domid
= atoi(optarg
);
5623 case QEMU_OPTION_xen_create
:
5624 xen_mode
= XEN_CREATE
;
5626 case QEMU_OPTION_xen_attach
:
5627 xen_mode
= XEN_ATTACH
;
5634 /* If no data_dir is specified then try to find it relative to the
5637 data_dir
= find_datadir(argv
[0]);
5639 /* If all else fails use the install patch specified when building. */
5641 data_dir
= CONFIG_QEMU_SHAREDIR
;
5644 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5645 if (kvm_allowed
&& kqemu_allowed
) {
5647 "You can not enable both KVM and kqemu at the same time\n");
5652 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5653 if (smp_cpus
> machine
->max_cpus
) {
5654 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5655 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5660 if (display_type
== DT_NOGRAPHIC
) {
5661 if (serial_device_index
== 0)
5662 serial_devices
[0] = "stdio";
5663 if (parallel_device_index
== 0)
5664 parallel_devices
[0] = "null";
5665 if (strncmp(monitor_device
, "vc", 2) == 0)
5666 monitor_device
= "stdio";
5673 if (pipe(fds
) == -1)
5684 len
= read(fds
[0], &status
, 1);
5685 if (len
== -1 && (errno
== EINTR
))
5690 else if (status
== 1) {
5691 fprintf(stderr
, "Could not acquire pidfile\n");
5708 signal(SIGTSTP
, SIG_IGN
);
5709 signal(SIGTTOU
, SIG_IGN
);
5710 signal(SIGTTIN
, SIG_IGN
);
5713 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5716 write(fds
[1], &status
, 1);
5718 fprintf(stderr
, "Could not acquire pid file\n");
5727 if (qemu_init_main_loop()) {
5728 fprintf(stderr
, "qemu_init_main_loop failed\n");
5731 linux_boot
= (kernel_filename
!= NULL
);
5733 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5734 fprintf(stderr
, "-append only allowed with -kernel option\n");
5738 if (!linux_boot
&& initrd_filename
!= NULL
) {
5739 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5743 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5746 if (init_timer_alarm() < 0) {
5747 fprintf(stderr
, "could not initialize alarm timer\n");
5750 if (use_icount
&& icount_time_shift
< 0) {
5752 /* 125MIPS seems a reasonable initial guess at the guest speed.
5753 It will be corrected fairly quickly anyway. */
5754 icount_time_shift
= 3;
5755 init_icount_adjust();
5762 /* init network clients */
5763 if (nb_net_clients
== 0) {
5764 /* if no clients, we use a default config */
5765 net_clients
[nb_net_clients
++] = "nic";
5767 net_clients
[nb_net_clients
++] = "user";
5771 for(i
= 0;i
< nb_net_clients
; i
++) {
5772 if (net_client_parse(net_clients
[i
]) < 0)
5776 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5777 net_set_boot_mask(net_boot
);
5781 /* init the bluetooth world */
5782 for (i
= 0; i
< nb_bt_opts
; i
++)
5783 if (bt_parse(bt_opts
[i
]))
5786 /* init the memory */
5788 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5791 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5792 guest ram allocation. It needs to go away. */
5793 if (kqemu_allowed
) {
5794 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5795 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5796 if (!kqemu_phys_ram_base
) {
5797 fprintf(stderr
, "Could not allocate physical memory\n");
5803 /* init the dynamic translator */
5804 cpu_exec_init_all(tb_size
* 1024 * 1024);
5808 /* we always create the cdrom drive, even if no disk is there */
5810 if (nb_drives_opt
< MAX_DRIVES
)
5811 drive_add(NULL
, CDROM_ALIAS
);
5813 /* we always create at least one floppy */
5815 if (nb_drives_opt
< MAX_DRIVES
)
5816 drive_add(NULL
, FD_ALIAS
, 0);
5818 /* we always create one sd slot, even if no card is in it */
5820 if (nb_drives_opt
< MAX_DRIVES
)
5821 drive_add(NULL
, SD_ALIAS
);
5823 /* open the virtual block devices */
5825 for(i
= 0; i
< nb_drives_opt
; i
++)
5826 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5829 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5830 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5833 /* must be after terminal init, SDL library changes signal handlers */
5837 /* Maintain compatibility with multiple stdio monitors */
5838 if (!strcmp(monitor_device
,"stdio")) {
5839 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5840 const char *devname
= serial_devices
[i
];
5841 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5842 monitor_device
= NULL
;
5844 } else if (devname
&& !strcmp(devname
,"stdio")) {
5845 monitor_device
= NULL
;
5846 serial_devices
[i
] = "mon:stdio";
5852 if (nb_numa_nodes
> 0) {
5855 if (nb_numa_nodes
> smp_cpus
) {
5856 nb_numa_nodes
= smp_cpus
;
5859 /* If no memory size if given for any node, assume the default case
5860 * and distribute the available memory equally across all nodes
5862 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5863 if (node_mem
[i
] != 0)
5866 if (i
== nb_numa_nodes
) {
5867 uint64_t usedmem
= 0;
5869 /* On Linux, the each node's border has to be 8MB aligned,
5870 * the final node gets the rest.
5872 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5873 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5874 usedmem
+= node_mem
[i
];
5876 node_mem
[i
] = ram_size
- usedmem
;
5879 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5880 if (node_cpumask
[i
] != 0)
5883 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5884 * must cope with this anyway, because there are BIOSes out there in
5885 * real machines which also use this scheme.
5887 if (i
== nb_numa_nodes
) {
5888 for (i
= 0; i
< smp_cpus
; i
++) {
5889 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5894 if (kvm_enabled()) {
5897 ret
= kvm_init(smp_cpus
);
5899 fprintf(stderr
, "failed to initialize KVM\n");
5904 if (monitor_device
) {
5905 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5907 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5912 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5913 const char *devname
= serial_devices
[i
];
5914 if (devname
&& strcmp(devname
, "none")) {
5916 snprintf(label
, sizeof(label
), "serial%d", i
);
5917 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5918 if (!serial_hds
[i
]) {
5919 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5926 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5927 const char *devname
= parallel_devices
[i
];
5928 if (devname
&& strcmp(devname
, "none")) {
5930 snprintf(label
, sizeof(label
), "parallel%d", i
);
5931 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5932 if (!parallel_hds
[i
]) {
5933 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5940 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5941 const char *devname
= virtio_consoles
[i
];
5942 if (devname
&& strcmp(devname
, "none")) {
5944 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5945 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5946 if (!virtcon_hds
[i
]) {
5947 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5954 module_call_init(MODULE_INIT_DEVICE
);
5956 if (machine
->compat_props
) {
5957 qdev_prop_register_compat(machine
->compat_props
);
5959 machine
->init(ram_size
, boot_devices
,
5960 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5963 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5964 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5965 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5971 current_machine
= machine
;
5973 /* init USB devices */
5975 for(i
= 0; i
< usb_devices_index
; i
++) {
5976 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5977 fprintf(stderr
, "Warning: could not add USB device %s\n",
5984 dumb_display_init();
5985 /* just use the first displaystate for the moment */
5988 if (display_type
== DT_DEFAULT
) {
5989 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5990 display_type
= DT_SDL
;
5992 display_type
= DT_VNC
;
5993 vnc_display
= "localhost:0,to=99";
5999 switch (display_type
) {
6002 #if defined(CONFIG_CURSES)
6004 curses_display_init(ds
, full_screen
);
6007 #if defined(CONFIG_SDL)
6009 sdl_display_init(ds
, full_screen
, no_frame
);
6011 #elif defined(CONFIG_COCOA)
6013 cocoa_display_init(ds
, full_screen
);
6017 vnc_display_init(ds
);
6018 if (vnc_display_open(ds
, vnc_display
) < 0)
6021 if (show_vnc_port
) {
6022 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6030 dcl
= ds
->listeners
;
6031 while (dcl
!= NULL
) {
6032 if (dcl
->dpy_refresh
!= NULL
) {
6033 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6034 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6039 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6040 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6041 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6044 text_consoles_set_display(display_state
);
6045 qemu_chr_initial_reset();
6047 if (monitor_device
&& monitor_hd
)
6048 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6050 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6051 const char *devname
= serial_devices
[i
];
6052 if (devname
&& strcmp(devname
, "none")) {
6053 if (strstart(devname
, "vc", 0))
6054 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6058 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6059 const char *devname
= parallel_devices
[i
];
6060 if (devname
&& strcmp(devname
, "none")) {
6061 if (strstart(devname
, "vc", 0))
6062 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6066 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6067 const char *devname
= virtio_consoles
[i
];
6068 if (virtcon_hds
[i
] && devname
) {
6069 if (strstart(devname
, "vc", 0))
6070 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6074 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6075 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6081 do_loadvm(cur_mon
, loadvm
);
6085 qemu_start_incoming_migration(incoming
);
6097 len
= write(fds
[1], &status
, 1);
6098 if (len
== -1 && (errno
== EINTR
))
6105 TFR(fd
= open("/dev/null", O_RDWR
));
6111 pwd
= getpwnam(run_as
);
6113 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6119 if (chroot(chroot_dir
) < 0) {
6120 fprintf(stderr
, "chroot failed\n");
6127 if (setgid(pwd
->pw_gid
) < 0) {
6128 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6131 if (setuid(pwd
->pw_uid
) < 0) {
6132 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6135 if (setuid(0) != -1) {
6136 fprintf(stderr
, "Dropping privileges failed\n");