4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
111 #include <mmsystem.h>
115 #if defined(__APPLE__) || defined(main)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 return qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/watchdog.h"
142 #include "hw/smbios.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
156 #include "audio/audio.h"
157 #include "migration.h"
160 #include "qemu-option.h"
161 #include "qemu-config.h"
165 #include "exec-all.h"
167 #include "qemu_socket.h"
169 #include "slirp/libslirp.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
179 to store the VM snapshots */
180 struct drivelist drives
= TAILQ_HEAD_INITIALIZER(drives
);
181 struct driveoptlist driveopts
= TAILQ_HEAD_INITIALIZER(driveopts
);
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 static DisplayState
*display_state
;
184 DisplayType display_type
= DT_DEFAULT
;
185 const char* keyboard_layout
= NULL
;
186 int64_t ticks_per_sec
;
189 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 int vga_interface_type
= VGA_CIRRUS
;
196 int graphic_width
= 1024;
197 int graphic_height
= 768;
198 int graphic_depth
= 8;
200 int graphic_width
= 800;
201 int graphic_height
= 600;
202 int graphic_depth
= 15;
204 static int full_screen
= 0;
206 static int no_frame
= 0;
209 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
210 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
211 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
213 int win2k_install_hack
= 0;
222 const char *vnc_display
;
223 int acpi_enabled
= 1;
225 int virtio_balloon
= 1;
226 const char *virtio_balloon_devaddr
;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
245 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
246 unsigned int nb_prom_envs
= 0;
247 const char *prom_envs
[MAX_PROM_ENVS
];
252 uint64_t node_mem
[MAX_NODES
];
253 uint64_t node_cpumask
[MAX_NODES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int timer_alarm_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
266 static QEMUTimer
*nographic_timer
;
268 uint8_t qemu_uuid
[16];
270 static QEMUBootSetHandler
*boot_set_handler
;
271 static void *boot_set_opaque
;
273 /***********************************************************/
274 /* x86 ISA bus support */
276 target_phys_addr_t isa_mem_base
= 0;
279 /***********************************************************/
280 void hw_error(const char *fmt
, ...)
286 fprintf(stderr
, "qemu: hardware error: ");
287 vfprintf(stderr
, fmt
, ap
);
288 fprintf(stderr
, "\n");
289 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
290 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
292 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
294 cpu_dump_state(env
, stderr
, fprintf
, 0);
301 static void set_proc_name(const char *s
)
303 #if defined(__linux__) && defined(PR_SET_NAME)
307 name
[sizeof(name
) - 1] = 0;
308 strncpy(name
, s
, sizeof(name
));
309 /* Could rewrite argv[0] too, but that's a bit more complicated.
310 This simple way is enough for `top'. */
311 prctl(PR_SET_NAME
, name
);
318 static QEMUBalloonEvent
*qemu_balloon_event
;
319 void *qemu_balloon_event_opaque
;
321 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
323 qemu_balloon_event
= func
;
324 qemu_balloon_event_opaque
= opaque
;
327 void qemu_balloon(ram_addr_t target
)
329 if (qemu_balloon_event
)
330 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
333 ram_addr_t
qemu_balloon_status(void)
335 if (qemu_balloon_event
)
336 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
340 /***********************************************************/
343 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
344 static void *qemu_put_kbd_event_opaque
;
345 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
348 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
350 qemu_put_kbd_event_opaque
= opaque
;
351 qemu_put_kbd_event
= func
;
354 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
355 void *opaque
, int absolute
,
358 QEMUPutMouseEntry
*s
, *cursor
;
360 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
362 s
->qemu_put_mouse_event
= func
;
363 s
->qemu_put_mouse_event_opaque
= opaque
;
364 s
->qemu_put_mouse_event_absolute
= absolute
;
365 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
368 if (!qemu_put_mouse_event_head
) {
369 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
373 cursor
= qemu_put_mouse_event_head
;
374 while (cursor
->next
!= NULL
)
375 cursor
= cursor
->next
;
378 qemu_put_mouse_event_current
= s
;
383 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
385 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
387 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
390 cursor
= qemu_put_mouse_event_head
;
391 while (cursor
!= NULL
&& cursor
!= entry
) {
393 cursor
= cursor
->next
;
396 if (cursor
== NULL
) // does not exist or list empty
398 else if (prev
== NULL
) { // entry is head
399 qemu_put_mouse_event_head
= cursor
->next
;
400 if (qemu_put_mouse_event_current
== entry
)
401 qemu_put_mouse_event_current
= cursor
->next
;
402 qemu_free(entry
->qemu_put_mouse_event_name
);
407 prev
->next
= entry
->next
;
409 if (qemu_put_mouse_event_current
== entry
)
410 qemu_put_mouse_event_current
= prev
;
412 qemu_free(entry
->qemu_put_mouse_event_name
);
416 void kbd_put_keycode(int keycode
)
418 if (qemu_put_kbd_event
) {
419 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
423 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
425 QEMUPutMouseEvent
*mouse_event
;
426 void *mouse_event_opaque
;
429 if (!qemu_put_mouse_event_current
) {
434 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
436 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
439 if (graphic_rotate
) {
440 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
443 width
= graphic_width
- 1;
444 mouse_event(mouse_event_opaque
,
445 width
- dy
, dx
, dz
, buttons_state
);
447 mouse_event(mouse_event_opaque
,
448 dx
, dy
, dz
, buttons_state
);
452 int kbd_mouse_is_absolute(void)
454 if (!qemu_put_mouse_event_current
)
457 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
460 void do_info_mice(Monitor
*mon
)
462 QEMUPutMouseEntry
*cursor
;
465 if (!qemu_put_mouse_event_head
) {
466 monitor_printf(mon
, "No mouse devices connected\n");
470 monitor_printf(mon
, "Mouse devices available:\n");
471 cursor
= qemu_put_mouse_event_head
;
472 while (cursor
!= NULL
) {
473 monitor_printf(mon
, "%c Mouse #%d: %s\n",
474 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
475 index
, cursor
->qemu_put_mouse_event_name
);
477 cursor
= cursor
->next
;
481 void do_mouse_set(Monitor
*mon
, int index
)
483 QEMUPutMouseEntry
*cursor
;
486 if (!qemu_put_mouse_event_head
) {
487 monitor_printf(mon
, "No mouse devices connected\n");
491 cursor
= qemu_put_mouse_event_head
;
492 while (cursor
!= NULL
&& index
!= i
) {
494 cursor
= cursor
->next
;
498 qemu_put_mouse_event_current
= cursor
;
500 monitor_printf(mon
, "Mouse at given index not found\n");
503 /* compute with 96 bit intermediate result: (a*b)/c */
504 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
509 #ifdef HOST_WORDS_BIGENDIAN
519 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
520 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
523 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
527 /***********************************************************/
528 /* real time host monotonic timer */
530 #define QEMU_TIMER_BASE 1000000000LL
534 static int64_t clock_freq
;
536 static void init_get_clock(void)
540 ret
= QueryPerformanceFrequency(&freq
);
542 fprintf(stderr
, "Could not calibrate ticks\n");
545 clock_freq
= freq
.QuadPart
;
548 static int64_t get_clock(void)
551 QueryPerformanceCounter(&ti
);
552 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
557 static int use_rt_clock
;
559 static void init_get_clock(void)
562 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
563 || defined(__DragonFly__)
566 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
573 static int64_t get_clock(void)
575 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
576 || defined(__DragonFly__)
579 clock_gettime(CLOCK_MONOTONIC
, &ts
);
580 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
584 /* XXX: using gettimeofday leads to problems if the date
585 changes, so it should be avoided. */
587 gettimeofday(&tv
, NULL
);
588 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
593 /* Return the virtual CPU time, based on the instruction counter. */
594 static int64_t cpu_get_icount(void)
597 CPUState
*env
= cpu_single_env
;;
598 icount
= qemu_icount
;
601 fprintf(stderr
, "Bad clock read\n");
602 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
604 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
607 /***********************************************************/
608 /* guest cycle counter */
610 static int64_t cpu_ticks_prev
;
611 static int64_t cpu_ticks_offset
;
612 static int64_t cpu_clock_offset
;
613 static int cpu_ticks_enabled
;
615 /* return the host CPU cycle counter and handle stop/restart */
616 int64_t cpu_get_ticks(void)
619 return cpu_get_icount();
621 if (!cpu_ticks_enabled
) {
622 return cpu_ticks_offset
;
625 ticks
= cpu_get_real_ticks();
626 if (cpu_ticks_prev
> ticks
) {
627 /* Note: non increasing ticks may happen if the host uses
629 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
631 cpu_ticks_prev
= ticks
;
632 return ticks
+ cpu_ticks_offset
;
636 /* return the host CPU monotonic timer and handle stop/restart */
637 static int64_t cpu_get_clock(void)
640 if (!cpu_ticks_enabled
) {
641 return cpu_clock_offset
;
644 return ti
+ cpu_clock_offset
;
648 /* enable cpu_get_ticks() */
649 void cpu_enable_ticks(void)
651 if (!cpu_ticks_enabled
) {
652 cpu_ticks_offset
-= cpu_get_real_ticks();
653 cpu_clock_offset
-= get_clock();
654 cpu_ticks_enabled
= 1;
658 /* disable cpu_get_ticks() : the clock is stopped. You must not call
659 cpu_get_ticks() after that. */
660 void cpu_disable_ticks(void)
662 if (cpu_ticks_enabled
) {
663 cpu_ticks_offset
= cpu_get_ticks();
664 cpu_clock_offset
= cpu_get_clock();
665 cpu_ticks_enabled
= 0;
669 /***********************************************************/
672 #define QEMU_TIMER_REALTIME 0
673 #define QEMU_TIMER_VIRTUAL 1
677 /* XXX: add frequency */
685 struct QEMUTimer
*next
;
688 struct qemu_alarm_timer
{
692 int (*start
)(struct qemu_alarm_timer
*t
);
693 void (*stop
)(struct qemu_alarm_timer
*t
);
694 void (*rearm
)(struct qemu_alarm_timer
*t
);
698 #define ALARM_FLAG_DYNTICKS 0x1
699 #define ALARM_FLAG_EXPIRED 0x2
701 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
703 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
706 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
708 if (!alarm_has_dynticks(t
))
714 /* TODO: MIN_TIMER_REARM_US should be optimized */
715 #define MIN_TIMER_REARM_US 250
717 static struct qemu_alarm_timer
*alarm_timer
;
721 struct qemu_alarm_win32
{
724 } alarm_win32_data
= {0, -1};
726 static int win32_start_timer(struct qemu_alarm_timer
*t
);
727 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
728 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
732 static int unix_start_timer(struct qemu_alarm_timer
*t
);
733 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
737 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
738 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
739 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
741 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
742 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
744 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
745 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
747 #endif /* __linux__ */
751 /* Correlation between real and virtual time is always going to be
752 fairly approximate, so ignore small variation.
753 When the guest is idle real and virtual time will be aligned in
755 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
757 static void icount_adjust(void)
762 static int64_t last_delta
;
763 /* If the VM is not running, then do nothing. */
767 cur_time
= cpu_get_clock();
768 cur_icount
= qemu_get_clock(vm_clock
);
769 delta
= cur_icount
- cur_time
;
770 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
772 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
773 && icount_time_shift
> 0) {
774 /* The guest is getting too far ahead. Slow time down. */
778 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
779 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
780 /* The guest is getting too far behind. Speed time up. */
784 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
787 static void icount_adjust_rt(void * opaque
)
789 qemu_mod_timer(icount_rt_timer
,
790 qemu_get_clock(rt_clock
) + 1000);
794 static void icount_adjust_vm(void * opaque
)
796 qemu_mod_timer(icount_vm_timer
,
797 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
801 static void init_icount_adjust(void)
803 /* Have both realtime and virtual time triggers for speed adjustment.
804 The realtime trigger catches emulated time passing too slowly,
805 the virtual time trigger catches emulated time passing too fast.
806 Realtime triggers occur even when idle, so use them less frequently
808 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
809 qemu_mod_timer(icount_rt_timer
,
810 qemu_get_clock(rt_clock
) + 1000);
811 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
812 qemu_mod_timer(icount_vm_timer
,
813 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
816 static struct qemu_alarm_timer alarm_timers
[] = {
819 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
820 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
821 /* HPET - if available - is preferred */
822 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
823 /* ...otherwise try RTC */
824 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
826 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
828 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
829 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
830 {"win32", 0, win32_start_timer
,
831 win32_stop_timer
, NULL
, &alarm_win32_data
},
836 static void show_available_alarms(void)
840 printf("Available alarm timers, in order of precedence:\n");
841 for (i
= 0; alarm_timers
[i
].name
; i
++)
842 printf("%s\n", alarm_timers
[i
].name
);
845 static void configure_alarms(char const *opt
)
849 int count
= ARRAY_SIZE(alarm_timers
) - 1;
852 struct qemu_alarm_timer tmp
;
854 if (!strcmp(opt
, "?")) {
855 show_available_alarms();
861 /* Reorder the array */
862 name
= strtok(arg
, ",");
864 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
865 if (!strcmp(alarm_timers
[i
].name
, name
))
870 fprintf(stderr
, "Unknown clock %s\n", name
);
879 tmp
= alarm_timers
[i
];
880 alarm_timers
[i
] = alarm_timers
[cur
];
881 alarm_timers
[cur
] = tmp
;
885 name
= strtok(NULL
, ",");
891 /* Disable remaining timers */
892 for (i
= cur
; i
< count
; i
++)
893 alarm_timers
[i
].name
= NULL
;
895 show_available_alarms();
903 static QEMUTimer
*active_timers
[2];
905 static QEMUClock
*qemu_new_clock(int type
)
908 clock
= qemu_mallocz(sizeof(QEMUClock
));
913 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
917 ts
= qemu_mallocz(sizeof(QEMUTimer
));
924 void qemu_free_timer(QEMUTimer
*ts
)
929 /* stop a timer, but do not dealloc it */
930 void qemu_del_timer(QEMUTimer
*ts
)
934 /* NOTE: this code must be signal safe because
935 qemu_timer_expired() can be called from a signal. */
936 pt
= &active_timers
[ts
->clock
->type
];
949 /* modify the current timer so that it will be fired when current_time
950 >= expire_time. The corresponding callback will be called. */
951 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
957 /* add the timer in the sorted list */
958 /* NOTE: this code must be signal safe because
959 qemu_timer_expired() can be called from a signal. */
960 pt
= &active_timers
[ts
->clock
->type
];
965 if (t
->expire_time
> expire_time
)
969 ts
->expire_time
= expire_time
;
973 /* Rearm if necessary */
974 if (pt
== &active_timers
[ts
->clock
->type
]) {
975 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
976 qemu_rearm_alarm_timer(alarm_timer
);
978 /* Interrupt execution to force deadline recalculation. */
984 int qemu_timer_pending(QEMUTimer
*ts
)
987 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
994 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
998 return (timer_head
->expire_time
<= current_time
);
1001 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1007 if (!ts
|| ts
->expire_time
> current_time
)
1009 /* remove timer from the list before calling the callback */
1010 *ptimer_head
= ts
->next
;
1013 /* run the callback (the timer list can be modified) */
1018 int64_t qemu_get_clock(QEMUClock
*clock
)
1020 switch(clock
->type
) {
1021 case QEMU_TIMER_REALTIME
:
1022 return get_clock() / 1000000;
1024 case QEMU_TIMER_VIRTUAL
:
1026 return cpu_get_icount();
1028 return cpu_get_clock();
1033 static void init_timers(void)
1036 ticks_per_sec
= QEMU_TIMER_BASE
;
1037 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1038 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1042 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1044 uint64_t expire_time
;
1046 if (qemu_timer_pending(ts
)) {
1047 expire_time
= ts
->expire_time
;
1051 qemu_put_be64(f
, expire_time
);
1054 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1056 uint64_t expire_time
;
1058 expire_time
= qemu_get_be64(f
);
1059 if (expire_time
!= -1) {
1060 qemu_mod_timer(ts
, expire_time
);
1066 static void timer_save(QEMUFile
*f
, void *opaque
)
1068 if (cpu_ticks_enabled
) {
1069 hw_error("cannot save state if virtual timers are running");
1071 qemu_put_be64(f
, cpu_ticks_offset
);
1072 qemu_put_be64(f
, ticks_per_sec
);
1073 qemu_put_be64(f
, cpu_clock_offset
);
1076 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1078 if (version_id
!= 1 && version_id
!= 2)
1080 if (cpu_ticks_enabled
) {
1083 cpu_ticks_offset
=qemu_get_be64(f
);
1084 ticks_per_sec
=qemu_get_be64(f
);
1085 if (version_id
== 2) {
1086 cpu_clock_offset
=qemu_get_be64(f
);
1091 static void qemu_event_increment(void);
1094 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1095 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1098 static void host_alarm_handler(int host_signum
)
1102 #define DISP_FREQ 1000
1104 static int64_t delta_min
= INT64_MAX
;
1105 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1107 ti
= qemu_get_clock(vm_clock
);
1108 if (last_clock
!= 0) {
1109 delta
= ti
- last_clock
;
1110 if (delta
< delta_min
)
1112 if (delta
> delta_max
)
1115 if (++count
== DISP_FREQ
) {
1116 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1117 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1118 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1119 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1120 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1122 delta_min
= INT64_MAX
;
1130 if (alarm_has_dynticks(alarm_timer
) ||
1132 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1133 qemu_get_clock(vm_clock
))) ||
1134 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1135 qemu_get_clock(rt_clock
))) {
1136 qemu_event_increment();
1137 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1139 #ifndef CONFIG_IOTHREAD
1141 /* stop the currently executing cpu because a timer occured */
1145 timer_alarm_pending
= 1;
1146 qemu_notify_event();
1150 static int64_t qemu_next_deadline(void)
1154 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1155 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1156 qemu_get_clock(vm_clock
);
1158 /* To avoid problems with overflow limit this to 2^32. */
1168 #if defined(__linux__) || defined(_WIN32)
1169 static uint64_t qemu_next_deadline_dyntick(void)
1177 delta
= (qemu_next_deadline() + 999) / 1000;
1179 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1180 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1181 qemu_get_clock(rt_clock
))*1000;
1182 if (rtdelta
< delta
)
1186 if (delta
< MIN_TIMER_REARM_US
)
1187 delta
= MIN_TIMER_REARM_US
;
1195 /* Sets a specific flag */
1196 static int fcntl_setfl(int fd
, int flag
)
1200 flags
= fcntl(fd
, F_GETFL
);
1204 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1210 #if defined(__linux__)
1212 #define RTC_FREQ 1024
1214 static void enable_sigio_timer(int fd
)
1216 struct sigaction act
;
1219 sigfillset(&act
.sa_mask
);
1221 act
.sa_handler
= host_alarm_handler
;
1223 sigaction(SIGIO
, &act
, NULL
);
1224 fcntl_setfl(fd
, O_ASYNC
);
1225 fcntl(fd
, F_SETOWN
, getpid());
1228 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1230 struct hpet_info info
;
1233 fd
= open("/dev/hpet", O_RDONLY
);
1238 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1240 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1241 "error, but for better emulation accuracy type:\n"
1242 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1246 /* Check capabilities */
1247 r
= ioctl(fd
, HPET_INFO
, &info
);
1251 /* Enable periodic mode */
1252 r
= ioctl(fd
, HPET_EPI
, 0);
1253 if (info
.hi_flags
&& (r
< 0))
1256 /* Enable interrupt */
1257 r
= ioctl(fd
, HPET_IE_ON
, 0);
1261 enable_sigio_timer(fd
);
1262 t
->priv
= (void *)(long)fd
;
1270 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1272 int fd
= (long)t
->priv
;
1277 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1280 unsigned long current_rtc_freq
= 0;
1282 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1285 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1286 if (current_rtc_freq
!= RTC_FREQ
&&
1287 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1288 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1289 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1290 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1293 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1299 enable_sigio_timer(rtc_fd
);
1301 t
->priv
= (void *)(long)rtc_fd
;
1306 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1308 int rtc_fd
= (long)t
->priv
;
1313 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1317 struct sigaction act
;
1319 sigfillset(&act
.sa_mask
);
1321 act
.sa_handler
= host_alarm_handler
;
1323 sigaction(SIGALRM
, &act
, NULL
);
1326 * Initialize ev struct to 0 to avoid valgrind complaining
1327 * about uninitialized data in timer_create call
1329 memset(&ev
, 0, sizeof(ev
));
1330 ev
.sigev_value
.sival_int
= 0;
1331 ev
.sigev_notify
= SIGEV_SIGNAL
;
1332 ev
.sigev_signo
= SIGALRM
;
1334 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1335 perror("timer_create");
1337 /* disable dynticks */
1338 fprintf(stderr
, "Dynamic Ticks disabled\n");
1343 t
->priv
= (void *)(long)host_timer
;
1348 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1350 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1352 timer_delete(host_timer
);
1355 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1357 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1358 struct itimerspec timeout
;
1359 int64_t nearest_delta_us
= INT64_MAX
;
1362 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1363 !active_timers
[QEMU_TIMER_VIRTUAL
])
1366 nearest_delta_us
= qemu_next_deadline_dyntick();
1368 /* check whether a timer is already running */
1369 if (timer_gettime(host_timer
, &timeout
)) {
1371 fprintf(stderr
, "Internal timer error: aborting\n");
1374 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1375 if (current_us
&& current_us
<= nearest_delta_us
)
1378 timeout
.it_interval
.tv_sec
= 0;
1379 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1380 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1381 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1382 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1384 fprintf(stderr
, "Internal timer error: aborting\n");
1389 #endif /* defined(__linux__) */
1391 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1393 struct sigaction act
;
1394 struct itimerval itv
;
1398 sigfillset(&act
.sa_mask
);
1400 act
.sa_handler
= host_alarm_handler
;
1402 sigaction(SIGALRM
, &act
, NULL
);
1404 itv
.it_interval
.tv_sec
= 0;
1405 /* for i386 kernel 2.6 to get 1 ms */
1406 itv
.it_interval
.tv_usec
= 999;
1407 itv
.it_value
.tv_sec
= 0;
1408 itv
.it_value
.tv_usec
= 10 * 1000;
1410 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1417 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1419 struct itimerval itv
;
1421 memset(&itv
, 0, sizeof(itv
));
1422 setitimer(ITIMER_REAL
, &itv
, NULL
);
1425 #endif /* !defined(_WIN32) */
1430 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1433 struct qemu_alarm_win32
*data
= t
->priv
;
1436 memset(&tc
, 0, sizeof(tc
));
1437 timeGetDevCaps(&tc
, sizeof(tc
));
1439 if (data
->period
< tc
.wPeriodMin
)
1440 data
->period
= tc
.wPeriodMin
;
1442 timeBeginPeriod(data
->period
);
1444 flags
= TIME_CALLBACK_FUNCTION
;
1445 if (alarm_has_dynticks(t
))
1446 flags
|= TIME_ONESHOT
;
1448 flags
|= TIME_PERIODIC
;
1450 data
->timerId
= timeSetEvent(1, // interval (ms)
1451 data
->period
, // resolution
1452 host_alarm_handler
, // function
1453 (DWORD
)t
, // parameter
1456 if (!data
->timerId
) {
1457 perror("Failed to initialize win32 alarm timer");
1458 timeEndPeriod(data
->period
);
1465 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1467 struct qemu_alarm_win32
*data
= t
->priv
;
1469 timeKillEvent(data
->timerId
);
1470 timeEndPeriod(data
->period
);
1473 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1475 struct qemu_alarm_win32
*data
= t
->priv
;
1476 uint64_t nearest_delta_us
;
1478 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1479 !active_timers
[QEMU_TIMER_VIRTUAL
])
1482 nearest_delta_us
= qemu_next_deadline_dyntick();
1483 nearest_delta_us
/= 1000;
1485 timeKillEvent(data
->timerId
);
1487 data
->timerId
= timeSetEvent(1,
1491 TIME_ONESHOT
| TIME_PERIODIC
);
1493 if (!data
->timerId
) {
1494 perror("Failed to re-arm win32 alarm timer");
1496 timeEndPeriod(data
->period
);
1503 static int init_timer_alarm(void)
1505 struct qemu_alarm_timer
*t
= NULL
;
1508 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1509 t
= &alarm_timers
[i
];
1529 static void quit_timers(void)
1531 alarm_timer
->stop(alarm_timer
);
1535 /***********************************************************/
1536 /* host time/date access */
1537 void qemu_get_timedate(struct tm
*tm
, int offset
)
1544 if (rtc_date_offset
== -1) {
1548 ret
= localtime(&ti
);
1550 ti
-= rtc_date_offset
;
1554 memcpy(tm
, ret
, sizeof(struct tm
));
1557 int qemu_timedate_diff(struct tm
*tm
)
1561 if (rtc_date_offset
== -1)
1563 seconds
= mktimegm(tm
);
1565 seconds
= mktime(tm
);
1567 seconds
= mktimegm(tm
) + rtc_date_offset
;
1569 return seconds
- time(NULL
);
1573 static void socket_cleanup(void)
1578 static int socket_init(void)
1583 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1585 err
= WSAGetLastError();
1586 fprintf(stderr
, "WSAStartup: %d\n", err
);
1589 atexit(socket_cleanup
);
1594 /***********************************************************/
1595 /* Bluetooth support */
1598 static struct HCIInfo
*hci_table
[MAX_NICS
];
1600 static struct bt_vlan_s
{
1601 struct bt_scatternet_s net
;
1603 struct bt_vlan_s
*next
;
1606 /* find or alloc a new bluetooth "VLAN" */
1607 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1609 struct bt_vlan_s
**pvlan
, *vlan
;
1610 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1614 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1616 pvlan
= &first_bt_vlan
;
1617 while (*pvlan
!= NULL
)
1618 pvlan
= &(*pvlan
)->next
;
1623 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1627 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1632 static struct HCIInfo null_hci
= {
1633 .cmd_send
= null_hci_send
,
1634 .sco_send
= null_hci_send
,
1635 .acl_send
= null_hci_send
,
1636 .bdaddr_set
= null_hci_addr_set
,
1639 struct HCIInfo
*qemu_next_hci(void)
1641 if (cur_hci
== nb_hcis
)
1644 return hci_table
[cur_hci
++];
1647 static struct HCIInfo
*hci_init(const char *str
)
1650 struct bt_scatternet_s
*vlan
= 0;
1652 if (!strcmp(str
, "null"))
1655 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1657 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1658 else if (!strncmp(str
, "hci", 3)) {
1661 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1662 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1667 vlan
= qemu_find_bt_vlan(0);
1669 return bt_new_hci(vlan
);
1672 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1677 static int bt_hci_parse(const char *str
)
1679 struct HCIInfo
*hci
;
1682 if (nb_hcis
>= MAX_NICS
) {
1683 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1687 hci
= hci_init(str
);
1696 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1697 hci
->bdaddr_set(hci
, bdaddr
.b
);
1699 hci_table
[nb_hcis
++] = hci
;
1704 static void bt_vhci_add(int vlan_id
)
1706 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1709 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1710 "an empty scatternet %i\n", vlan_id
);
1712 bt_vhci_init(bt_new_hci(vlan
));
1715 static struct bt_device_s
*bt_device_add(const char *opt
)
1717 struct bt_scatternet_s
*vlan
;
1719 char *endp
= strstr(opt
, ",vlan=");
1720 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1723 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1726 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1728 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1733 vlan
= qemu_find_bt_vlan(vlan_id
);
1736 fprintf(stderr
, "qemu: warning: adding a slave device to "
1737 "an empty scatternet %i\n", vlan_id
);
1739 if (!strcmp(devname
, "keyboard"))
1740 return bt_keyboard_init(vlan
);
1742 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1746 static int bt_parse(const char *opt
)
1748 const char *endp
, *p
;
1751 if (strstart(opt
, "hci", &endp
)) {
1752 if (!*endp
|| *endp
== ',') {
1754 if (!strstart(endp
, ",vlan=", 0))
1757 return bt_hci_parse(opt
);
1759 } else if (strstart(opt
, "vhci", &endp
)) {
1760 if (!*endp
|| *endp
== ',') {
1762 if (strstart(endp
, ",vlan=", &p
)) {
1763 vlan
= strtol(p
, (char **) &endp
, 0);
1765 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1769 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1778 } else if (strstart(opt
, "device:", &endp
))
1779 return !bt_device_add(endp
);
1781 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1785 /***********************************************************/
1786 /* QEMU Block devices */
1788 #define HD_ALIAS "index=%d,media=disk"
1789 #define CDROM_ALIAS "index=2,media=cdrom"
1790 #define FD_ALIAS "index=%d,if=floppy"
1791 #define PFLASH_ALIAS "if=pflash"
1792 #define MTD_ALIAS "if=mtd"
1793 #define SD_ALIAS "index=0,if=sd"
1795 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1802 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1805 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1807 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1808 __FUNCTION__
, optstr
);
1812 qemu_opt_set(opts
, "file", file
);
1816 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1820 /* seek interface, bus and unit */
1822 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1823 if (dinfo
->type
== type
&&
1824 dinfo
->bus
== bus
&&
1825 dinfo
->unit
== unit
)
1832 DriveInfo
*drive_get_by_id(const char *id
)
1836 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1837 if (strcmp(id
, dinfo
->id
))
1844 int drive_get_max_bus(BlockInterfaceType type
)
1850 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1851 if(dinfo
->type
== type
&&
1852 dinfo
->bus
> max_bus
)
1853 max_bus
= dinfo
->bus
;
1858 const char *drive_get_serial(BlockDriverState
*bdrv
)
1862 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1863 if (dinfo
->bdrv
== bdrv
)
1864 return dinfo
->serial
;
1870 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1874 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1875 if (dinfo
->bdrv
== bdrv
)
1876 return dinfo
->onerror
;
1879 return BLOCK_ERR_STOP_ENOSPC
;
1882 static void bdrv_format_print(void *opaque
, const char *name
)
1884 fprintf(stderr
, " %s", name
);
1887 void drive_uninit(BlockDriverState
*bdrv
)
1891 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1892 if (dinfo
->bdrv
!= bdrv
)
1894 qemu_opts_del(dinfo
->opts
);
1895 TAILQ_REMOVE(&drives
, dinfo
, next
);
1901 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1905 const char *file
= NULL
;
1908 const char *mediastr
= "";
1909 BlockInterfaceType type
;
1910 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1911 int bus_id
, unit_id
;
1912 int cyls
, heads
, secs
, translation
;
1913 BlockDriver
*drv
= NULL
;
1914 QEMUMachine
*machine
= opaque
;
1919 int bdrv_flags
, onerror
;
1920 const char *devaddr
;
1926 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1929 if (machine
->use_scsi
) {
1931 max_devs
= MAX_SCSI_DEVS
;
1932 pstrcpy(devname
, sizeof(devname
), "scsi");
1935 max_devs
= MAX_IDE_DEVS
;
1936 pstrcpy(devname
, sizeof(devname
), "ide");
1940 /* extract parameters */
1941 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1942 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1943 index
= qemu_opt_get_number(opts
, "index", -1);
1945 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1946 heads
= qemu_opt_get_number(opts
, "heads", 0);
1947 secs
= qemu_opt_get_number(opts
, "secs", 0);
1949 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1951 file
= qemu_opt_get(opts
, "file");
1952 serial
= qemu_opt_get(opts
, "serial");
1954 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1955 pstrcpy(devname
, sizeof(devname
), buf
);
1956 if (!strcmp(buf
, "ide")) {
1958 max_devs
= MAX_IDE_DEVS
;
1959 } else if (!strcmp(buf
, "scsi")) {
1961 max_devs
= MAX_SCSI_DEVS
;
1962 } else if (!strcmp(buf
, "floppy")) {
1965 } else if (!strcmp(buf
, "pflash")) {
1968 } else if (!strcmp(buf
, "mtd")) {
1971 } else if (!strcmp(buf
, "sd")) {
1974 } else if (!strcmp(buf
, "virtio")) {
1977 } else if (!strcmp(buf
, "xen")) {
1980 } else if (!strcmp(buf
, "none")) {
1984 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1989 if (cyls
|| heads
|| secs
) {
1990 if (cyls
< 1 || cyls
> 16383) {
1991 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1994 if (heads
< 1 || heads
> 16) {
1995 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1998 if (secs
< 1 || secs
> 63) {
1999 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2004 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2007 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2011 if (!strcmp(buf
, "none"))
2012 translation
= BIOS_ATA_TRANSLATION_NONE
;
2013 else if (!strcmp(buf
, "lba"))
2014 translation
= BIOS_ATA_TRANSLATION_LBA
;
2015 else if (!strcmp(buf
, "auto"))
2016 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2018 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2023 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2024 if (!strcmp(buf
, "disk")) {
2026 } else if (!strcmp(buf
, "cdrom")) {
2027 if (cyls
|| secs
|| heads
) {
2029 "qemu: '%s' invalid physical CHS format\n", buf
);
2032 media
= MEDIA_CDROM
;
2034 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2039 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2040 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2042 else if (!strcmp(buf
, "writethrough"))
2044 else if (!strcmp(buf
, "writeback"))
2047 fprintf(stderr
, "qemu: invalid cache option\n");
2052 #ifdef CONFIG_LINUX_AIO
2053 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2054 if (!strcmp(buf
, "threads"))
2056 else if (!strcmp(buf
, "native"))
2059 fprintf(stderr
, "qemu: invalid aio option\n");
2065 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2066 if (strcmp(buf
, "?") == 0) {
2067 fprintf(stderr
, "qemu: Supported formats:");
2068 bdrv_iterate_format(bdrv_format_print
, NULL
);
2069 fprintf(stderr
, "\n");
2072 drv
= bdrv_find_format(buf
);
2074 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2079 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2080 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2081 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2082 fprintf(stderr
, "werror is no supported by this format\n");
2085 if (!strcmp(buf
, "ignore"))
2086 onerror
= BLOCK_ERR_IGNORE
;
2087 else if (!strcmp(buf
, "enospc"))
2088 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2089 else if (!strcmp(buf
, "stop"))
2090 onerror
= BLOCK_ERR_STOP_ANY
;
2091 else if (!strcmp(buf
, "report"))
2092 onerror
= BLOCK_ERR_REPORT
;
2094 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2099 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2100 if (type
!= IF_VIRTIO
) {
2101 fprintf(stderr
, "addr is not supported\n");
2106 /* compute bus and unit according index */
2109 if (bus_id
!= 0 || unit_id
!= -1) {
2111 "qemu: index cannot be used with bus and unit\n");
2119 unit_id
= index
% max_devs
;
2120 bus_id
= index
/ max_devs
;
2124 /* if user doesn't specify a unit_id,
2125 * try to find the first free
2128 if (unit_id
== -1) {
2130 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2132 if (max_devs
&& unit_id
>= max_devs
) {
2133 unit_id
-= max_devs
;
2141 if (max_devs
&& unit_id
>= max_devs
) {
2142 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2143 unit_id
, max_devs
- 1);
2148 * ignore multiple definitions
2151 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2158 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2159 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2160 dinfo
->id
= qemu_strdup(buf
);
2162 /* no id supplied -> create one */
2163 dinfo
->id
= qemu_mallocz(32);
2164 if (type
== IF_IDE
|| type
== IF_SCSI
)
2165 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2167 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2168 devname
, bus_id
, mediastr
, unit_id
);
2170 snprintf(dinfo
->id
, 32, "%s%s%i",
2171 devname
, mediastr
, unit_id
);
2173 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2174 dinfo
->devaddr
= devaddr
;
2176 dinfo
->bus
= bus_id
;
2177 dinfo
->unit
= unit_id
;
2178 dinfo
->onerror
= onerror
;
2181 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2182 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2191 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2192 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2196 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2201 /* FIXME: This isn't really a floppy, but it's a reasonable
2204 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2211 /* add virtio block device */
2212 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2213 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2214 qemu_opt_set(opts
, "drive", dinfo
->id
);
2216 qemu_opt_set(opts
, "addr", devaddr
);
2227 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2228 cache
= 2; /* always use write-back with snapshot */
2230 if (cache
== 0) /* no caching */
2231 bdrv_flags
|= BDRV_O_NOCACHE
;
2232 else if (cache
== 2) /* write-back */
2233 bdrv_flags
|= BDRV_O_CACHE_WB
;
2236 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2238 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2241 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2242 fprintf(stderr
, "qemu: could not open disk image %s\n",
2247 if (bdrv_key_required(dinfo
->bdrv
))
2253 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2255 QEMUMachine
*machine
= opaque
;
2256 int fatal_error
= 0;
2258 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2265 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2267 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2268 qemu_opt_set(opts
, "snapshot", "on");
2273 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2275 boot_set_handler
= func
;
2276 boot_set_opaque
= opaque
;
2279 int qemu_boot_set(const char *boot_devices
)
2281 if (!boot_set_handler
) {
2284 return boot_set_handler(boot_set_opaque
, boot_devices
);
2287 static int parse_bootdevices(char *devices
)
2289 /* We just do some generic consistency checks */
2293 for (p
= devices
; *p
!= '\0'; p
++) {
2294 /* Allowed boot devices are:
2295 * a-b: floppy disk drives
2296 * c-f: IDE disk drives
2297 * g-m: machine implementation dependant drives
2298 * n-p: network devices
2299 * It's up to each machine implementation to check if the given boot
2300 * devices match the actual hardware implementation and firmware
2303 if (*p
< 'a' || *p
> 'p') {
2304 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2307 if (bitmap
& (1 << (*p
- 'a'))) {
2308 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2311 bitmap
|= 1 << (*p
- 'a');
2316 static void restore_boot_devices(void *opaque
)
2318 char *standard_boot_devices
= opaque
;
2320 qemu_boot_set(standard_boot_devices
);
2322 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2323 qemu_free(standard_boot_devices
);
2326 static void numa_add(const char *optarg
)
2330 unsigned long long value
, endvalue
;
2333 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2334 if (!strcmp(option
, "node")) {
2335 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2336 nodenr
= nb_numa_nodes
;
2338 nodenr
= strtoull(option
, NULL
, 10);
2341 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2342 node_mem
[nodenr
] = 0;
2344 value
= strtoull(option
, &endptr
, 0);
2346 case 0: case 'M': case 'm':
2353 node_mem
[nodenr
] = value
;
2355 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2356 node_cpumask
[nodenr
] = 0;
2358 value
= strtoull(option
, &endptr
, 10);
2361 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2363 if (*endptr
== '-') {
2364 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2365 if (endvalue
>= 63) {
2368 "only 63 CPUs in NUMA mode supported.\n");
2370 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2375 node_cpumask
[nodenr
] = value
;
2382 static void smp_parse(const char *optarg
)
2384 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2388 smp
= strtoul(optarg
, &endptr
, 10);
2389 if (endptr
!= optarg
) {
2390 if (*endptr
== ',') {
2394 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2395 sockets
= strtoull(option
, NULL
, 10);
2396 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2397 cores
= strtoull(option
, NULL
, 10);
2398 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2399 threads
= strtoull(option
, NULL
, 10);
2400 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2401 max_cpus
= strtoull(option
, NULL
, 10);
2403 /* compute missing values, prefer sockets over cores over threads */
2404 if (smp
== 0 || sockets
== 0) {
2405 sockets
= sockets
> 0 ? sockets
: 1;
2406 cores
= cores
> 0 ? cores
: 1;
2407 threads
= threads
> 0 ? threads
: 1;
2409 smp
= cores
* threads
* sockets
;
2411 sockets
= smp
/ (cores
* threads
);
2415 threads
= threads
> 0 ? threads
: 1;
2416 cores
= smp
/ (sockets
* threads
);
2419 sockets
= smp
/ (cores
* threads
);
2421 threads
= smp
/ (cores
* sockets
);
2426 smp_cores
= cores
> 0 ? cores
: 1;
2427 smp_threads
= threads
> 0 ? threads
: 1;
2429 max_cpus
= smp_cpus
;
2432 /***********************************************************/
2435 static USBPort
*used_usb_ports
;
2436 static USBPort
*free_usb_ports
;
2438 /* ??? Maybe change this to register a hub to keep track of the topology. */
2439 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2440 usb_attachfn attach
)
2442 port
->opaque
= opaque
;
2443 port
->index
= index
;
2444 port
->attach
= attach
;
2445 port
->next
= free_usb_ports
;
2446 free_usb_ports
= port
;
2449 int usb_device_add_dev(USBDevice
*dev
)
2453 /* Find a USB port to add the device to. */
2454 port
= free_usb_ports
;
2458 /* Create a new hub and chain it on. */
2459 free_usb_ports
= NULL
;
2460 port
->next
= used_usb_ports
;
2461 used_usb_ports
= port
;
2463 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2464 usb_attach(port
, hub
);
2465 port
= free_usb_ports
;
2468 free_usb_ports
= port
->next
;
2469 port
->next
= used_usb_ports
;
2470 used_usb_ports
= port
;
2471 usb_attach(port
, dev
);
2475 static void usb_msd_password_cb(void *opaque
, int err
)
2477 USBDevice
*dev
= opaque
;
2480 usb_device_add_dev(dev
);
2482 dev
->handle_destroy(dev
);
2485 static int usb_device_add(const char *devname
, int is_hotplug
)
2490 if (!free_usb_ports
)
2493 if (strstart(devname
, "host:", &p
)) {
2494 dev
= usb_host_device_open(p
);
2495 } else if (!strcmp(devname
, "mouse")) {
2496 dev
= usb_mouse_init();
2497 } else if (!strcmp(devname
, "tablet")) {
2498 dev
= usb_tablet_init();
2499 } else if (!strcmp(devname
, "keyboard")) {
2500 dev
= usb_keyboard_init();
2501 } else if (strstart(devname
, "disk:", &p
)) {
2502 BlockDriverState
*bs
;
2504 dev
= usb_msd_init(p
);
2507 bs
= usb_msd_get_bdrv(dev
);
2508 if (bdrv_key_required(bs
)) {
2511 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2516 } else if (!strcmp(devname
, "wacom-tablet")) {
2517 dev
= usb_wacom_init();
2518 } else if (strstart(devname
, "serial:", &p
)) {
2519 dev
= usb_serial_init(p
);
2520 #ifdef CONFIG_BRLAPI
2521 } else if (!strcmp(devname
, "braille")) {
2522 dev
= usb_baum_init();
2524 } else if (strstart(devname
, "net:", &p
)) {
2527 if (net_client_init(NULL
, "nic", p
) < 0)
2529 nd_table
[nic
].model
= "usb";
2530 dev
= usb_net_init(&nd_table
[nic
]);
2531 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2532 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2533 bt_new_hci(qemu_find_bt_vlan(0)));
2540 return usb_device_add_dev(dev
);
2543 int usb_device_del_addr(int bus_num
, int addr
)
2549 if (!used_usb_ports
)
2555 lastp
= &used_usb_ports
;
2556 port
= used_usb_ports
;
2557 while (port
&& port
->dev
->addr
!= addr
) {
2558 lastp
= &port
->next
;
2566 *lastp
= port
->next
;
2567 usb_attach(port
, NULL
);
2568 dev
->handle_destroy(dev
);
2569 port
->next
= free_usb_ports
;
2570 free_usb_ports
= port
;
2574 static int usb_device_del(const char *devname
)
2579 if (strstart(devname
, "host:", &p
))
2580 return usb_host_device_close(p
);
2582 if (!used_usb_ports
)
2585 p
= strchr(devname
, '.');
2588 bus_num
= strtoul(devname
, NULL
, 0);
2589 addr
= strtoul(p
+ 1, NULL
, 0);
2591 return usb_device_del_addr(bus_num
, addr
);
2594 static int usb_parse(const char *cmdline
)
2596 return usb_device_add(cmdline
, 0);
2599 void do_usb_add(Monitor
*mon
, const char *devname
)
2601 usb_device_add(devname
, 1);
2604 void do_usb_del(Monitor
*mon
, const char *devname
)
2606 usb_device_del(devname
);
2609 void usb_info(Monitor
*mon
)
2613 const char *speed_str
;
2616 monitor_printf(mon
, "USB support not enabled\n");
2620 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2624 switch(dev
->speed
) {
2628 case USB_SPEED_FULL
:
2631 case USB_SPEED_HIGH
:
2638 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2639 0, dev
->addr
, speed_str
, dev
->devname
);
2643 /***********************************************************/
2644 /* PCMCIA/Cardbus */
2646 static struct pcmcia_socket_entry_s
{
2647 PCMCIASocket
*socket
;
2648 struct pcmcia_socket_entry_s
*next
;
2649 } *pcmcia_sockets
= 0;
2651 void pcmcia_socket_register(PCMCIASocket
*socket
)
2653 struct pcmcia_socket_entry_s
*entry
;
2655 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2656 entry
->socket
= socket
;
2657 entry
->next
= pcmcia_sockets
;
2658 pcmcia_sockets
= entry
;
2661 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2663 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2665 ptr
= &pcmcia_sockets
;
2666 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2667 if (entry
->socket
== socket
) {
2673 void pcmcia_info(Monitor
*mon
)
2675 struct pcmcia_socket_entry_s
*iter
;
2677 if (!pcmcia_sockets
)
2678 monitor_printf(mon
, "No PCMCIA sockets\n");
2680 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2681 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2682 iter
->socket
->attached
? iter
->socket
->card_string
:
2686 /***********************************************************/
2687 /* register display */
2689 struct DisplayAllocator default_allocator
= {
2690 defaultallocator_create_displaysurface
,
2691 defaultallocator_resize_displaysurface
,
2692 defaultallocator_free_displaysurface
2695 void register_displaystate(DisplayState
*ds
)
2705 DisplayState
*get_displaystate(void)
2707 return display_state
;
2710 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2712 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2713 return ds
->allocator
;
2718 static void dumb_display_init(void)
2720 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2721 ds
->allocator
= &default_allocator
;
2722 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2723 register_displaystate(ds
);
2726 /***********************************************************/
2729 typedef struct IOHandlerRecord
{
2731 IOCanRWHandler
*fd_read_poll
;
2733 IOHandler
*fd_write
;
2736 /* temporary data */
2738 struct IOHandlerRecord
*next
;
2741 static IOHandlerRecord
*first_io_handler
;
2743 /* XXX: fd_read_poll should be suppressed, but an API change is
2744 necessary in the character devices to suppress fd_can_read(). */
2745 int qemu_set_fd_handler2(int fd
,
2746 IOCanRWHandler
*fd_read_poll
,
2748 IOHandler
*fd_write
,
2751 IOHandlerRecord
**pioh
, *ioh
;
2753 if (!fd_read
&& !fd_write
) {
2754 pioh
= &first_io_handler
;
2759 if (ioh
->fd
== fd
) {
2766 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2770 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2771 ioh
->next
= first_io_handler
;
2772 first_io_handler
= ioh
;
2775 ioh
->fd_read_poll
= fd_read_poll
;
2776 ioh
->fd_read
= fd_read
;
2777 ioh
->fd_write
= fd_write
;
2778 ioh
->opaque
= opaque
;
2784 int qemu_set_fd_handler(int fd
,
2786 IOHandler
*fd_write
,
2789 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2793 /***********************************************************/
2794 /* Polling handling */
2796 typedef struct PollingEntry
{
2799 struct PollingEntry
*next
;
2802 static PollingEntry
*first_polling_entry
;
2804 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2806 PollingEntry
**ppe
, *pe
;
2807 pe
= qemu_mallocz(sizeof(PollingEntry
));
2809 pe
->opaque
= opaque
;
2810 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2815 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2817 PollingEntry
**ppe
, *pe
;
2818 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2820 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2828 /***********************************************************/
2829 /* Wait objects support */
2830 typedef struct WaitObjects
{
2832 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2833 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2834 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2837 static WaitObjects wait_objects
= {0};
2839 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2841 WaitObjects
*w
= &wait_objects
;
2843 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2845 w
->events
[w
->num
] = handle
;
2846 w
->func
[w
->num
] = func
;
2847 w
->opaque
[w
->num
] = opaque
;
2852 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2855 WaitObjects
*w
= &wait_objects
;
2858 for (i
= 0; i
< w
->num
; i
++) {
2859 if (w
->events
[i
] == handle
)
2862 w
->events
[i
] = w
->events
[i
+ 1];
2863 w
->func
[i
] = w
->func
[i
+ 1];
2864 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2872 /***********************************************************/
2873 /* ram save/restore */
2875 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2879 v
= qemu_get_byte(f
);
2882 if (qemu_get_buffer(f
, buf
, len
) != len
)
2886 v
= qemu_get_byte(f
);
2887 memset(buf
, v
, len
);
2893 if (qemu_file_has_error(f
))
2899 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2904 if (qemu_get_be32(f
) != last_ram_offset
)
2906 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2907 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2914 #define BDRV_HASH_BLOCK_SIZE 1024
2915 #define IOBUF_SIZE 4096
2916 #define RAM_CBLOCK_MAGIC 0xfabe
2918 typedef struct RamDecompressState
{
2921 uint8_t buf
[IOBUF_SIZE
];
2922 } RamDecompressState
;
2924 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2927 memset(s
, 0, sizeof(*s
));
2929 ret
= inflateInit(&s
->zstream
);
2935 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2939 s
->zstream
.avail_out
= len
;
2940 s
->zstream
.next_out
= buf
;
2941 while (s
->zstream
.avail_out
> 0) {
2942 if (s
->zstream
.avail_in
== 0) {
2943 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2945 clen
= qemu_get_be16(s
->f
);
2946 if (clen
> IOBUF_SIZE
)
2948 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2949 s
->zstream
.avail_in
= clen
;
2950 s
->zstream
.next_in
= s
->buf
;
2952 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2953 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2960 static void ram_decompress_close(RamDecompressState
*s
)
2962 inflateEnd(&s
->zstream
);
2965 #define RAM_SAVE_FLAG_FULL 0x01
2966 #define RAM_SAVE_FLAG_COMPRESS 0x02
2967 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2968 #define RAM_SAVE_FLAG_PAGE 0x08
2969 #define RAM_SAVE_FLAG_EOS 0x10
2971 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2973 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2974 uint32_t *array
= (uint32_t *)page
;
2977 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2978 if (array
[i
] != val
)
2985 static int ram_save_block(QEMUFile
*f
)
2987 static ram_addr_t current_addr
= 0;
2988 ram_addr_t saved_addr
= current_addr
;
2989 ram_addr_t addr
= 0;
2992 while (addr
< last_ram_offset
) {
2993 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2996 cpu_physical_memory_reset_dirty(current_addr
,
2997 current_addr
+ TARGET_PAGE_SIZE
,
2998 MIGRATION_DIRTY_FLAG
);
3000 p
= qemu_get_ram_ptr(current_addr
);
3002 if (is_dup_page(p
, *p
)) {
3003 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3004 qemu_put_byte(f
, *p
);
3006 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3007 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3013 addr
+= TARGET_PAGE_SIZE
;
3014 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3020 static uint64_t bytes_transferred
= 0;
3022 static ram_addr_t
ram_save_remaining(void)
3025 ram_addr_t count
= 0;
3027 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3028 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3035 uint64_t ram_bytes_remaining(void)
3037 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3040 uint64_t ram_bytes_transferred(void)
3042 return bytes_transferred
;
3045 uint64_t ram_bytes_total(void)
3047 return last_ram_offset
;
3050 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3053 uint64_t bytes_transferred_last
;
3055 uint64_t expected_time
= 0;
3057 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3058 qemu_file_set_error(f
);
3063 /* Make sure all dirty bits are set */
3064 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3065 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3066 cpu_physical_memory_set_dirty(addr
);
3069 /* Enable dirty memory tracking */
3070 cpu_physical_memory_set_dirty_tracking(1);
3072 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3075 bytes_transferred_last
= bytes_transferred
;
3076 bwidth
= get_clock();
3078 while (!qemu_file_rate_limit(f
)) {
3081 ret
= ram_save_block(f
);
3082 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3083 if (ret
== 0) /* no more blocks */
3087 bwidth
= get_clock() - bwidth
;
3088 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3090 /* if we haven't transferred anything this round, force expected_time to a
3091 * a very high value, but without crashing */
3095 /* try transferring iterative blocks of memory */
3099 /* flush all remaining blocks regardless of rate limiting */
3100 while (ram_save_block(f
) != 0) {
3101 bytes_transferred
+= TARGET_PAGE_SIZE
;
3103 cpu_physical_memory_set_dirty_tracking(0);
3106 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3108 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3110 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3113 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3115 RamDecompressState s1
, *s
= &s1
;
3119 if (ram_decompress_open(s
, f
) < 0)
3121 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3122 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3123 fprintf(stderr
, "Error while reading ram block header\n");
3127 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3128 BDRV_HASH_BLOCK_SIZE
) < 0) {
3129 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3134 printf("Error block header\n");
3138 ram_decompress_close(s
);
3143 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3148 if (version_id
== 1)
3149 return ram_load_v1(f
, opaque
);
3151 if (version_id
== 2) {
3152 if (qemu_get_be32(f
) != last_ram_offset
)
3154 return ram_load_dead(f
, opaque
);
3157 if (version_id
!= 3)
3161 addr
= qemu_get_be64(f
);
3163 flags
= addr
& ~TARGET_PAGE_MASK
;
3164 addr
&= TARGET_PAGE_MASK
;
3166 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3167 if (addr
!= last_ram_offset
)
3171 if (flags
& RAM_SAVE_FLAG_FULL
) {
3172 if (ram_load_dead(f
, opaque
) < 0)
3176 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3177 uint8_t ch
= qemu_get_byte(f
);
3178 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3181 (!kvm_enabled() || kvm_has_sync_mmu())) {
3182 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3185 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3186 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3187 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3192 void qemu_service_io(void)
3194 qemu_notify_event();
3197 /***********************************************************/
3198 /* bottom halves (can be seen as timers which expire ASAP) */
3209 static QEMUBH
*first_bh
= NULL
;
3211 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3214 bh
= qemu_mallocz(sizeof(QEMUBH
));
3216 bh
->opaque
= opaque
;
3217 bh
->next
= first_bh
;
3222 int qemu_bh_poll(void)
3228 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3229 if (!bh
->deleted
&& bh
->scheduled
) {
3238 /* remove deleted bhs */
3252 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3260 void qemu_bh_schedule(QEMUBH
*bh
)
3266 /* stop the currently executing CPU to execute the BH ASAP */
3267 qemu_notify_event();
3270 void qemu_bh_cancel(QEMUBH
*bh
)
3275 void qemu_bh_delete(QEMUBH
*bh
)
3281 static void qemu_bh_update_timeout(int *timeout
)
3285 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3286 if (!bh
->deleted
&& bh
->scheduled
) {
3288 /* idle bottom halves will be polled at least
3290 *timeout
= MIN(10, *timeout
);
3292 /* non-idle bottom halves will be executed
3301 /***********************************************************/
3302 /* machine registration */
3304 static QEMUMachine
*first_machine
= NULL
;
3305 QEMUMachine
*current_machine
= NULL
;
3307 int qemu_register_machine(QEMUMachine
*m
)
3310 pm
= &first_machine
;
3318 static QEMUMachine
*find_machine(const char *name
)
3322 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3323 if (!strcmp(m
->name
, name
))
3325 if (m
->alias
&& !strcmp(m
->alias
, name
))
3331 static QEMUMachine
*find_default_machine(void)
3335 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3336 if (m
->is_default
) {
3343 /***********************************************************/
3344 /* main execution loop */
3346 static void gui_update(void *opaque
)
3348 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3349 DisplayState
*ds
= opaque
;
3350 DisplayChangeListener
*dcl
= ds
->listeners
;
3354 while (dcl
!= NULL
) {
3355 if (dcl
->gui_timer_interval
&&
3356 dcl
->gui_timer_interval
< interval
)
3357 interval
= dcl
->gui_timer_interval
;
3360 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3363 static void nographic_update(void *opaque
)
3365 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3367 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3370 struct vm_change_state_entry
{
3371 VMChangeStateHandler
*cb
;
3373 LIST_ENTRY (vm_change_state_entry
) entries
;
3376 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3378 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3381 VMChangeStateEntry
*e
;
3383 e
= qemu_mallocz(sizeof (*e
));
3387 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3391 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3393 LIST_REMOVE (e
, entries
);
3397 static void vm_state_notify(int running
, int reason
)
3399 VMChangeStateEntry
*e
;
3401 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3402 e
->cb(e
->opaque
, running
, reason
);
3406 static void resume_all_vcpus(void);
3407 static void pause_all_vcpus(void);
3414 vm_state_notify(1, 0);
3415 qemu_rearm_alarm_timer(alarm_timer
);
3420 /* reset/shutdown handler */
3422 typedef struct QEMUResetEntry
{
3423 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3424 QEMUResetHandler
*func
;
3428 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3429 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3430 static int reset_requested
;
3431 static int shutdown_requested
;
3432 static int powerdown_requested
;
3433 static int debug_requested
;
3434 static int vmstop_requested
;
3436 int qemu_shutdown_requested(void)
3438 int r
= shutdown_requested
;
3439 shutdown_requested
= 0;
3443 int qemu_reset_requested(void)
3445 int r
= reset_requested
;
3446 reset_requested
= 0;
3450 int qemu_powerdown_requested(void)
3452 int r
= powerdown_requested
;
3453 powerdown_requested
= 0;
3457 static int qemu_debug_requested(void)
3459 int r
= debug_requested
;
3460 debug_requested
= 0;
3464 static int qemu_vmstop_requested(void)
3466 int r
= vmstop_requested
;
3467 vmstop_requested
= 0;
3471 static void do_vm_stop(int reason
)
3474 cpu_disable_ticks();
3477 vm_state_notify(0, reason
);
3481 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3483 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3486 re
->opaque
= opaque
;
3487 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3490 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3494 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3495 if (re
->func
== func
&& re
->opaque
== opaque
) {
3496 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3503 void qemu_system_reset(void)
3505 QEMUResetEntry
*re
, *nre
;
3507 /* reset all devices */
3508 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3509 re
->func(re
->opaque
);
3513 void qemu_system_reset_request(void)
3516 shutdown_requested
= 1;
3518 reset_requested
= 1;
3520 qemu_notify_event();
3523 void qemu_system_shutdown_request(void)
3525 shutdown_requested
= 1;
3526 qemu_notify_event();
3529 void qemu_system_powerdown_request(void)
3531 powerdown_requested
= 1;
3532 qemu_notify_event();
3535 #ifdef CONFIG_IOTHREAD
3536 static void qemu_system_vmstop_request(int reason
)
3538 vmstop_requested
= reason
;
3539 qemu_notify_event();
3544 static int io_thread_fd
= -1;
3546 static void qemu_event_increment(void)
3548 static const char byte
= 0;
3550 if (io_thread_fd
== -1)
3553 write(io_thread_fd
, &byte
, sizeof(byte
));
3556 static void qemu_event_read(void *opaque
)
3558 int fd
= (unsigned long)opaque
;
3561 /* Drain the notify pipe */
3564 len
= read(fd
, buffer
, sizeof(buffer
));
3565 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3568 static int qemu_event_init(void)
3577 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3581 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3585 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3586 (void *)(unsigned long)fds
[0]);
3588 io_thread_fd
= fds
[1];
3597 HANDLE qemu_event_handle
;
3599 static void dummy_event_handler(void *opaque
)
3603 static int qemu_event_init(void)
3605 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3606 if (!qemu_event_handle
) {
3607 perror("Failed CreateEvent");
3610 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3614 static void qemu_event_increment(void)
3616 SetEvent(qemu_event_handle
);
3620 static int cpu_can_run(CPUState
*env
)
3629 #ifndef CONFIG_IOTHREAD
3630 static int qemu_init_main_loop(void)
3632 return qemu_event_init();
3635 void qemu_init_vcpu(void *_env
)
3637 CPUState
*env
= _env
;
3641 env
->nr_cores
= smp_cores
;
3642 env
->nr_threads
= smp_threads
;
3646 int qemu_cpu_self(void *env
)
3651 static void resume_all_vcpus(void)
3655 static void pause_all_vcpus(void)
3659 void qemu_cpu_kick(void *env
)
3664 void qemu_notify_event(void)
3666 CPUState
*env
= cpu_single_env
;
3673 #define qemu_mutex_lock_iothread() do { } while (0)
3674 #define qemu_mutex_unlock_iothread() do { } while (0)
3676 void vm_stop(int reason
)
3681 #else /* CONFIG_IOTHREAD */
3683 #include "qemu-thread.h"
3685 QemuMutex qemu_global_mutex
;
3686 static QemuMutex qemu_fair_mutex
;
3688 static QemuThread io_thread
;
3690 static QemuThread
*tcg_cpu_thread
;
3691 static QemuCond
*tcg_halt_cond
;
3693 static int qemu_system_ready
;
3695 static QemuCond qemu_cpu_cond
;
3697 static QemuCond qemu_system_cond
;
3698 static QemuCond qemu_pause_cond
;
3700 static void block_io_signals(void);
3701 static void unblock_io_signals(void);
3702 static int tcg_has_work(void);
3704 static int qemu_init_main_loop(void)
3708 ret
= qemu_event_init();
3712 qemu_cond_init(&qemu_pause_cond
);
3713 qemu_mutex_init(&qemu_fair_mutex
);
3714 qemu_mutex_init(&qemu_global_mutex
);
3715 qemu_mutex_lock(&qemu_global_mutex
);
3717 unblock_io_signals();
3718 qemu_thread_self(&io_thread
);
3723 static void qemu_wait_io_event(CPUState
*env
)
3725 while (!tcg_has_work())
3726 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3728 qemu_mutex_unlock(&qemu_global_mutex
);
3731 * Users of qemu_global_mutex can be starved, having no chance
3732 * to acquire it since this path will get to it first.
3733 * So use another lock to provide fairness.
3735 qemu_mutex_lock(&qemu_fair_mutex
);
3736 qemu_mutex_unlock(&qemu_fair_mutex
);
3738 qemu_mutex_lock(&qemu_global_mutex
);
3742 qemu_cond_signal(&qemu_pause_cond
);
3746 static int qemu_cpu_exec(CPUState
*env
);
3748 static void *kvm_cpu_thread_fn(void *arg
)
3750 CPUState
*env
= arg
;
3753 qemu_thread_self(env
->thread
);
3755 /* signal CPU creation */
3756 qemu_mutex_lock(&qemu_global_mutex
);
3758 qemu_cond_signal(&qemu_cpu_cond
);
3760 /* and wait for machine initialization */
3761 while (!qemu_system_ready
)
3762 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3765 if (cpu_can_run(env
))
3767 qemu_wait_io_event(env
);
3773 static void tcg_cpu_exec(void);
3775 static void *tcg_cpu_thread_fn(void *arg
)
3777 CPUState
*env
= arg
;
3780 qemu_thread_self(env
->thread
);
3782 /* signal CPU creation */
3783 qemu_mutex_lock(&qemu_global_mutex
);
3784 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3786 qemu_cond_signal(&qemu_cpu_cond
);
3788 /* and wait for machine initialization */
3789 while (!qemu_system_ready
)
3790 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3794 qemu_wait_io_event(cur_cpu
);
3800 void qemu_cpu_kick(void *_env
)
3802 CPUState
*env
= _env
;
3803 qemu_cond_broadcast(env
->halt_cond
);
3805 qemu_thread_signal(env
->thread
, SIGUSR1
);
3808 int qemu_cpu_self(void *env
)
3810 return (cpu_single_env
!= NULL
);
3813 static void cpu_signal(int sig
)
3816 cpu_exit(cpu_single_env
);
3819 static void block_io_signals(void)
3822 struct sigaction sigact
;
3825 sigaddset(&set
, SIGUSR2
);
3826 sigaddset(&set
, SIGIO
);
3827 sigaddset(&set
, SIGALRM
);
3828 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3831 sigaddset(&set
, SIGUSR1
);
3832 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3834 memset(&sigact
, 0, sizeof(sigact
));
3835 sigact
.sa_handler
= cpu_signal
;
3836 sigaction(SIGUSR1
, &sigact
, NULL
);
3839 static void unblock_io_signals(void)
3844 sigaddset(&set
, SIGUSR2
);
3845 sigaddset(&set
, SIGIO
);
3846 sigaddset(&set
, SIGALRM
);
3847 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3850 sigaddset(&set
, SIGUSR1
);
3851 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3854 static void qemu_signal_lock(unsigned int msecs
)
3856 qemu_mutex_lock(&qemu_fair_mutex
);
3858 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3859 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3860 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3863 qemu_mutex_unlock(&qemu_fair_mutex
);
3866 static void qemu_mutex_lock_iothread(void)
3868 if (kvm_enabled()) {
3869 qemu_mutex_lock(&qemu_fair_mutex
);
3870 qemu_mutex_lock(&qemu_global_mutex
);
3871 qemu_mutex_unlock(&qemu_fair_mutex
);
3873 qemu_signal_lock(100);
3876 static void qemu_mutex_unlock_iothread(void)
3878 qemu_mutex_unlock(&qemu_global_mutex
);
3881 static int all_vcpus_paused(void)
3883 CPUState
*penv
= first_cpu
;
3888 penv
= (CPUState
*)penv
->next_cpu
;
3894 static void pause_all_vcpus(void)
3896 CPUState
*penv
= first_cpu
;
3900 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3901 qemu_cpu_kick(penv
);
3902 penv
= (CPUState
*)penv
->next_cpu
;
3905 while (!all_vcpus_paused()) {
3906 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3909 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3910 penv
= (CPUState
*)penv
->next_cpu
;
3915 static void resume_all_vcpus(void)
3917 CPUState
*penv
= first_cpu
;
3922 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3923 qemu_cpu_kick(penv
);
3924 penv
= (CPUState
*)penv
->next_cpu
;
3928 static void tcg_init_vcpu(void *_env
)
3930 CPUState
*env
= _env
;
3931 /* share a single thread for all cpus with TCG */
3932 if (!tcg_cpu_thread
) {
3933 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3934 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3935 qemu_cond_init(env
->halt_cond
);
3936 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3937 while (env
->created
== 0)
3938 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3939 tcg_cpu_thread
= env
->thread
;
3940 tcg_halt_cond
= env
->halt_cond
;
3942 env
->thread
= tcg_cpu_thread
;
3943 env
->halt_cond
= tcg_halt_cond
;
3947 static void kvm_start_vcpu(CPUState
*env
)
3950 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3951 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3952 qemu_cond_init(env
->halt_cond
);
3953 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3954 while (env
->created
== 0)
3955 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3958 void qemu_init_vcpu(void *_env
)
3960 CPUState
*env
= _env
;
3963 kvm_start_vcpu(env
);
3966 env
->nr_cores
= smp_cores
;
3967 env
->nr_threads
= smp_threads
;
3970 void qemu_notify_event(void)
3972 qemu_event_increment();
3975 void vm_stop(int reason
)
3978 qemu_thread_self(&me
);
3980 if (!qemu_thread_equal(&me
, &io_thread
)) {
3981 qemu_system_vmstop_request(reason
);
3983 * FIXME: should not return to device code in case
3984 * vm_stop() has been requested.
3986 if (cpu_single_env
) {
3987 cpu_exit(cpu_single_env
);
3988 cpu_single_env
->stop
= 1;
3999 static void host_main_loop_wait(int *timeout
)
4005 /* XXX: need to suppress polling by better using win32 events */
4007 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4008 ret
|= pe
->func(pe
->opaque
);
4012 WaitObjects
*w
= &wait_objects
;
4014 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4015 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4016 if (w
->func
[ret
- WAIT_OBJECT_0
])
4017 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4019 /* Check for additional signaled events */
4020 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4022 /* Check if event is signaled */
4023 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4024 if(ret2
== WAIT_OBJECT_0
) {
4026 w
->func
[i
](w
->opaque
[i
]);
4027 } else if (ret2
== WAIT_TIMEOUT
) {
4029 err
= GetLastError();
4030 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4033 } else if (ret
== WAIT_TIMEOUT
) {
4035 err
= GetLastError();
4036 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4043 static void host_main_loop_wait(int *timeout
)
4048 void main_loop_wait(int timeout
)
4050 IOHandlerRecord
*ioh
;
4051 fd_set rfds
, wfds
, xfds
;
4055 qemu_bh_update_timeout(&timeout
);
4057 host_main_loop_wait(&timeout
);
4059 /* poll any events */
4060 /* XXX: separate device handlers from system ones */
4065 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4069 (!ioh
->fd_read_poll
||
4070 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4071 FD_SET(ioh
->fd
, &rfds
);
4075 if (ioh
->fd_write
) {
4076 FD_SET(ioh
->fd
, &wfds
);
4082 tv
.tv_sec
= timeout
/ 1000;
4083 tv
.tv_usec
= (timeout
% 1000) * 1000;
4085 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4087 qemu_mutex_unlock_iothread();
4088 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4089 qemu_mutex_lock_iothread();
4091 IOHandlerRecord
**pioh
;
4093 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4094 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4095 ioh
->fd_read(ioh
->opaque
);
4097 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4098 ioh
->fd_write(ioh
->opaque
);
4102 /* remove deleted IO handlers */
4103 pioh
= &first_io_handler
;
4114 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4116 /* rearm timer, if not periodic */
4117 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4118 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4119 qemu_rearm_alarm_timer(alarm_timer
);
4122 /* vm time timers */
4124 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4125 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4126 qemu_get_clock(vm_clock
));
4129 /* real time timers */
4130 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4131 qemu_get_clock(rt_clock
));
4133 /* Check bottom-halves last in case any of the earlier events triggered
4139 static int qemu_cpu_exec(CPUState
*env
)
4142 #ifdef CONFIG_PROFILER
4146 #ifdef CONFIG_PROFILER
4147 ti
= profile_getclock();
4152 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4153 env
->icount_decr
.u16
.low
= 0;
4154 env
->icount_extra
= 0;
4155 count
= qemu_next_deadline();
4156 count
= (count
+ (1 << icount_time_shift
) - 1)
4157 >> icount_time_shift
;
4158 qemu_icount
+= count
;
4159 decr
= (count
> 0xffff) ? 0xffff : count
;
4161 env
->icount_decr
.u16
.low
= decr
;
4162 env
->icount_extra
= count
;
4164 ret
= cpu_exec(env
);
4165 #ifdef CONFIG_PROFILER
4166 qemu_time
+= profile_getclock() - ti
;
4169 /* Fold pending instructions back into the
4170 instruction counter, and clear the interrupt flag. */
4171 qemu_icount
-= (env
->icount_decr
.u16
.low
4172 + env
->icount_extra
);
4173 env
->icount_decr
.u32
= 0;
4174 env
->icount_extra
= 0;
4179 static void tcg_cpu_exec(void)
4183 if (next_cpu
== NULL
)
4184 next_cpu
= first_cpu
;
4185 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4186 CPUState
*env
= cur_cpu
= next_cpu
;
4190 if (timer_alarm_pending
) {
4191 timer_alarm_pending
= 0;
4194 if (cpu_can_run(env
))
4195 ret
= qemu_cpu_exec(env
);
4196 if (ret
== EXCP_DEBUG
) {
4197 gdb_set_stop_cpu(env
);
4198 debug_requested
= 1;
4204 static int cpu_has_work(CPUState
*env
)
4212 if (qemu_cpu_has_work(env
))
4217 static int tcg_has_work(void)
4221 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4222 if (cpu_has_work(env
))
4227 static int qemu_calculate_timeout(void)
4229 #ifndef CONFIG_IOTHREAD
4234 else if (tcg_has_work())
4236 else if (!use_icount
)
4239 /* XXX: use timeout computed from timers */
4242 /* Advance virtual time to the next event. */
4243 if (use_icount
== 1) {
4244 /* When not using an adaptive execution frequency
4245 we tend to get badly out of sync with real time,
4246 so just delay for a reasonable amount of time. */
4249 delta
= cpu_get_icount() - cpu_get_clock();
4252 /* If virtual time is ahead of real time then just
4254 timeout
= (delta
/ 1000000) + 1;
4256 /* Wait for either IO to occur or the next
4258 add
= qemu_next_deadline();
4259 /* We advance the timer before checking for IO.
4260 Limit the amount we advance so that early IO
4261 activity won't get the guest too far ahead. */
4265 add
= (add
+ (1 << icount_time_shift
) - 1)
4266 >> icount_time_shift
;
4268 timeout
= delta
/ 1000000;
4275 #else /* CONFIG_IOTHREAD */
4280 static int vm_can_run(void)
4282 if (powerdown_requested
)
4284 if (reset_requested
)
4286 if (shutdown_requested
)
4288 if (debug_requested
)
4293 qemu_irq qemu_system_powerdown
;
4295 static void main_loop(void)
4299 #ifdef CONFIG_IOTHREAD
4300 qemu_system_ready
= 1;
4301 qemu_cond_broadcast(&qemu_system_cond
);
4306 #ifdef CONFIG_PROFILER
4309 #ifndef CONFIG_IOTHREAD
4312 #ifdef CONFIG_PROFILER
4313 ti
= profile_getclock();
4315 main_loop_wait(qemu_calculate_timeout());
4316 #ifdef CONFIG_PROFILER
4317 dev_time
+= profile_getclock() - ti
;
4319 } while (vm_can_run());
4321 if (qemu_debug_requested())
4322 vm_stop(EXCP_DEBUG
);
4323 if (qemu_shutdown_requested()) {
4330 if (qemu_reset_requested()) {
4332 qemu_system_reset();
4335 if (qemu_powerdown_requested()) {
4336 qemu_irq_raise(qemu_system_powerdown
);
4338 if ((r
= qemu_vmstop_requested()))
4344 static void version(void)
4346 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4349 static void help(int exitcode
)
4352 printf("usage: %s [options] [disk_image]\n"
4354 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4356 #define DEF(option, opt_arg, opt_enum, opt_help) \
4358 #define DEFHEADING(text) stringify(text) "\n"
4359 #include "qemu-options.h"
4364 "During emulation, the following keys are useful:\n"
4365 "ctrl-alt-f toggle full screen\n"
4366 "ctrl-alt-n switch to virtual console 'n'\n"
4367 "ctrl-alt toggle mouse and keyboard grab\n"
4369 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4374 DEFAULT_NETWORK_SCRIPT
,
4375 DEFAULT_NETWORK_DOWN_SCRIPT
,
4377 DEFAULT_GDBSTUB_PORT
,
4382 #define HAS_ARG 0x0001
4385 #define DEF(option, opt_arg, opt_enum, opt_help) \
4387 #define DEFHEADING(text)
4388 #include "qemu-options.h"
4394 typedef struct QEMUOption
{
4400 static const QEMUOption qemu_options
[] = {
4401 { "h", 0, QEMU_OPTION_h
},
4402 #define DEF(option, opt_arg, opt_enum, opt_help) \
4403 { option, opt_arg, opt_enum },
4404 #define DEFHEADING(text)
4405 #include "qemu-options.h"
4413 struct soundhw soundhw
[] = {
4414 #ifdef HAS_AUDIO_CHOICE
4415 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4421 { .init_isa
= pcspk_audio_init
}
4428 "Creative Sound Blaster 16",
4431 { .init_isa
= SB16_init
}
4435 #ifdef CONFIG_CS4231A
4441 { .init_isa
= cs4231a_init
}
4449 "Yamaha YMF262 (OPL3)",
4451 "Yamaha YM3812 (OPL2)",
4455 { .init_isa
= Adlib_init
}
4462 "Gravis Ultrasound GF1",
4465 { .init_isa
= GUS_init
}
4472 "Intel 82801AA AC97 Audio",
4475 { .init_pci
= ac97_init
}
4479 #ifdef CONFIG_ES1370
4482 "ENSONIQ AudioPCI ES1370",
4485 { .init_pci
= es1370_init
}
4489 #endif /* HAS_AUDIO_CHOICE */
4491 { NULL
, NULL
, 0, 0, { NULL
} }
4494 static void select_soundhw (const char *optarg
)
4498 if (*optarg
== '?') {
4501 printf ("Valid sound card names (comma separated):\n");
4502 for (c
= soundhw
; c
->name
; ++c
) {
4503 printf ("%-11s %s\n", c
->name
, c
->descr
);
4505 printf ("\n-soundhw all will enable all of the above\n");
4506 exit (*optarg
!= '?');
4514 if (!strcmp (optarg
, "all")) {
4515 for (c
= soundhw
; c
->name
; ++c
) {
4523 e
= strchr (p
, ',');
4524 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4526 for (c
= soundhw
; c
->name
; ++c
) {
4527 if (!strncmp (c
->name
, p
, l
)) {
4536 "Unknown sound card name (too big to show)\n");
4539 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4544 p
+= l
+ (e
!= NULL
);
4548 goto show_valid_cards
;
4553 static void select_vgahw (const char *p
)
4557 vga_interface_type
= VGA_NONE
;
4558 if (strstart(p
, "std", &opts
)) {
4559 vga_interface_type
= VGA_STD
;
4560 } else if (strstart(p
, "cirrus", &opts
)) {
4561 vga_interface_type
= VGA_CIRRUS
;
4562 } else if (strstart(p
, "vmware", &opts
)) {
4563 vga_interface_type
= VGA_VMWARE
;
4564 } else if (strstart(p
, "xenfb", &opts
)) {
4565 vga_interface_type
= VGA_XENFB
;
4566 } else if (!strstart(p
, "none", &opts
)) {
4568 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4572 const char *nextopt
;
4574 if (strstart(opts
, ",retrace=", &nextopt
)) {
4576 if (strstart(opts
, "dumb", &nextopt
))
4577 vga_retrace_method
= VGA_RETRACE_DUMB
;
4578 else if (strstart(opts
, "precise", &nextopt
))
4579 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4580 else goto invalid_vga
;
4581 } else goto invalid_vga
;
4587 static int balloon_parse(const char *arg
)
4592 if (!strcmp(arg
, "none")) {
4594 } else if (!strncmp(arg
, "virtio", 6)) {
4596 if (arg
[6] == ',') {
4598 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4599 virtio_balloon_devaddr
= strdup(buf
);
4610 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4612 exit(STATUS_CONTROL_C_EXIT
);
4617 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4621 if(strlen(str
) != 36)
4624 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4625 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4626 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4632 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4638 #define MAX_NET_CLIENTS 32
4642 static void termsig_handler(int signal
)
4644 qemu_system_shutdown_request();
4647 static void sigchld_handler(int signal
)
4649 waitpid(-1, NULL
, WNOHANG
);
4652 static void sighandler_setup(void)
4654 struct sigaction act
;
4656 memset(&act
, 0, sizeof(act
));
4657 act
.sa_handler
= termsig_handler
;
4658 sigaction(SIGINT
, &act
, NULL
);
4659 sigaction(SIGHUP
, &act
, NULL
);
4660 sigaction(SIGTERM
, &act
, NULL
);
4662 act
.sa_handler
= sigchld_handler
;
4663 act
.sa_flags
= SA_NOCLDSTOP
;
4664 sigaction(SIGCHLD
, &act
, NULL
);
4670 /* Look for support files in the same directory as the executable. */
4671 static char *find_datadir(const char *argv0
)
4677 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4684 while (p
!= buf
&& *p
!= '\\')
4687 if (access(buf
, R_OK
) == 0) {
4688 return qemu_strdup(buf
);
4694 /* Find a likely location for support files using the location of the binary.
4695 For installed binaries this will be "$bindir/../share/qemu". When
4696 running from the build tree this will be "$bindir/../pc-bios". */
4697 #define SHARE_SUFFIX "/share/qemu"
4698 #define BUILD_SUFFIX "/pc-bios"
4699 static char *find_datadir(const char *argv0
)
4709 #if defined(__linux__)
4712 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4718 #elif defined(__FreeBSD__)
4721 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4728 /* If we don't have any way of figuring out the actual executable
4729 location then try argv[0]. */
4734 p
= realpath(argv0
, p
);
4742 max_len
= strlen(dir
) +
4743 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4744 res
= qemu_mallocz(max_len
);
4745 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4746 if (access(res
, R_OK
)) {
4747 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4748 if (access(res
, R_OK
)) {
4762 char *qemu_find_file(int type
, const char *name
)
4768 /* If name contains path separators then try it as a straight path. */
4769 if ((strchr(name
, '/') || strchr(name
, '\\'))
4770 && access(name
, R_OK
) == 0) {
4771 return strdup(name
);
4774 case QEMU_FILE_TYPE_BIOS
:
4777 case QEMU_FILE_TYPE_KEYMAP
:
4778 subdir
= "keymaps/";
4783 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4784 buf
= qemu_mallocz(len
);
4785 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4786 if (access(buf
, R_OK
)) {
4793 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4797 dev
= qdev_device_add(opts
);
4803 struct device_config
{
4805 DEV_USB
, /* -usbdevice */
4808 const char *cmdline
;
4809 TAILQ_ENTRY(device_config
) next
;
4811 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4813 static void add_device_config(int type
, const char *cmdline
)
4815 struct device_config
*conf
;
4817 conf
= qemu_mallocz(sizeof(*conf
));
4819 conf
->cmdline
= cmdline
;
4820 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4823 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4825 struct device_config
*conf
;
4828 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4829 if (conf
->type
!= type
)
4831 rc
= func(conf
->cmdline
);
4838 int main(int argc
, char **argv
, char **envp
)
4840 const char *gdbstub_dev
= NULL
;
4841 uint32_t boot_devices_bitmap
= 0;
4843 int snapshot
, linux_boot
, net_boot
;
4844 const char *initrd_filename
;
4845 const char *kernel_filename
, *kernel_cmdline
;
4846 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4848 DisplayChangeListener
*dcl
;
4849 int cyls
, heads
, secs
, translation
;
4850 const char *net_clients
[MAX_NET_CLIENTS
];
4852 QemuOpts
*hda_opts
= NULL
, *opts
;
4854 const char *r
, *optarg
;
4855 CharDriverState
*monitor_hd
= NULL
;
4856 const char *monitor_device
;
4857 const char *serial_devices
[MAX_SERIAL_PORTS
];
4858 int serial_device_index
;
4859 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4860 int parallel_device_index
;
4861 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4862 int virtio_console_index
;
4863 const char *loadvm
= NULL
;
4864 QEMUMachine
*machine
;
4865 const char *cpu_model
;
4870 const char *pid_file
= NULL
;
4871 const char *incoming
= NULL
;
4874 struct passwd
*pwd
= NULL
;
4875 const char *chroot_dir
= NULL
;
4876 const char *run_as
= NULL
;
4879 int show_vnc_port
= 0;
4881 qemu_errors_to_file(stderr
);
4882 qemu_cache_utils_init(envp
);
4884 LIST_INIT (&vm_change_state_head
);
4887 struct sigaction act
;
4888 sigfillset(&act
.sa_mask
);
4890 act
.sa_handler
= SIG_IGN
;
4891 sigaction(SIGPIPE
, &act
, NULL
);
4894 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4895 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4896 QEMU to run on a single CPU */
4901 h
= GetCurrentProcess();
4902 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4903 for(i
= 0; i
< 32; i
++) {
4904 if (mask
& (1 << i
))
4909 SetProcessAffinityMask(h
, mask
);
4915 module_call_init(MODULE_INIT_MACHINE
);
4916 machine
= find_default_machine();
4918 initrd_filename
= NULL
;
4921 kernel_filename
= NULL
;
4922 kernel_cmdline
= "";
4923 cyls
= heads
= secs
= 0;
4924 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4925 monitor_device
= "vc:80Cx24C";
4927 serial_devices
[0] = "vc:80Cx24C";
4928 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4929 serial_devices
[i
] = NULL
;
4930 serial_device_index
= 0;
4932 parallel_devices
[0] = "vc:80Cx24C";
4933 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4934 parallel_devices
[i
] = NULL
;
4935 parallel_device_index
= 0;
4937 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4938 virtio_consoles
[i
] = NULL
;
4939 virtio_console_index
= 0;
4941 for (i
= 0; i
< MAX_NODES
; i
++) {
4943 node_cpumask
[i
] = 0;
4959 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4961 const QEMUOption
*popt
;
4964 /* Treat --foo the same as -foo. */
4967 popt
= qemu_options
;
4970 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4974 if (!strcmp(popt
->name
, r
+ 1))
4978 if (popt
->flags
& HAS_ARG
) {
4979 if (optind
>= argc
) {
4980 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4984 optarg
= argv
[optind
++];
4989 switch(popt
->index
) {
4991 machine
= find_machine(optarg
);
4994 printf("Supported machines are:\n");
4995 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4997 printf("%-10s %s (alias of %s)\n",
4998 m
->alias
, m
->desc
, m
->name
);
4999 printf("%-10s %s%s\n",
5001 m
->is_default
? " (default)" : "");
5003 exit(*optarg
!= '?');
5006 case QEMU_OPTION_cpu
:
5007 /* hw initialization will check this */
5008 if (*optarg
== '?') {
5009 /* XXX: implement xxx_cpu_list for targets that still miss it */
5010 #if defined(cpu_list)
5011 cpu_list(stdout
, &fprintf
);
5018 case QEMU_OPTION_initrd
:
5019 initrd_filename
= optarg
;
5021 case QEMU_OPTION_hda
:
5023 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5025 hda_opts
= drive_add(optarg
, HD_ALIAS
5026 ",cyls=%d,heads=%d,secs=%d%s",
5027 0, cyls
, heads
, secs
,
5028 translation
== BIOS_ATA_TRANSLATION_LBA
?
5030 translation
== BIOS_ATA_TRANSLATION_NONE
?
5031 ",trans=none" : "");
5033 case QEMU_OPTION_hdb
:
5034 case QEMU_OPTION_hdc
:
5035 case QEMU_OPTION_hdd
:
5036 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5038 case QEMU_OPTION_drive
:
5039 drive_add(NULL
, "%s", optarg
);
5041 case QEMU_OPTION_set
:
5042 if (qemu_set_option(optarg
) != 0)
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_opts
!= NULL
) {
5093 snprintf(num
, sizeof(num
), "%d", cyls
);
5094 qemu_opt_set(hda_opts
, "cyls", num
);
5095 snprintf(num
, sizeof(num
), "%d", heads
);
5096 qemu_opt_set(hda_opts
, "heads", num
);
5097 snprintf(num
, sizeof(num
), "%d", secs
);
5098 qemu_opt_set(hda_opts
, "secs", num
);
5099 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5100 qemu_opt_set(hda_opts
, "trans", "lba");
5101 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5102 qemu_opt_set(hda_opts
, "trans", "none");
5106 case QEMU_OPTION_numa
:
5107 if (nb_numa_nodes
>= MAX_NODES
) {
5108 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5113 case QEMU_OPTION_nographic
:
5114 display_type
= DT_NOGRAPHIC
;
5116 #ifdef CONFIG_CURSES
5117 case QEMU_OPTION_curses
:
5118 display_type
= DT_CURSES
;
5121 case QEMU_OPTION_portrait
:
5124 case QEMU_OPTION_kernel
:
5125 kernel_filename
= optarg
;
5127 case QEMU_OPTION_append
:
5128 kernel_cmdline
= optarg
;
5130 case QEMU_OPTION_cdrom
:
5131 drive_add(optarg
, CDROM_ALIAS
);
5133 case QEMU_OPTION_boot
:
5135 static const char * const params
[] = {
5136 "order", "once", "menu", NULL
5138 char buf
[sizeof(boot_devices
)];
5139 char *standard_boot_devices
;
5142 if (!strchr(optarg
, '=')) {
5144 pstrcpy(buf
, sizeof(buf
), optarg
);
5145 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5147 "qemu: unknown boot parameter '%s' in '%s'\n",
5153 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5154 boot_devices_bitmap
= parse_bootdevices(buf
);
5155 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5158 if (get_param_value(buf
, sizeof(buf
),
5160 boot_devices_bitmap
|= parse_bootdevices(buf
);
5161 standard_boot_devices
= qemu_strdup(boot_devices
);
5162 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5163 qemu_register_reset(restore_boot_devices
,
5164 standard_boot_devices
);
5166 if (get_param_value(buf
, sizeof(buf
),
5168 if (!strcmp(buf
, "on")) {
5170 } else if (!strcmp(buf
, "off")) {
5174 "qemu: invalid option value '%s'\n",
5182 case QEMU_OPTION_fda
:
5183 case QEMU_OPTION_fdb
:
5184 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5187 case QEMU_OPTION_no_fd_bootchk
:
5191 case QEMU_OPTION_net
:
5192 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5193 fprintf(stderr
, "qemu: too many network clients\n");
5196 net_clients
[nb_net_clients
] = optarg
;
5200 case QEMU_OPTION_tftp
:
5201 legacy_tftp_prefix
= optarg
;
5203 case QEMU_OPTION_bootp
:
5204 legacy_bootp_filename
= optarg
;
5207 case QEMU_OPTION_smb
:
5208 net_slirp_smb(optarg
);
5211 case QEMU_OPTION_redir
:
5212 net_slirp_redir(optarg
);
5215 case QEMU_OPTION_bt
:
5216 add_device_config(DEV_BT
, 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) && HOST_LONG_BITS
== 32) {
5253 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5256 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5257 fprintf(stderr
, "qemu: ram size too large\n");
5266 const CPULogItem
*item
;
5268 mask
= cpu_str_to_log_mask(optarg
);
5270 printf("Log items (comma separated):\n");
5271 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5272 printf("%-10s %s\n", item
->name
, item
->help
);
5280 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5282 case QEMU_OPTION_gdb
:
5283 gdbstub_dev
= optarg
;
5288 case QEMU_OPTION_bios
:
5291 case QEMU_OPTION_singlestep
:
5299 keyboard_layout
= optarg
;
5302 case QEMU_OPTION_localtime
:
5305 case QEMU_OPTION_vga
:
5306 select_vgahw (optarg
);
5308 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5314 w
= strtol(p
, (char **)&p
, 10);
5317 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5323 h
= strtol(p
, (char **)&p
, 10);
5328 depth
= strtol(p
, (char **)&p
, 10);
5329 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5330 depth
!= 24 && depth
!= 32)
5332 } else if (*p
== '\0') {
5333 depth
= graphic_depth
;
5340 graphic_depth
= depth
;
5344 case QEMU_OPTION_echr
:
5347 term_escape_char
= strtol(optarg
, &r
, 0);
5349 printf("Bad argument to echr\n");
5352 case QEMU_OPTION_monitor
:
5353 monitor_device
= optarg
;
5355 case QEMU_OPTION_serial
:
5356 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5357 fprintf(stderr
, "qemu: too many serial ports\n");
5360 serial_devices
[serial_device_index
] = optarg
;
5361 serial_device_index
++;
5363 case QEMU_OPTION_watchdog
:
5366 "qemu: only one watchdog option may be given\n");
5371 case QEMU_OPTION_watchdog_action
:
5372 if (select_watchdog_action(optarg
) == -1) {
5373 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5377 case QEMU_OPTION_virtiocon
:
5378 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5379 fprintf(stderr
, "qemu: too many virtio consoles\n");
5382 virtio_consoles
[virtio_console_index
] = optarg
;
5383 virtio_console_index
++;
5385 case QEMU_OPTION_parallel
:
5386 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5387 fprintf(stderr
, "qemu: too many parallel ports\n");
5390 parallel_devices
[parallel_device_index
] = optarg
;
5391 parallel_device_index
++;
5393 case QEMU_OPTION_loadvm
:
5396 case QEMU_OPTION_full_screen
:
5400 case QEMU_OPTION_no_frame
:
5403 case QEMU_OPTION_alt_grab
:
5406 case QEMU_OPTION_no_quit
:
5409 case QEMU_OPTION_sdl
:
5410 display_type
= DT_SDL
;
5413 case QEMU_OPTION_pidfile
:
5417 case QEMU_OPTION_win2k_hack
:
5418 win2k_install_hack
= 1;
5420 case QEMU_OPTION_rtc_td_hack
:
5423 case QEMU_OPTION_acpitable
:
5424 if(acpi_table_add(optarg
) < 0) {
5425 fprintf(stderr
, "Wrong acpi table provided\n");
5429 case QEMU_OPTION_smbios
:
5430 if(smbios_entry_add(optarg
) < 0) {
5431 fprintf(stderr
, "Wrong smbios provided\n");
5437 case QEMU_OPTION_enable_kvm
:
5441 case QEMU_OPTION_usb
:
5444 case QEMU_OPTION_usbdevice
:
5446 add_device_config(DEV_USB
, optarg
);
5448 case QEMU_OPTION_device
:
5449 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5451 fprintf(stderr
, "parse error: %s\n", optarg
);
5455 case QEMU_OPTION_smp
:
5458 fprintf(stderr
, "Invalid number of CPUs\n");
5461 if (max_cpus
< smp_cpus
) {
5462 fprintf(stderr
, "maxcpus must be equal to or greater than "
5466 if (max_cpus
> 255) {
5467 fprintf(stderr
, "Unsupported number of maxcpus\n");
5471 case QEMU_OPTION_vnc
:
5472 display_type
= DT_VNC
;
5473 vnc_display
= optarg
;
5476 case QEMU_OPTION_no_acpi
:
5479 case QEMU_OPTION_no_hpet
:
5482 case QEMU_OPTION_balloon
:
5483 if (balloon_parse(optarg
) < 0) {
5484 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5489 case QEMU_OPTION_no_reboot
:
5492 case QEMU_OPTION_no_shutdown
:
5495 case QEMU_OPTION_show_cursor
:
5498 case QEMU_OPTION_uuid
:
5499 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5500 fprintf(stderr
, "Fail to parse UUID string."
5501 " Wrong format.\n");
5506 case QEMU_OPTION_daemonize
:
5510 case QEMU_OPTION_option_rom
:
5511 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5512 fprintf(stderr
, "Too many option ROMs\n");
5515 option_rom
[nb_option_roms
] = optarg
;
5518 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5519 case QEMU_OPTION_semihosting
:
5520 semihosting_enabled
= 1;
5523 case QEMU_OPTION_name
:
5524 qemu_name
= qemu_strdup(optarg
);
5526 char *p
= strchr(qemu_name
, ',');
5529 if (strncmp(p
, "process=", 8)) {
5530 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5538 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5539 case QEMU_OPTION_prom_env
:
5540 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5541 fprintf(stderr
, "Too many prom variables\n");
5544 prom_envs
[nb_prom_envs
] = optarg
;
5549 case QEMU_OPTION_old_param
:
5553 case QEMU_OPTION_clock
:
5554 configure_alarms(optarg
);
5556 case QEMU_OPTION_startdate
:
5559 time_t rtc_start_date
;
5560 if (!strcmp(optarg
, "now")) {
5561 rtc_date_offset
= -1;
5563 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5571 } else if (sscanf(optarg
, "%d-%d-%d",
5574 &tm
.tm_mday
) == 3) {
5583 rtc_start_date
= mktimegm(&tm
);
5584 if (rtc_start_date
== -1) {
5586 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5587 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5590 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5594 case QEMU_OPTION_tb_size
:
5595 tb_size
= strtol(optarg
, NULL
, 0);
5599 case QEMU_OPTION_icount
:
5601 if (strcmp(optarg
, "auto") == 0) {
5602 icount_time_shift
= -1;
5604 icount_time_shift
= strtol(optarg
, NULL
, 0);
5607 case QEMU_OPTION_incoming
:
5611 case QEMU_OPTION_chroot
:
5612 chroot_dir
= optarg
;
5614 case QEMU_OPTION_runas
:
5619 case QEMU_OPTION_xen_domid
:
5620 xen_domid
= atoi(optarg
);
5622 case QEMU_OPTION_xen_create
:
5623 xen_mode
= XEN_CREATE
;
5625 case QEMU_OPTION_xen_attach
:
5626 xen_mode
= XEN_ATTACH
;
5633 /* If no data_dir is specified then try to find it relative to the
5636 data_dir
= find_datadir(argv
[0]);
5638 /* If all else fails use the install patch specified when building. */
5640 data_dir
= CONFIG_QEMU_SHAREDIR
;
5644 * Default to max_cpus = smp_cpus, in case the user doesn't
5645 * specify a max_cpus value.
5648 max_cpus
= smp_cpus
;
5650 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5651 if (smp_cpus
> machine
->max_cpus
) {
5652 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5653 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5658 if (display_type
== DT_NOGRAPHIC
) {
5659 if (serial_device_index
== 0)
5660 serial_devices
[0] = "stdio";
5661 if (parallel_device_index
== 0)
5662 parallel_devices
[0] = "null";
5663 if (strncmp(monitor_device
, "vc", 2) == 0)
5664 monitor_device
= "stdio";
5671 if (pipe(fds
) == -1)
5682 len
= read(fds
[0], &status
, 1);
5683 if (len
== -1 && (errno
== EINTR
))
5688 else if (status
== 1) {
5689 fprintf(stderr
, "Could not acquire pidfile\n");
5706 signal(SIGTSTP
, SIG_IGN
);
5707 signal(SIGTTOU
, SIG_IGN
);
5708 signal(SIGTTIN
, SIG_IGN
);
5711 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5714 write(fds
[1], &status
, 1);
5716 fprintf(stderr
, "Could not acquire pid file\n");
5721 if (qemu_init_main_loop()) {
5722 fprintf(stderr
, "qemu_init_main_loop failed\n");
5725 linux_boot
= (kernel_filename
!= NULL
);
5727 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5728 fprintf(stderr
, "-append only allowed with -kernel option\n");
5732 if (!linux_boot
&& initrd_filename
!= NULL
) {
5733 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5738 /* Win32 doesn't support line-buffering and requires size >= 2 */
5739 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5743 if (init_timer_alarm() < 0) {
5744 fprintf(stderr
, "could not initialize alarm timer\n");
5747 if (use_icount
&& icount_time_shift
< 0) {
5749 /* 125MIPS seems a reasonable initial guess at the guest speed.
5750 It will be corrected fairly quickly anyway. */
5751 icount_time_shift
= 3;
5752 init_icount_adjust();
5759 /* init network clients */
5760 if (nb_net_clients
== 0) {
5761 /* if no clients, we use a default config */
5762 net_clients
[nb_net_clients
++] = "nic";
5764 net_clients
[nb_net_clients
++] = "user";
5768 for(i
= 0;i
< nb_net_clients
; i
++) {
5769 if (net_client_parse(net_clients
[i
]) < 0)
5773 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5774 net_set_boot_mask(net_boot
);
5778 /* init the bluetooth world */
5779 if (foreach_device_config(DEV_BT
, bt_parse
))
5782 /* init the memory */
5784 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5786 /* init the dynamic translator */
5787 cpu_exec_init_all(tb_size
* 1024 * 1024);
5791 /* we always create the cdrom drive, even if no disk is there */
5792 drive_add(NULL
, CDROM_ALIAS
);
5794 /* we always create at least one floppy */
5795 drive_add(NULL
, FD_ALIAS
, 0);
5797 /* we always create one sd slot, even if no card is in it */
5798 drive_add(NULL
, SD_ALIAS
);
5800 /* open the virtual block devices */
5802 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5803 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5806 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5807 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5810 /* must be after terminal init, SDL library changes signal handlers */
5814 /* Maintain compatibility with multiple stdio monitors */
5815 if (!strcmp(monitor_device
,"stdio")) {
5816 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5817 const char *devname
= serial_devices
[i
];
5818 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5819 monitor_device
= NULL
;
5821 } else if (devname
&& !strcmp(devname
,"stdio")) {
5822 monitor_device
= NULL
;
5823 serial_devices
[i
] = "mon:stdio";
5829 if (nb_numa_nodes
> 0) {
5832 if (nb_numa_nodes
> smp_cpus
) {
5833 nb_numa_nodes
= smp_cpus
;
5836 /* If no memory size if given for any node, assume the default case
5837 * and distribute the available memory equally across all nodes
5839 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5840 if (node_mem
[i
] != 0)
5843 if (i
== nb_numa_nodes
) {
5844 uint64_t usedmem
= 0;
5846 /* On Linux, the each node's border has to be 8MB aligned,
5847 * the final node gets the rest.
5849 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5850 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5851 usedmem
+= node_mem
[i
];
5853 node_mem
[i
] = ram_size
- usedmem
;
5856 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5857 if (node_cpumask
[i
] != 0)
5860 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5861 * must cope with this anyway, because there are BIOSes out there in
5862 * real machines which also use this scheme.
5864 if (i
== nb_numa_nodes
) {
5865 for (i
= 0; i
< smp_cpus
; i
++) {
5866 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5871 if (kvm_enabled()) {
5874 ret
= kvm_init(smp_cpus
);
5876 fprintf(stderr
, "failed to initialize KVM\n");
5881 if (monitor_device
) {
5882 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5884 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5889 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5890 const char *devname
= serial_devices
[i
];
5891 if (devname
&& strcmp(devname
, "none")) {
5893 snprintf(label
, sizeof(label
), "serial%d", i
);
5894 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5895 if (!serial_hds
[i
]) {
5896 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5903 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5904 const char *devname
= parallel_devices
[i
];
5905 if (devname
&& strcmp(devname
, "none")) {
5907 snprintf(label
, sizeof(label
), "parallel%d", i
);
5908 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5909 if (!parallel_hds
[i
]) {
5910 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5917 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5918 const char *devname
= virtio_consoles
[i
];
5919 if (devname
&& strcmp(devname
, "none")) {
5921 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5922 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5923 if (!virtcon_hds
[i
]) {
5924 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5931 module_call_init(MODULE_INIT_DEVICE
);
5934 i
= select_watchdog(watchdog
);
5936 exit (i
== 1 ? 1 : 0);
5939 if (machine
->compat_props
) {
5940 qdev_prop_register_compat(machine
->compat_props
);
5942 machine
->init(ram_size
, boot_devices
,
5943 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5946 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5947 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5948 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5954 current_machine
= machine
;
5956 /* init USB devices */
5958 foreach_device_config(DEV_USB
, usb_parse
);
5961 /* init generic devices */
5962 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5966 dumb_display_init();
5967 /* just use the first displaystate for the moment */
5970 if (display_type
== DT_DEFAULT
) {
5971 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5972 display_type
= DT_SDL
;
5974 display_type
= DT_VNC
;
5975 vnc_display
= "localhost:0,to=99";
5981 switch (display_type
) {
5984 #if defined(CONFIG_CURSES)
5986 curses_display_init(ds
, full_screen
);
5989 #if defined(CONFIG_SDL)
5991 sdl_display_init(ds
, full_screen
, no_frame
);
5993 #elif defined(CONFIG_COCOA)
5995 cocoa_display_init(ds
, full_screen
);
5999 vnc_display_init(ds
);
6000 if (vnc_display_open(ds
, vnc_display
) < 0)
6003 if (show_vnc_port
) {
6004 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6012 dcl
= ds
->listeners
;
6013 while (dcl
!= NULL
) {
6014 if (dcl
->dpy_refresh
!= NULL
) {
6015 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6016 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6021 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6022 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6023 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6026 text_consoles_set_display(display_state
);
6027 qemu_chr_initial_reset();
6029 if (monitor_device
&& monitor_hd
)
6030 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6032 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6033 const char *devname
= serial_devices
[i
];
6034 if (devname
&& strcmp(devname
, "none")) {
6035 if (strstart(devname
, "vc", 0))
6036 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6040 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6041 const char *devname
= parallel_devices
[i
];
6042 if (devname
&& strcmp(devname
, "none")) {
6043 if (strstart(devname
, "vc", 0))
6044 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6048 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6049 const char *devname
= virtio_consoles
[i
];
6050 if (virtcon_hds
[i
] && devname
) {
6051 if (strstart(devname
, "vc", 0))
6052 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6056 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6057 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6063 if (load_vmstate(cur_mon
, loadvm
) < 0) {
6069 qemu_start_incoming_migration(incoming
);
6070 } else if (autostart
) {
6080 len
= write(fds
[1], &status
, 1);
6081 if (len
== -1 && (errno
== EINTR
))
6088 TFR(fd
= open("/dev/null", O_RDWR
));
6094 pwd
= getpwnam(run_as
);
6096 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6102 if (chroot(chroot_dir
) < 0) {
6103 fprintf(stderr
, "chroot failed\n");
6110 if (setgid(pwd
->pw_gid
) < 0) {
6111 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6114 if (setuid(pwd
->pw_uid
) < 0) {
6115 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6118 if (setuid(0) != -1) {
6119 fprintf(stderr
, "Dropping privileges failed\n");