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;
229 int graphic_rotate
= 0;
230 uint8_t irq0override
= 1;
234 const char *watchdog
;
235 const char *option_rom
[MAX_OPTION_ROMS
];
237 int semihosting_enabled
= 0;
241 const char *qemu_name
;
243 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
244 unsigned int nb_prom_envs
= 0;
245 const char *prom_envs
[MAX_PROM_ENVS
];
250 uint64_t node_mem
[MAX_NODES
];
251 uint64_t node_cpumask
[MAX_NODES
];
253 static CPUState
*cur_cpu
;
254 static CPUState
*next_cpu
;
255 static int timer_alarm_pending
= 1;
256 /* Conversion factor from emulated instructions to virtual clock ticks. */
257 static int icount_time_shift
;
258 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
259 #define MAX_ICOUNT_SHIFT 10
260 /* Compensate for varying guest execution speed. */
261 static int64_t qemu_icount_bias
;
262 static QEMUTimer
*icount_rt_timer
;
263 static QEMUTimer
*icount_vm_timer
;
264 static QEMUTimer
*nographic_timer
;
266 uint8_t qemu_uuid
[16];
268 static QEMUBootSetHandler
*boot_set_handler
;
269 static void *boot_set_opaque
;
271 /***********************************************************/
272 /* x86 ISA bus support */
274 target_phys_addr_t isa_mem_base
= 0;
277 /***********************************************************/
278 void hw_error(const char *fmt
, ...)
284 fprintf(stderr
, "qemu: hardware error: ");
285 vfprintf(stderr
, fmt
, ap
);
286 fprintf(stderr
, "\n");
287 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
288 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
290 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
292 cpu_dump_state(env
, stderr
, fprintf
, 0);
299 static void set_proc_name(const char *s
)
301 #if defined(__linux__) && defined(PR_SET_NAME)
305 name
[sizeof(name
) - 1] = 0;
306 strncpy(name
, s
, sizeof(name
));
307 /* Could rewrite argv[0] too, but that's a bit more complicated.
308 This simple way is enough for `top'. */
309 prctl(PR_SET_NAME
, name
);
316 static QEMUBalloonEvent
*qemu_balloon_event
;
317 void *qemu_balloon_event_opaque
;
319 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
321 qemu_balloon_event
= func
;
322 qemu_balloon_event_opaque
= opaque
;
325 void qemu_balloon(ram_addr_t target
)
327 if (qemu_balloon_event
)
328 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
331 ram_addr_t
qemu_balloon_status(void)
333 if (qemu_balloon_event
)
334 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
338 /***********************************************************/
341 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
342 static void *qemu_put_kbd_event_opaque
;
343 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
344 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
346 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
348 qemu_put_kbd_event_opaque
= opaque
;
349 qemu_put_kbd_event
= func
;
352 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
353 void *opaque
, int absolute
,
356 QEMUPutMouseEntry
*s
, *cursor
;
358 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
360 s
->qemu_put_mouse_event
= func
;
361 s
->qemu_put_mouse_event_opaque
= opaque
;
362 s
->qemu_put_mouse_event_absolute
= absolute
;
363 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
366 if (!qemu_put_mouse_event_head
) {
367 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
371 cursor
= qemu_put_mouse_event_head
;
372 while (cursor
->next
!= NULL
)
373 cursor
= cursor
->next
;
376 qemu_put_mouse_event_current
= s
;
381 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
383 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
385 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
388 cursor
= qemu_put_mouse_event_head
;
389 while (cursor
!= NULL
&& cursor
!= entry
) {
391 cursor
= cursor
->next
;
394 if (cursor
== NULL
) // does not exist or list empty
396 else if (prev
== NULL
) { // entry is head
397 qemu_put_mouse_event_head
= cursor
->next
;
398 if (qemu_put_mouse_event_current
== entry
)
399 qemu_put_mouse_event_current
= cursor
->next
;
400 qemu_free(entry
->qemu_put_mouse_event_name
);
405 prev
->next
= entry
->next
;
407 if (qemu_put_mouse_event_current
== entry
)
408 qemu_put_mouse_event_current
= prev
;
410 qemu_free(entry
->qemu_put_mouse_event_name
);
414 void kbd_put_keycode(int keycode
)
416 if (qemu_put_kbd_event
) {
417 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
421 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
423 QEMUPutMouseEvent
*mouse_event
;
424 void *mouse_event_opaque
;
427 if (!qemu_put_mouse_event_current
) {
432 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
434 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
437 if (graphic_rotate
) {
438 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
441 width
= graphic_width
- 1;
442 mouse_event(mouse_event_opaque
,
443 width
- dy
, dx
, dz
, buttons_state
);
445 mouse_event(mouse_event_opaque
,
446 dx
, dy
, dz
, buttons_state
);
450 int kbd_mouse_is_absolute(void)
452 if (!qemu_put_mouse_event_current
)
455 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
458 void do_info_mice(Monitor
*mon
)
460 QEMUPutMouseEntry
*cursor
;
463 if (!qemu_put_mouse_event_head
) {
464 monitor_printf(mon
, "No mouse devices connected\n");
468 monitor_printf(mon
, "Mouse devices available:\n");
469 cursor
= qemu_put_mouse_event_head
;
470 while (cursor
!= NULL
) {
471 monitor_printf(mon
, "%c Mouse #%d: %s\n",
472 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
473 index
, cursor
->qemu_put_mouse_event_name
);
475 cursor
= cursor
->next
;
479 void do_mouse_set(Monitor
*mon
, int index
)
481 QEMUPutMouseEntry
*cursor
;
484 if (!qemu_put_mouse_event_head
) {
485 monitor_printf(mon
, "No mouse devices connected\n");
489 cursor
= qemu_put_mouse_event_head
;
490 while (cursor
!= NULL
&& index
!= i
) {
492 cursor
= cursor
->next
;
496 qemu_put_mouse_event_current
= cursor
;
498 monitor_printf(mon
, "Mouse at given index not found\n");
501 /* compute with 96 bit intermediate result: (a*b)/c */
502 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
507 #ifdef HOST_WORDS_BIGENDIAN
517 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
518 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
521 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
525 /***********************************************************/
526 /* real time host monotonic timer */
528 #define QEMU_TIMER_BASE 1000000000LL
532 static int64_t clock_freq
;
534 static void init_get_clock(void)
538 ret
= QueryPerformanceFrequency(&freq
);
540 fprintf(stderr
, "Could not calibrate ticks\n");
543 clock_freq
= freq
.QuadPart
;
546 static int64_t get_clock(void)
549 QueryPerformanceCounter(&ti
);
550 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
555 static int use_rt_clock
;
557 static void init_get_clock(void)
560 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
561 || defined(__DragonFly__)
564 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
571 static int64_t get_clock(void)
573 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
574 || defined(__DragonFly__)
577 clock_gettime(CLOCK_MONOTONIC
, &ts
);
578 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
582 /* XXX: using gettimeofday leads to problems if the date
583 changes, so it should be avoided. */
585 gettimeofday(&tv
, NULL
);
586 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
591 /* Return the virtual CPU time, based on the instruction counter. */
592 static int64_t cpu_get_icount(void)
595 CPUState
*env
= cpu_single_env
;;
596 icount
= qemu_icount
;
599 fprintf(stderr
, "Bad clock read\n");
600 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
602 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
605 /***********************************************************/
606 /* guest cycle counter */
608 static int64_t cpu_ticks_prev
;
609 static int64_t cpu_ticks_offset
;
610 static int64_t cpu_clock_offset
;
611 static int cpu_ticks_enabled
;
613 /* return the host CPU cycle counter and handle stop/restart */
614 int64_t cpu_get_ticks(void)
617 return cpu_get_icount();
619 if (!cpu_ticks_enabled
) {
620 return cpu_ticks_offset
;
623 ticks
= cpu_get_real_ticks();
624 if (cpu_ticks_prev
> ticks
) {
625 /* Note: non increasing ticks may happen if the host uses
627 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
629 cpu_ticks_prev
= ticks
;
630 return ticks
+ cpu_ticks_offset
;
634 /* return the host CPU monotonic timer and handle stop/restart */
635 static int64_t cpu_get_clock(void)
638 if (!cpu_ticks_enabled
) {
639 return cpu_clock_offset
;
642 return ti
+ cpu_clock_offset
;
646 /* enable cpu_get_ticks() */
647 void cpu_enable_ticks(void)
649 if (!cpu_ticks_enabled
) {
650 cpu_ticks_offset
-= cpu_get_real_ticks();
651 cpu_clock_offset
-= get_clock();
652 cpu_ticks_enabled
= 1;
656 /* disable cpu_get_ticks() : the clock is stopped. You must not call
657 cpu_get_ticks() after that. */
658 void cpu_disable_ticks(void)
660 if (cpu_ticks_enabled
) {
661 cpu_ticks_offset
= cpu_get_ticks();
662 cpu_clock_offset
= cpu_get_clock();
663 cpu_ticks_enabled
= 0;
667 /***********************************************************/
670 #define QEMU_TIMER_REALTIME 0
671 #define QEMU_TIMER_VIRTUAL 1
675 /* XXX: add frequency */
683 struct QEMUTimer
*next
;
686 struct qemu_alarm_timer
{
690 int (*start
)(struct qemu_alarm_timer
*t
);
691 void (*stop
)(struct qemu_alarm_timer
*t
);
692 void (*rearm
)(struct qemu_alarm_timer
*t
);
696 #define ALARM_FLAG_DYNTICKS 0x1
697 #define ALARM_FLAG_EXPIRED 0x2
699 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
701 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
704 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
706 if (!alarm_has_dynticks(t
))
712 /* TODO: MIN_TIMER_REARM_US should be optimized */
713 #define MIN_TIMER_REARM_US 250
715 static struct qemu_alarm_timer
*alarm_timer
;
719 struct qemu_alarm_win32
{
722 } alarm_win32_data
= {0, -1};
724 static int win32_start_timer(struct qemu_alarm_timer
*t
);
725 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
726 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
730 static int unix_start_timer(struct qemu_alarm_timer
*t
);
731 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
735 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
736 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
737 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
739 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
740 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
742 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
743 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
745 #endif /* __linux__ */
749 /* Correlation between real and virtual time is always going to be
750 fairly approximate, so ignore small variation.
751 When the guest is idle real and virtual time will be aligned in
753 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
755 static void icount_adjust(void)
760 static int64_t last_delta
;
761 /* If the VM is not running, then do nothing. */
765 cur_time
= cpu_get_clock();
766 cur_icount
= qemu_get_clock(vm_clock
);
767 delta
= cur_icount
- cur_time
;
768 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
770 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
771 && icount_time_shift
> 0) {
772 /* The guest is getting too far ahead. Slow time down. */
776 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
777 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
778 /* The guest is getting too far behind. Speed time up. */
782 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
785 static void icount_adjust_rt(void * opaque
)
787 qemu_mod_timer(icount_rt_timer
,
788 qemu_get_clock(rt_clock
) + 1000);
792 static void icount_adjust_vm(void * opaque
)
794 qemu_mod_timer(icount_vm_timer
,
795 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
799 static void init_icount_adjust(void)
801 /* Have both realtime and virtual time triggers for speed adjustment.
802 The realtime trigger catches emulated time passing too slowly,
803 the virtual time trigger catches emulated time passing too fast.
804 Realtime triggers occur even when idle, so use them less frequently
806 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
807 qemu_mod_timer(icount_rt_timer
,
808 qemu_get_clock(rt_clock
) + 1000);
809 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
810 qemu_mod_timer(icount_vm_timer
,
811 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
814 static struct qemu_alarm_timer alarm_timers
[] = {
817 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
818 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
819 /* HPET - if available - is preferred */
820 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
821 /* ...otherwise try RTC */
822 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
824 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
826 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
827 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
828 {"win32", 0, win32_start_timer
,
829 win32_stop_timer
, NULL
, &alarm_win32_data
},
834 static void show_available_alarms(void)
838 printf("Available alarm timers, in order of precedence:\n");
839 for (i
= 0; alarm_timers
[i
].name
; i
++)
840 printf("%s\n", alarm_timers
[i
].name
);
843 static void configure_alarms(char const *opt
)
847 int count
= ARRAY_SIZE(alarm_timers
) - 1;
850 struct qemu_alarm_timer tmp
;
852 if (!strcmp(opt
, "?")) {
853 show_available_alarms();
859 /* Reorder the array */
860 name
= strtok(arg
, ",");
862 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
863 if (!strcmp(alarm_timers
[i
].name
, name
))
868 fprintf(stderr
, "Unknown clock %s\n", name
);
877 tmp
= alarm_timers
[i
];
878 alarm_timers
[i
] = alarm_timers
[cur
];
879 alarm_timers
[cur
] = tmp
;
883 name
= strtok(NULL
, ",");
889 /* Disable remaining timers */
890 for (i
= cur
; i
< count
; i
++)
891 alarm_timers
[i
].name
= NULL
;
893 show_available_alarms();
901 static QEMUTimer
*active_timers
[2];
903 static QEMUClock
*qemu_new_clock(int type
)
906 clock
= qemu_mallocz(sizeof(QEMUClock
));
911 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
915 ts
= qemu_mallocz(sizeof(QEMUTimer
));
922 void qemu_free_timer(QEMUTimer
*ts
)
927 /* stop a timer, but do not dealloc it */
928 void qemu_del_timer(QEMUTimer
*ts
)
932 /* NOTE: this code must be signal safe because
933 qemu_timer_expired() can be called from a signal. */
934 pt
= &active_timers
[ts
->clock
->type
];
947 /* modify the current timer so that it will be fired when current_time
948 >= expire_time. The corresponding callback will be called. */
949 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
955 /* add the timer in the sorted list */
956 /* NOTE: this code must be signal safe because
957 qemu_timer_expired() can be called from a signal. */
958 pt
= &active_timers
[ts
->clock
->type
];
963 if (t
->expire_time
> expire_time
)
967 ts
->expire_time
= expire_time
;
971 /* Rearm if necessary */
972 if (pt
== &active_timers
[ts
->clock
->type
]) {
973 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
974 qemu_rearm_alarm_timer(alarm_timer
);
976 /* Interrupt execution to force deadline recalculation. */
982 int qemu_timer_pending(QEMUTimer
*ts
)
985 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
992 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
996 return (timer_head
->expire_time
<= current_time
);
999 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1005 if (!ts
|| ts
->expire_time
> current_time
)
1007 /* remove timer from the list before calling the callback */
1008 *ptimer_head
= ts
->next
;
1011 /* run the callback (the timer list can be modified) */
1016 int64_t qemu_get_clock(QEMUClock
*clock
)
1018 switch(clock
->type
) {
1019 case QEMU_TIMER_REALTIME
:
1020 return get_clock() / 1000000;
1022 case QEMU_TIMER_VIRTUAL
:
1024 return cpu_get_icount();
1026 return cpu_get_clock();
1031 static void init_timers(void)
1034 ticks_per_sec
= QEMU_TIMER_BASE
;
1035 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1036 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1040 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1042 uint64_t expire_time
;
1044 if (qemu_timer_pending(ts
)) {
1045 expire_time
= ts
->expire_time
;
1049 qemu_put_be64(f
, expire_time
);
1052 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1054 uint64_t expire_time
;
1056 expire_time
= qemu_get_be64(f
);
1057 if (expire_time
!= -1) {
1058 qemu_mod_timer(ts
, expire_time
);
1064 static void timer_save(QEMUFile
*f
, void *opaque
)
1066 if (cpu_ticks_enabled
) {
1067 hw_error("cannot save state if virtual timers are running");
1069 qemu_put_be64(f
, cpu_ticks_offset
);
1070 qemu_put_be64(f
, ticks_per_sec
);
1071 qemu_put_be64(f
, cpu_clock_offset
);
1074 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1076 if (version_id
!= 1 && version_id
!= 2)
1078 if (cpu_ticks_enabled
) {
1081 cpu_ticks_offset
=qemu_get_be64(f
);
1082 ticks_per_sec
=qemu_get_be64(f
);
1083 if (version_id
== 2) {
1084 cpu_clock_offset
=qemu_get_be64(f
);
1089 static void qemu_event_increment(void);
1092 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1093 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1096 static void host_alarm_handler(int host_signum
)
1100 #define DISP_FREQ 1000
1102 static int64_t delta_min
= INT64_MAX
;
1103 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1105 ti
= qemu_get_clock(vm_clock
);
1106 if (last_clock
!= 0) {
1107 delta
= ti
- last_clock
;
1108 if (delta
< delta_min
)
1110 if (delta
> delta_max
)
1113 if (++count
== DISP_FREQ
) {
1114 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1115 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1116 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1117 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1118 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1120 delta_min
= INT64_MAX
;
1128 if (alarm_has_dynticks(alarm_timer
) ||
1130 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1131 qemu_get_clock(vm_clock
))) ||
1132 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1133 qemu_get_clock(rt_clock
))) {
1134 qemu_event_increment();
1135 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1137 #ifndef CONFIG_IOTHREAD
1139 /* stop the currently executing cpu because a timer occured */
1143 timer_alarm_pending
= 1;
1144 qemu_notify_event();
1148 static int64_t qemu_next_deadline(void)
1152 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1153 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1154 qemu_get_clock(vm_clock
);
1156 /* To avoid problems with overflow limit this to 2^32. */
1166 #if defined(__linux__) || defined(_WIN32)
1167 static uint64_t qemu_next_deadline_dyntick(void)
1175 delta
= (qemu_next_deadline() + 999) / 1000;
1177 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1178 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1179 qemu_get_clock(rt_clock
))*1000;
1180 if (rtdelta
< delta
)
1184 if (delta
< MIN_TIMER_REARM_US
)
1185 delta
= MIN_TIMER_REARM_US
;
1193 /* Sets a specific flag */
1194 static int fcntl_setfl(int fd
, int flag
)
1198 flags
= fcntl(fd
, F_GETFL
);
1202 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1208 #if defined(__linux__)
1210 #define RTC_FREQ 1024
1212 static void enable_sigio_timer(int fd
)
1214 struct sigaction act
;
1217 sigfillset(&act
.sa_mask
);
1219 act
.sa_handler
= host_alarm_handler
;
1221 sigaction(SIGIO
, &act
, NULL
);
1222 fcntl_setfl(fd
, O_ASYNC
);
1223 fcntl(fd
, F_SETOWN
, getpid());
1226 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1228 struct hpet_info info
;
1231 fd
= open("/dev/hpet", O_RDONLY
);
1236 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1238 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1239 "error, but for better emulation accuracy type:\n"
1240 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1244 /* Check capabilities */
1245 r
= ioctl(fd
, HPET_INFO
, &info
);
1249 /* Enable periodic mode */
1250 r
= ioctl(fd
, HPET_EPI
, 0);
1251 if (info
.hi_flags
&& (r
< 0))
1254 /* Enable interrupt */
1255 r
= ioctl(fd
, HPET_IE_ON
, 0);
1259 enable_sigio_timer(fd
);
1260 t
->priv
= (void *)(long)fd
;
1268 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1270 int fd
= (long)t
->priv
;
1275 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1278 unsigned long current_rtc_freq
= 0;
1280 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1283 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1284 if (current_rtc_freq
!= RTC_FREQ
&&
1285 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1286 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1287 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1288 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1291 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1297 enable_sigio_timer(rtc_fd
);
1299 t
->priv
= (void *)(long)rtc_fd
;
1304 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1306 int rtc_fd
= (long)t
->priv
;
1311 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1315 struct sigaction act
;
1317 sigfillset(&act
.sa_mask
);
1319 act
.sa_handler
= host_alarm_handler
;
1321 sigaction(SIGALRM
, &act
, NULL
);
1324 * Initialize ev struct to 0 to avoid valgrind complaining
1325 * about uninitialized data in timer_create call
1327 memset(&ev
, 0, sizeof(ev
));
1328 ev
.sigev_value
.sival_int
= 0;
1329 ev
.sigev_notify
= SIGEV_SIGNAL
;
1330 ev
.sigev_signo
= SIGALRM
;
1332 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1333 perror("timer_create");
1335 /* disable dynticks */
1336 fprintf(stderr
, "Dynamic Ticks disabled\n");
1341 t
->priv
= (void *)(long)host_timer
;
1346 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1348 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1350 timer_delete(host_timer
);
1353 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1355 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1356 struct itimerspec timeout
;
1357 int64_t nearest_delta_us
= INT64_MAX
;
1360 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1361 !active_timers
[QEMU_TIMER_VIRTUAL
])
1364 nearest_delta_us
= qemu_next_deadline_dyntick();
1366 /* check whether a timer is already running */
1367 if (timer_gettime(host_timer
, &timeout
)) {
1369 fprintf(stderr
, "Internal timer error: aborting\n");
1372 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1373 if (current_us
&& current_us
<= nearest_delta_us
)
1376 timeout
.it_interval
.tv_sec
= 0;
1377 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1378 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1379 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1380 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1382 fprintf(stderr
, "Internal timer error: aborting\n");
1387 #endif /* defined(__linux__) */
1389 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1391 struct sigaction act
;
1392 struct itimerval itv
;
1396 sigfillset(&act
.sa_mask
);
1398 act
.sa_handler
= host_alarm_handler
;
1400 sigaction(SIGALRM
, &act
, NULL
);
1402 itv
.it_interval
.tv_sec
= 0;
1403 /* for i386 kernel 2.6 to get 1 ms */
1404 itv
.it_interval
.tv_usec
= 999;
1405 itv
.it_value
.tv_sec
= 0;
1406 itv
.it_value
.tv_usec
= 10 * 1000;
1408 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1415 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1417 struct itimerval itv
;
1419 memset(&itv
, 0, sizeof(itv
));
1420 setitimer(ITIMER_REAL
, &itv
, NULL
);
1423 #endif /* !defined(_WIN32) */
1428 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1431 struct qemu_alarm_win32
*data
= t
->priv
;
1434 memset(&tc
, 0, sizeof(tc
));
1435 timeGetDevCaps(&tc
, sizeof(tc
));
1437 if (data
->period
< tc
.wPeriodMin
)
1438 data
->period
= tc
.wPeriodMin
;
1440 timeBeginPeriod(data
->period
);
1442 flags
= TIME_CALLBACK_FUNCTION
;
1443 if (alarm_has_dynticks(t
))
1444 flags
|= TIME_ONESHOT
;
1446 flags
|= TIME_PERIODIC
;
1448 data
->timerId
= timeSetEvent(1, // interval (ms)
1449 data
->period
, // resolution
1450 host_alarm_handler
, // function
1451 (DWORD
)t
, // parameter
1454 if (!data
->timerId
) {
1455 perror("Failed to initialize win32 alarm timer");
1456 timeEndPeriod(data
->period
);
1463 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1465 struct qemu_alarm_win32
*data
= t
->priv
;
1467 timeKillEvent(data
->timerId
);
1468 timeEndPeriod(data
->period
);
1471 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1473 struct qemu_alarm_win32
*data
= t
->priv
;
1474 uint64_t nearest_delta_us
;
1476 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1477 !active_timers
[QEMU_TIMER_VIRTUAL
])
1480 nearest_delta_us
= qemu_next_deadline_dyntick();
1481 nearest_delta_us
/= 1000;
1483 timeKillEvent(data
->timerId
);
1485 data
->timerId
= timeSetEvent(1,
1489 TIME_ONESHOT
| TIME_PERIODIC
);
1491 if (!data
->timerId
) {
1492 perror("Failed to re-arm win32 alarm timer");
1494 timeEndPeriod(data
->period
);
1501 static int init_timer_alarm(void)
1503 struct qemu_alarm_timer
*t
= NULL
;
1506 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1507 t
= &alarm_timers
[i
];
1527 static void quit_timers(void)
1529 alarm_timer
->stop(alarm_timer
);
1533 /***********************************************************/
1534 /* host time/date access */
1535 void qemu_get_timedate(struct tm
*tm
, int offset
)
1542 if (rtc_date_offset
== -1) {
1546 ret
= localtime(&ti
);
1548 ti
-= rtc_date_offset
;
1552 memcpy(tm
, ret
, sizeof(struct tm
));
1555 int qemu_timedate_diff(struct tm
*tm
)
1559 if (rtc_date_offset
== -1)
1561 seconds
= mktimegm(tm
);
1563 seconds
= mktime(tm
);
1565 seconds
= mktimegm(tm
) + rtc_date_offset
;
1567 return seconds
- time(NULL
);
1571 static void socket_cleanup(void)
1576 static int socket_init(void)
1581 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1583 err
= WSAGetLastError();
1584 fprintf(stderr
, "WSAStartup: %d\n", err
);
1587 atexit(socket_cleanup
);
1592 /***********************************************************/
1593 /* Bluetooth support */
1596 static struct HCIInfo
*hci_table
[MAX_NICS
];
1598 static struct bt_vlan_s
{
1599 struct bt_scatternet_s net
;
1601 struct bt_vlan_s
*next
;
1604 /* find or alloc a new bluetooth "VLAN" */
1605 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1607 struct bt_vlan_s
**pvlan
, *vlan
;
1608 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1612 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1614 pvlan
= &first_bt_vlan
;
1615 while (*pvlan
!= NULL
)
1616 pvlan
= &(*pvlan
)->next
;
1621 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1625 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1630 static struct HCIInfo null_hci
= {
1631 .cmd_send
= null_hci_send
,
1632 .sco_send
= null_hci_send
,
1633 .acl_send
= null_hci_send
,
1634 .bdaddr_set
= null_hci_addr_set
,
1637 struct HCIInfo
*qemu_next_hci(void)
1639 if (cur_hci
== nb_hcis
)
1642 return hci_table
[cur_hci
++];
1645 static struct HCIInfo
*hci_init(const char *str
)
1648 struct bt_scatternet_s
*vlan
= 0;
1650 if (!strcmp(str
, "null"))
1653 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1655 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1656 else if (!strncmp(str
, "hci", 3)) {
1659 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1660 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1665 vlan
= qemu_find_bt_vlan(0);
1667 return bt_new_hci(vlan
);
1670 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1675 static int bt_hci_parse(const char *str
)
1677 struct HCIInfo
*hci
;
1680 if (nb_hcis
>= MAX_NICS
) {
1681 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1685 hci
= hci_init(str
);
1694 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1695 hci
->bdaddr_set(hci
, bdaddr
.b
);
1697 hci_table
[nb_hcis
++] = hci
;
1702 static void bt_vhci_add(int vlan_id
)
1704 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1707 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1708 "an empty scatternet %i\n", vlan_id
);
1710 bt_vhci_init(bt_new_hci(vlan
));
1713 static struct bt_device_s
*bt_device_add(const char *opt
)
1715 struct bt_scatternet_s
*vlan
;
1717 char *endp
= strstr(opt
, ",vlan=");
1718 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1721 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1724 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1726 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1731 vlan
= qemu_find_bt_vlan(vlan_id
);
1734 fprintf(stderr
, "qemu: warning: adding a slave device to "
1735 "an empty scatternet %i\n", vlan_id
);
1737 if (!strcmp(devname
, "keyboard"))
1738 return bt_keyboard_init(vlan
);
1740 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1744 static int bt_parse(const char *opt
)
1746 const char *endp
, *p
;
1749 if (strstart(opt
, "hci", &endp
)) {
1750 if (!*endp
|| *endp
== ',') {
1752 if (!strstart(endp
, ",vlan=", 0))
1755 return bt_hci_parse(opt
);
1757 } else if (strstart(opt
, "vhci", &endp
)) {
1758 if (!*endp
|| *endp
== ',') {
1760 if (strstart(endp
, ",vlan=", &p
)) {
1761 vlan
= strtol(p
, (char **) &endp
, 0);
1763 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1767 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1776 } else if (strstart(opt
, "device:", &endp
))
1777 return !bt_device_add(endp
);
1779 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1783 /***********************************************************/
1784 /* QEMU Block devices */
1786 #define HD_ALIAS "index=%d,media=disk"
1787 #define CDROM_ALIAS "index=2,media=cdrom"
1788 #define FD_ALIAS "index=%d,if=floppy"
1789 #define PFLASH_ALIAS "if=pflash"
1790 #define MTD_ALIAS "if=mtd"
1791 #define SD_ALIAS "index=0,if=sd"
1793 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1800 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1803 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1805 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1806 __FUNCTION__
, optstr
);
1810 qemu_opt_set(opts
, "file", file
);
1814 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1818 /* seek interface, bus and unit */
1820 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1821 if (dinfo
->type
== type
&&
1822 dinfo
->bus
== bus
&&
1823 dinfo
->unit
== unit
)
1830 DriveInfo
*drive_get_by_id(const char *id
)
1834 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1835 if (strcmp(id
, dinfo
->id
))
1842 int drive_get_max_bus(BlockInterfaceType type
)
1848 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1849 if(dinfo
->type
== type
&&
1850 dinfo
->bus
> max_bus
)
1851 max_bus
= dinfo
->bus
;
1856 const char *drive_get_serial(BlockDriverState
*bdrv
)
1860 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1861 if (dinfo
->bdrv
== bdrv
)
1862 return dinfo
->serial
;
1868 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1872 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1873 if (dinfo
->bdrv
== bdrv
)
1874 return dinfo
->onerror
;
1877 return BLOCK_ERR_STOP_ENOSPC
;
1880 static void bdrv_format_print(void *opaque
, const char *name
)
1882 fprintf(stderr
, " %s", name
);
1885 void drive_uninit(BlockDriverState
*bdrv
)
1889 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1890 if (dinfo
->bdrv
!= bdrv
)
1892 qemu_opts_del(dinfo
->opts
);
1893 TAILQ_REMOVE(&drives
, dinfo
, next
);
1899 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1903 const char *file
= NULL
;
1906 const char *mediastr
= "";
1907 BlockInterfaceType type
;
1908 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1909 int bus_id
, unit_id
;
1910 int cyls
, heads
, secs
, translation
;
1911 BlockDriver
*drv
= NULL
;
1912 QEMUMachine
*machine
= opaque
;
1917 int bdrv_flags
, onerror
;
1918 const char *devaddr
;
1924 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1927 if (machine
->use_scsi
) {
1929 max_devs
= MAX_SCSI_DEVS
;
1930 pstrcpy(devname
, sizeof(devname
), "scsi");
1933 max_devs
= MAX_IDE_DEVS
;
1934 pstrcpy(devname
, sizeof(devname
), "ide");
1938 /* extract parameters */
1939 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1940 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1941 index
= qemu_opt_get_number(opts
, "index", -1);
1943 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1944 heads
= qemu_opt_get_number(opts
, "heads", 0);
1945 secs
= qemu_opt_get_number(opts
, "secs", 0);
1947 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1949 file
= qemu_opt_get(opts
, "file");
1950 serial
= qemu_opt_get(opts
, "serial");
1952 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1953 pstrcpy(devname
, sizeof(devname
), buf
);
1954 if (!strcmp(buf
, "ide")) {
1956 max_devs
= MAX_IDE_DEVS
;
1957 } else if (!strcmp(buf
, "scsi")) {
1959 max_devs
= MAX_SCSI_DEVS
;
1960 } else if (!strcmp(buf
, "floppy")) {
1963 } else if (!strcmp(buf
, "pflash")) {
1966 } else if (!strcmp(buf
, "mtd")) {
1969 } else if (!strcmp(buf
, "sd")) {
1972 } else if (!strcmp(buf
, "virtio")) {
1975 } else if (!strcmp(buf
, "xen")) {
1978 } else if (!strcmp(buf
, "none")) {
1982 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1987 if (cyls
|| heads
|| secs
) {
1988 if (cyls
< 1 || cyls
> 16383) {
1989 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1992 if (heads
< 1 || heads
> 16) {
1993 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1996 if (secs
< 1 || secs
> 63) {
1997 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2002 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2005 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2009 if (!strcmp(buf
, "none"))
2010 translation
= BIOS_ATA_TRANSLATION_NONE
;
2011 else if (!strcmp(buf
, "lba"))
2012 translation
= BIOS_ATA_TRANSLATION_LBA
;
2013 else if (!strcmp(buf
, "auto"))
2014 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2016 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2021 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2022 if (!strcmp(buf
, "disk")) {
2024 } else if (!strcmp(buf
, "cdrom")) {
2025 if (cyls
|| secs
|| heads
) {
2027 "qemu: '%s' invalid physical CHS format\n", buf
);
2030 media
= MEDIA_CDROM
;
2032 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2037 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2038 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2040 else if (!strcmp(buf
, "writethrough"))
2042 else if (!strcmp(buf
, "writeback"))
2045 fprintf(stderr
, "qemu: invalid cache option\n");
2050 #ifdef CONFIG_LINUX_AIO
2051 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2052 if (!strcmp(buf
, "threads"))
2054 else if (!strcmp(buf
, "native"))
2057 fprintf(stderr
, "qemu: invalid aio option\n");
2063 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2064 if (strcmp(buf
, "?") == 0) {
2065 fprintf(stderr
, "qemu: Supported formats:");
2066 bdrv_iterate_format(bdrv_format_print
, NULL
);
2067 fprintf(stderr
, "\n");
2070 drv
= bdrv_find_format(buf
);
2072 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2077 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2078 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2079 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2080 fprintf(stderr
, "werror is no supported by this format\n");
2083 if (!strcmp(buf
, "ignore"))
2084 onerror
= BLOCK_ERR_IGNORE
;
2085 else if (!strcmp(buf
, "enospc"))
2086 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2087 else if (!strcmp(buf
, "stop"))
2088 onerror
= BLOCK_ERR_STOP_ANY
;
2089 else if (!strcmp(buf
, "report"))
2090 onerror
= BLOCK_ERR_REPORT
;
2092 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2097 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2098 if (type
!= IF_VIRTIO
) {
2099 fprintf(stderr
, "addr is not supported\n");
2104 /* compute bus and unit according index */
2107 if (bus_id
!= 0 || unit_id
!= -1) {
2109 "qemu: index cannot be used with bus and unit\n");
2117 unit_id
= index
% max_devs
;
2118 bus_id
= index
/ max_devs
;
2122 /* if user doesn't specify a unit_id,
2123 * try to find the first free
2126 if (unit_id
== -1) {
2128 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2130 if (max_devs
&& unit_id
>= max_devs
) {
2131 unit_id
-= max_devs
;
2139 if (max_devs
&& unit_id
>= max_devs
) {
2140 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2141 unit_id
, max_devs
- 1);
2146 * ignore multiple definitions
2149 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2156 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2157 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2158 dinfo
->id
= qemu_strdup(buf
);
2160 /* no id supplied -> create one */
2161 dinfo
->id
= qemu_mallocz(32);
2162 if (type
== IF_IDE
|| type
== IF_SCSI
)
2163 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2165 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2166 devname
, bus_id
, mediastr
, unit_id
);
2168 snprintf(dinfo
->id
, 32, "%s%s%i",
2169 devname
, mediastr
, unit_id
);
2171 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2172 dinfo
->devaddr
= devaddr
;
2174 dinfo
->bus
= bus_id
;
2175 dinfo
->unit
= unit_id
;
2176 dinfo
->onerror
= onerror
;
2179 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2180 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2189 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2190 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2194 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2199 /* FIXME: This isn't really a floppy, but it's a reasonable
2202 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2209 /* add virtio block device */
2210 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2211 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2212 qemu_opt_set(opts
, "drive", dinfo
->id
);
2214 qemu_opt_set(opts
, "addr", devaddr
);
2225 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2226 cache
= 2; /* always use write-back with snapshot */
2228 if (cache
== 0) /* no caching */
2229 bdrv_flags
|= BDRV_O_NOCACHE
;
2230 else if (cache
== 2) /* write-back */
2231 bdrv_flags
|= BDRV_O_CACHE_WB
;
2234 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2236 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2239 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2240 fprintf(stderr
, "qemu: could not open disk image %s\n",
2245 if (bdrv_key_required(dinfo
->bdrv
))
2251 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2253 QEMUMachine
*machine
= opaque
;
2254 int fatal_error
= 0;
2256 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2263 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2265 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2266 qemu_opt_set(opts
, "snapshot", "on");
2271 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2273 boot_set_handler
= func
;
2274 boot_set_opaque
= opaque
;
2277 int qemu_boot_set(const char *boot_devices
)
2279 if (!boot_set_handler
) {
2282 return boot_set_handler(boot_set_opaque
, boot_devices
);
2285 static int parse_bootdevices(char *devices
)
2287 /* We just do some generic consistency checks */
2291 for (p
= devices
; *p
!= '\0'; p
++) {
2292 /* Allowed boot devices are:
2293 * a-b: floppy disk drives
2294 * c-f: IDE disk drives
2295 * g-m: machine implementation dependant drives
2296 * n-p: network devices
2297 * It's up to each machine implementation to check if the given boot
2298 * devices match the actual hardware implementation and firmware
2301 if (*p
< 'a' || *p
> 'p') {
2302 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2305 if (bitmap
& (1 << (*p
- 'a'))) {
2306 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2309 bitmap
|= 1 << (*p
- 'a');
2314 static void restore_boot_devices(void *opaque
)
2316 char *standard_boot_devices
= opaque
;
2318 qemu_boot_set(standard_boot_devices
);
2320 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2321 qemu_free(standard_boot_devices
);
2324 static void numa_add(const char *optarg
)
2328 unsigned long long value
, endvalue
;
2331 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2332 if (!strcmp(option
, "node")) {
2333 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2334 nodenr
= nb_numa_nodes
;
2336 nodenr
= strtoull(option
, NULL
, 10);
2339 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2340 node_mem
[nodenr
] = 0;
2342 value
= strtoull(option
, &endptr
, 0);
2344 case 0: case 'M': case 'm':
2351 node_mem
[nodenr
] = value
;
2353 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2354 node_cpumask
[nodenr
] = 0;
2356 value
= strtoull(option
, &endptr
, 10);
2359 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2361 if (*endptr
== '-') {
2362 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2363 if (endvalue
>= 63) {
2366 "only 63 CPUs in NUMA mode supported.\n");
2368 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2373 node_cpumask
[nodenr
] = value
;
2380 static void smp_parse(const char *optarg
)
2382 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2386 smp
= strtoul(optarg
, &endptr
, 10);
2387 if (endptr
!= optarg
) {
2388 if (*endptr
== ',') {
2392 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2393 sockets
= strtoull(option
, NULL
, 10);
2394 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2395 cores
= strtoull(option
, NULL
, 10);
2396 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2397 threads
= strtoull(option
, NULL
, 10);
2398 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2399 max_cpus
= strtoull(option
, NULL
, 10);
2401 /* compute missing values, prefer sockets over cores over threads */
2402 if (smp
== 0 || sockets
== 0) {
2403 sockets
= sockets
> 0 ? sockets
: 1;
2404 cores
= cores
> 0 ? cores
: 1;
2405 threads
= threads
> 0 ? threads
: 1;
2407 smp
= cores
* threads
* sockets
;
2409 sockets
= smp
/ (cores
* threads
);
2413 threads
= threads
> 0 ? threads
: 1;
2414 cores
= smp
/ (sockets
* threads
);
2417 sockets
= smp
/ (cores
* threads
);
2419 threads
= smp
/ (cores
* sockets
);
2424 smp_cores
= cores
> 0 ? cores
: 1;
2425 smp_threads
= threads
> 0 ? threads
: 1;
2427 max_cpus
= smp_cpus
;
2430 /***********************************************************/
2433 static USBPort
*used_usb_ports
;
2434 static USBPort
*free_usb_ports
;
2436 /* ??? Maybe change this to register a hub to keep track of the topology. */
2437 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2438 usb_attachfn attach
)
2440 port
->opaque
= opaque
;
2441 port
->index
= index
;
2442 port
->attach
= attach
;
2443 port
->next
= free_usb_ports
;
2444 free_usb_ports
= port
;
2447 int usb_device_add_dev(USBDevice
*dev
)
2451 /* Find a USB port to add the device to. */
2452 port
= free_usb_ports
;
2456 /* Create a new hub and chain it on. */
2457 free_usb_ports
= NULL
;
2458 port
->next
= used_usb_ports
;
2459 used_usb_ports
= port
;
2461 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2462 usb_attach(port
, hub
);
2463 port
= free_usb_ports
;
2466 free_usb_ports
= port
->next
;
2467 port
->next
= used_usb_ports
;
2468 used_usb_ports
= port
;
2469 usb_attach(port
, dev
);
2473 static void usb_msd_password_cb(void *opaque
, int err
)
2475 USBDevice
*dev
= opaque
;
2478 usb_device_add_dev(dev
);
2480 dev
->handle_destroy(dev
);
2483 static int usb_device_add(const char *devname
, int is_hotplug
)
2488 if (!free_usb_ports
)
2491 if (strstart(devname
, "host:", &p
)) {
2492 dev
= usb_host_device_open(p
);
2493 } else if (!strcmp(devname
, "mouse")) {
2494 dev
= usb_mouse_init();
2495 } else if (!strcmp(devname
, "tablet")) {
2496 dev
= usb_tablet_init();
2497 } else if (!strcmp(devname
, "keyboard")) {
2498 dev
= usb_keyboard_init();
2499 } else if (strstart(devname
, "disk:", &p
)) {
2500 BlockDriverState
*bs
;
2502 dev
= usb_msd_init(p
);
2505 bs
= usb_msd_get_bdrv(dev
);
2506 if (bdrv_key_required(bs
)) {
2509 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2514 } else if (!strcmp(devname
, "wacom-tablet")) {
2515 dev
= usb_wacom_init();
2516 } else if (strstart(devname
, "serial:", &p
)) {
2517 dev
= usb_serial_init(p
);
2518 #ifdef CONFIG_BRLAPI
2519 } else if (!strcmp(devname
, "braille")) {
2520 dev
= usb_baum_init();
2522 } else if (strstart(devname
, "net:", &p
)) {
2525 if (net_client_init(NULL
, "nic", p
) < 0)
2527 nd_table
[nic
].model
= "usb";
2528 dev
= usb_net_init(&nd_table
[nic
]);
2529 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2530 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2531 bt_new_hci(qemu_find_bt_vlan(0)));
2538 return usb_device_add_dev(dev
);
2541 int usb_device_del_addr(int bus_num
, int addr
)
2547 if (!used_usb_ports
)
2553 lastp
= &used_usb_ports
;
2554 port
= used_usb_ports
;
2555 while (port
&& port
->dev
->addr
!= addr
) {
2556 lastp
= &port
->next
;
2564 *lastp
= port
->next
;
2565 usb_attach(port
, NULL
);
2566 dev
->handle_destroy(dev
);
2567 port
->next
= free_usb_ports
;
2568 free_usb_ports
= port
;
2572 static int usb_device_del(const char *devname
)
2577 if (strstart(devname
, "host:", &p
))
2578 return usb_host_device_close(p
);
2580 if (!used_usb_ports
)
2583 p
= strchr(devname
, '.');
2586 bus_num
= strtoul(devname
, NULL
, 0);
2587 addr
= strtoul(p
+ 1, NULL
, 0);
2589 return usb_device_del_addr(bus_num
, addr
);
2592 static int usb_parse(const char *cmdline
)
2594 return usb_device_add(cmdline
, 0);
2597 void do_usb_add(Monitor
*mon
, const char *devname
)
2599 usb_device_add(devname
, 1);
2602 void do_usb_del(Monitor
*mon
, const char *devname
)
2604 usb_device_del(devname
);
2607 void usb_info(Monitor
*mon
)
2611 const char *speed_str
;
2614 monitor_printf(mon
, "USB support not enabled\n");
2618 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2622 switch(dev
->speed
) {
2626 case USB_SPEED_FULL
:
2629 case USB_SPEED_HIGH
:
2636 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2637 0, dev
->addr
, speed_str
, dev
->devname
);
2641 /***********************************************************/
2642 /* PCMCIA/Cardbus */
2644 static struct pcmcia_socket_entry_s
{
2645 PCMCIASocket
*socket
;
2646 struct pcmcia_socket_entry_s
*next
;
2647 } *pcmcia_sockets
= 0;
2649 void pcmcia_socket_register(PCMCIASocket
*socket
)
2651 struct pcmcia_socket_entry_s
*entry
;
2653 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2654 entry
->socket
= socket
;
2655 entry
->next
= pcmcia_sockets
;
2656 pcmcia_sockets
= entry
;
2659 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2661 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2663 ptr
= &pcmcia_sockets
;
2664 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2665 if (entry
->socket
== socket
) {
2671 void pcmcia_info(Monitor
*mon
)
2673 struct pcmcia_socket_entry_s
*iter
;
2675 if (!pcmcia_sockets
)
2676 monitor_printf(mon
, "No PCMCIA sockets\n");
2678 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2679 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2680 iter
->socket
->attached
? iter
->socket
->card_string
:
2684 /***********************************************************/
2685 /* register display */
2687 struct DisplayAllocator default_allocator
= {
2688 defaultallocator_create_displaysurface
,
2689 defaultallocator_resize_displaysurface
,
2690 defaultallocator_free_displaysurface
2693 void register_displaystate(DisplayState
*ds
)
2703 DisplayState
*get_displaystate(void)
2705 return display_state
;
2708 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2710 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2711 return ds
->allocator
;
2716 static void dumb_display_init(void)
2718 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2719 ds
->allocator
= &default_allocator
;
2720 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2721 register_displaystate(ds
);
2724 /***********************************************************/
2727 typedef struct IOHandlerRecord
{
2729 IOCanRWHandler
*fd_read_poll
;
2731 IOHandler
*fd_write
;
2734 /* temporary data */
2736 struct IOHandlerRecord
*next
;
2739 static IOHandlerRecord
*first_io_handler
;
2741 /* XXX: fd_read_poll should be suppressed, but an API change is
2742 necessary in the character devices to suppress fd_can_read(). */
2743 int qemu_set_fd_handler2(int fd
,
2744 IOCanRWHandler
*fd_read_poll
,
2746 IOHandler
*fd_write
,
2749 IOHandlerRecord
**pioh
, *ioh
;
2751 if (!fd_read
&& !fd_write
) {
2752 pioh
= &first_io_handler
;
2757 if (ioh
->fd
== fd
) {
2764 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2768 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2769 ioh
->next
= first_io_handler
;
2770 first_io_handler
= ioh
;
2773 ioh
->fd_read_poll
= fd_read_poll
;
2774 ioh
->fd_read
= fd_read
;
2775 ioh
->fd_write
= fd_write
;
2776 ioh
->opaque
= opaque
;
2782 int qemu_set_fd_handler(int fd
,
2784 IOHandler
*fd_write
,
2787 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2791 /***********************************************************/
2792 /* Polling handling */
2794 typedef struct PollingEntry
{
2797 struct PollingEntry
*next
;
2800 static PollingEntry
*first_polling_entry
;
2802 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2804 PollingEntry
**ppe
, *pe
;
2805 pe
= qemu_mallocz(sizeof(PollingEntry
));
2807 pe
->opaque
= opaque
;
2808 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2813 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2815 PollingEntry
**ppe
, *pe
;
2816 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2818 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2826 /***********************************************************/
2827 /* Wait objects support */
2828 typedef struct WaitObjects
{
2830 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2831 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2832 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2835 static WaitObjects wait_objects
= {0};
2837 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2839 WaitObjects
*w
= &wait_objects
;
2841 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2843 w
->events
[w
->num
] = handle
;
2844 w
->func
[w
->num
] = func
;
2845 w
->opaque
[w
->num
] = opaque
;
2850 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2853 WaitObjects
*w
= &wait_objects
;
2856 for (i
= 0; i
< w
->num
; i
++) {
2857 if (w
->events
[i
] == handle
)
2860 w
->events
[i
] = w
->events
[i
+ 1];
2861 w
->func
[i
] = w
->func
[i
+ 1];
2862 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2870 /***********************************************************/
2871 /* ram save/restore */
2873 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2877 v
= qemu_get_byte(f
);
2880 if (qemu_get_buffer(f
, buf
, len
) != len
)
2884 v
= qemu_get_byte(f
);
2885 memset(buf
, v
, len
);
2891 if (qemu_file_has_error(f
))
2897 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2902 if (qemu_get_be32(f
) != last_ram_offset
)
2904 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2905 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2912 #define BDRV_HASH_BLOCK_SIZE 1024
2913 #define IOBUF_SIZE 4096
2914 #define RAM_CBLOCK_MAGIC 0xfabe
2916 typedef struct RamDecompressState
{
2919 uint8_t buf
[IOBUF_SIZE
];
2920 } RamDecompressState
;
2922 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2925 memset(s
, 0, sizeof(*s
));
2927 ret
= inflateInit(&s
->zstream
);
2933 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2937 s
->zstream
.avail_out
= len
;
2938 s
->zstream
.next_out
= buf
;
2939 while (s
->zstream
.avail_out
> 0) {
2940 if (s
->zstream
.avail_in
== 0) {
2941 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2943 clen
= qemu_get_be16(s
->f
);
2944 if (clen
> IOBUF_SIZE
)
2946 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2947 s
->zstream
.avail_in
= clen
;
2948 s
->zstream
.next_in
= s
->buf
;
2950 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2951 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2958 static void ram_decompress_close(RamDecompressState
*s
)
2960 inflateEnd(&s
->zstream
);
2963 #define RAM_SAVE_FLAG_FULL 0x01
2964 #define RAM_SAVE_FLAG_COMPRESS 0x02
2965 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2966 #define RAM_SAVE_FLAG_PAGE 0x08
2967 #define RAM_SAVE_FLAG_EOS 0x10
2969 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2971 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2972 uint32_t *array
= (uint32_t *)page
;
2975 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2976 if (array
[i
] != val
)
2983 static int ram_save_block(QEMUFile
*f
)
2985 static ram_addr_t current_addr
= 0;
2986 ram_addr_t saved_addr
= current_addr
;
2987 ram_addr_t addr
= 0;
2990 while (addr
< last_ram_offset
) {
2991 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2994 cpu_physical_memory_reset_dirty(current_addr
,
2995 current_addr
+ TARGET_PAGE_SIZE
,
2996 MIGRATION_DIRTY_FLAG
);
2998 p
= qemu_get_ram_ptr(current_addr
);
3000 if (is_dup_page(p
, *p
)) {
3001 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3002 qemu_put_byte(f
, *p
);
3004 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3005 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3011 addr
+= TARGET_PAGE_SIZE
;
3012 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3018 static uint64_t bytes_transferred
= 0;
3020 static ram_addr_t
ram_save_remaining(void)
3023 ram_addr_t count
= 0;
3025 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3026 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3033 uint64_t ram_bytes_remaining(void)
3035 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3038 uint64_t ram_bytes_transferred(void)
3040 return bytes_transferred
;
3043 uint64_t ram_bytes_total(void)
3045 return last_ram_offset
;
3048 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3051 uint64_t bytes_transferred_last
;
3053 uint64_t expected_time
= 0;
3055 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3056 qemu_file_set_error(f
);
3061 /* Make sure all dirty bits are set */
3062 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3063 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3064 cpu_physical_memory_set_dirty(addr
);
3067 /* Enable dirty memory tracking */
3068 cpu_physical_memory_set_dirty_tracking(1);
3070 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3073 bytes_transferred_last
= bytes_transferred
;
3074 bwidth
= get_clock();
3076 while (!qemu_file_rate_limit(f
)) {
3079 ret
= ram_save_block(f
);
3080 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3081 if (ret
== 0) /* no more blocks */
3085 bwidth
= get_clock() - bwidth
;
3086 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3088 /* if we haven't transferred anything this round, force expected_time to a
3089 * a very high value, but without crashing */
3093 /* try transferring iterative blocks of memory */
3097 /* flush all remaining blocks regardless of rate limiting */
3098 while (ram_save_block(f
) != 0) {
3099 bytes_transferred
+= TARGET_PAGE_SIZE
;
3101 cpu_physical_memory_set_dirty_tracking(0);
3104 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3106 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3108 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3111 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3113 RamDecompressState s1
, *s
= &s1
;
3117 if (ram_decompress_open(s
, f
) < 0)
3119 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3120 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3121 fprintf(stderr
, "Error while reading ram block header\n");
3125 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3126 BDRV_HASH_BLOCK_SIZE
) < 0) {
3127 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3132 printf("Error block header\n");
3136 ram_decompress_close(s
);
3141 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3146 if (version_id
== 1)
3147 return ram_load_v1(f
, opaque
);
3149 if (version_id
== 2) {
3150 if (qemu_get_be32(f
) != last_ram_offset
)
3152 return ram_load_dead(f
, opaque
);
3155 if (version_id
!= 3)
3159 addr
= qemu_get_be64(f
);
3161 flags
= addr
& ~TARGET_PAGE_MASK
;
3162 addr
&= TARGET_PAGE_MASK
;
3164 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3165 if (addr
!= last_ram_offset
)
3169 if (flags
& RAM_SAVE_FLAG_FULL
) {
3170 if (ram_load_dead(f
, opaque
) < 0)
3174 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3175 uint8_t ch
= qemu_get_byte(f
);
3176 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3179 (!kvm_enabled() || kvm_has_sync_mmu())) {
3180 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3183 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3184 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3185 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3190 void qemu_service_io(void)
3192 qemu_notify_event();
3195 /***********************************************************/
3196 /* bottom halves (can be seen as timers which expire ASAP) */
3207 static QEMUBH
*first_bh
= NULL
;
3209 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3212 bh
= qemu_mallocz(sizeof(QEMUBH
));
3214 bh
->opaque
= opaque
;
3215 bh
->next
= first_bh
;
3220 int qemu_bh_poll(void)
3226 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3227 if (!bh
->deleted
&& bh
->scheduled
) {
3236 /* remove deleted bhs */
3250 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3258 void qemu_bh_schedule(QEMUBH
*bh
)
3264 /* stop the currently executing CPU to execute the BH ASAP */
3265 qemu_notify_event();
3268 void qemu_bh_cancel(QEMUBH
*bh
)
3273 void qemu_bh_delete(QEMUBH
*bh
)
3279 static void qemu_bh_update_timeout(int *timeout
)
3283 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3284 if (!bh
->deleted
&& bh
->scheduled
) {
3286 /* idle bottom halves will be polled at least
3288 *timeout
= MIN(10, *timeout
);
3290 /* non-idle bottom halves will be executed
3299 /***********************************************************/
3300 /* machine registration */
3302 static QEMUMachine
*first_machine
= NULL
;
3303 QEMUMachine
*current_machine
= NULL
;
3305 int qemu_register_machine(QEMUMachine
*m
)
3308 pm
= &first_machine
;
3316 static QEMUMachine
*find_machine(const char *name
)
3320 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3321 if (!strcmp(m
->name
, name
))
3323 if (m
->alias
&& !strcmp(m
->alias
, name
))
3329 static QEMUMachine
*find_default_machine(void)
3333 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3334 if (m
->is_default
) {
3341 /***********************************************************/
3342 /* main execution loop */
3344 static void gui_update(void *opaque
)
3346 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3347 DisplayState
*ds
= opaque
;
3348 DisplayChangeListener
*dcl
= ds
->listeners
;
3352 while (dcl
!= NULL
) {
3353 if (dcl
->gui_timer_interval
&&
3354 dcl
->gui_timer_interval
< interval
)
3355 interval
= dcl
->gui_timer_interval
;
3358 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3361 static void nographic_update(void *opaque
)
3363 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3365 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3368 struct vm_change_state_entry
{
3369 VMChangeStateHandler
*cb
;
3371 LIST_ENTRY (vm_change_state_entry
) entries
;
3374 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3376 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3379 VMChangeStateEntry
*e
;
3381 e
= qemu_mallocz(sizeof (*e
));
3385 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3389 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3391 LIST_REMOVE (e
, entries
);
3395 static void vm_state_notify(int running
, int reason
)
3397 VMChangeStateEntry
*e
;
3399 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3400 e
->cb(e
->opaque
, running
, reason
);
3404 static void resume_all_vcpus(void);
3405 static void pause_all_vcpus(void);
3412 vm_state_notify(1, 0);
3413 qemu_rearm_alarm_timer(alarm_timer
);
3418 /* reset/shutdown handler */
3420 typedef struct QEMUResetEntry
{
3421 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3422 QEMUResetHandler
*func
;
3426 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3427 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3428 static int reset_requested
;
3429 static int shutdown_requested
;
3430 static int powerdown_requested
;
3431 static int debug_requested
;
3432 static int vmstop_requested
;
3434 int qemu_shutdown_requested(void)
3436 int r
= shutdown_requested
;
3437 shutdown_requested
= 0;
3441 int qemu_reset_requested(void)
3443 int r
= reset_requested
;
3444 reset_requested
= 0;
3448 int qemu_powerdown_requested(void)
3450 int r
= powerdown_requested
;
3451 powerdown_requested
= 0;
3455 static int qemu_debug_requested(void)
3457 int r
= debug_requested
;
3458 debug_requested
= 0;
3462 static int qemu_vmstop_requested(void)
3464 int r
= vmstop_requested
;
3465 vmstop_requested
= 0;
3469 static void do_vm_stop(int reason
)
3472 cpu_disable_ticks();
3475 vm_state_notify(0, reason
);
3479 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3481 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3484 re
->opaque
= opaque
;
3485 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3488 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3492 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3493 if (re
->func
== func
&& re
->opaque
== opaque
) {
3494 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3501 void qemu_system_reset(void)
3503 QEMUResetEntry
*re
, *nre
;
3505 /* reset all devices */
3506 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3507 re
->func(re
->opaque
);
3511 void qemu_system_reset_request(void)
3514 shutdown_requested
= 1;
3516 reset_requested
= 1;
3518 qemu_notify_event();
3521 void qemu_system_shutdown_request(void)
3523 shutdown_requested
= 1;
3524 qemu_notify_event();
3527 void qemu_system_powerdown_request(void)
3529 powerdown_requested
= 1;
3530 qemu_notify_event();
3533 #ifdef CONFIG_IOTHREAD
3534 static void qemu_system_vmstop_request(int reason
)
3536 vmstop_requested
= reason
;
3537 qemu_notify_event();
3542 static int io_thread_fd
= -1;
3544 static void qemu_event_increment(void)
3546 static const char byte
= 0;
3548 if (io_thread_fd
== -1)
3551 write(io_thread_fd
, &byte
, sizeof(byte
));
3554 static void qemu_event_read(void *opaque
)
3556 int fd
= (unsigned long)opaque
;
3559 /* Drain the notify pipe */
3562 len
= read(fd
, buffer
, sizeof(buffer
));
3563 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3566 static int qemu_event_init(void)
3575 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3579 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3583 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3584 (void *)(unsigned long)fds
[0]);
3586 io_thread_fd
= fds
[1];
3595 HANDLE qemu_event_handle
;
3597 static void dummy_event_handler(void *opaque
)
3601 static int qemu_event_init(void)
3603 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3604 if (!qemu_event_handle
) {
3605 perror("Failed CreateEvent");
3608 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3612 static void qemu_event_increment(void)
3614 SetEvent(qemu_event_handle
);
3618 static int cpu_can_run(CPUState
*env
)
3627 #ifndef CONFIG_IOTHREAD
3628 static int qemu_init_main_loop(void)
3630 return qemu_event_init();
3633 void qemu_init_vcpu(void *_env
)
3635 CPUState
*env
= _env
;
3639 env
->nr_cores
= smp_cores
;
3640 env
->nr_threads
= smp_threads
;
3644 int qemu_cpu_self(void *env
)
3649 static void resume_all_vcpus(void)
3653 static void pause_all_vcpus(void)
3657 void qemu_cpu_kick(void *env
)
3662 void qemu_notify_event(void)
3664 CPUState
*env
= cpu_single_env
;
3671 #define qemu_mutex_lock_iothread() do { } while (0)
3672 #define qemu_mutex_unlock_iothread() do { } while (0)
3674 void vm_stop(int reason
)
3679 #else /* CONFIG_IOTHREAD */
3681 #include "qemu-thread.h"
3683 QemuMutex qemu_global_mutex
;
3684 static QemuMutex qemu_fair_mutex
;
3686 static QemuThread io_thread
;
3688 static QemuThread
*tcg_cpu_thread
;
3689 static QemuCond
*tcg_halt_cond
;
3691 static int qemu_system_ready
;
3693 static QemuCond qemu_cpu_cond
;
3695 static QemuCond qemu_system_cond
;
3696 static QemuCond qemu_pause_cond
;
3698 static void block_io_signals(void);
3699 static void unblock_io_signals(void);
3700 static int tcg_has_work(void);
3702 static int qemu_init_main_loop(void)
3706 ret
= qemu_event_init();
3710 qemu_cond_init(&qemu_pause_cond
);
3711 qemu_mutex_init(&qemu_fair_mutex
);
3712 qemu_mutex_init(&qemu_global_mutex
);
3713 qemu_mutex_lock(&qemu_global_mutex
);
3715 unblock_io_signals();
3716 qemu_thread_self(&io_thread
);
3721 static void qemu_wait_io_event(CPUState
*env
)
3723 while (!tcg_has_work())
3724 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3726 qemu_mutex_unlock(&qemu_global_mutex
);
3729 * Users of qemu_global_mutex can be starved, having no chance
3730 * to acquire it since this path will get to it first.
3731 * So use another lock to provide fairness.
3733 qemu_mutex_lock(&qemu_fair_mutex
);
3734 qemu_mutex_unlock(&qemu_fair_mutex
);
3736 qemu_mutex_lock(&qemu_global_mutex
);
3740 qemu_cond_signal(&qemu_pause_cond
);
3744 static int qemu_cpu_exec(CPUState
*env
);
3746 static void *kvm_cpu_thread_fn(void *arg
)
3748 CPUState
*env
= arg
;
3751 qemu_thread_self(env
->thread
);
3753 /* signal CPU creation */
3754 qemu_mutex_lock(&qemu_global_mutex
);
3756 qemu_cond_signal(&qemu_cpu_cond
);
3758 /* and wait for machine initialization */
3759 while (!qemu_system_ready
)
3760 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3763 if (cpu_can_run(env
))
3765 qemu_wait_io_event(env
);
3771 static void tcg_cpu_exec(void);
3773 static void *tcg_cpu_thread_fn(void *arg
)
3775 CPUState
*env
= arg
;
3778 qemu_thread_self(env
->thread
);
3780 /* signal CPU creation */
3781 qemu_mutex_lock(&qemu_global_mutex
);
3782 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3784 qemu_cond_signal(&qemu_cpu_cond
);
3786 /* and wait for machine initialization */
3787 while (!qemu_system_ready
)
3788 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3792 qemu_wait_io_event(cur_cpu
);
3798 void qemu_cpu_kick(void *_env
)
3800 CPUState
*env
= _env
;
3801 qemu_cond_broadcast(env
->halt_cond
);
3803 qemu_thread_signal(env
->thread
, SIGUSR1
);
3806 int qemu_cpu_self(void *env
)
3808 return (cpu_single_env
!= NULL
);
3811 static void cpu_signal(int sig
)
3814 cpu_exit(cpu_single_env
);
3817 static void block_io_signals(void)
3820 struct sigaction sigact
;
3823 sigaddset(&set
, SIGUSR2
);
3824 sigaddset(&set
, SIGIO
);
3825 sigaddset(&set
, SIGALRM
);
3826 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3829 sigaddset(&set
, SIGUSR1
);
3830 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3832 memset(&sigact
, 0, sizeof(sigact
));
3833 sigact
.sa_handler
= cpu_signal
;
3834 sigaction(SIGUSR1
, &sigact
, NULL
);
3837 static void unblock_io_signals(void)
3842 sigaddset(&set
, SIGUSR2
);
3843 sigaddset(&set
, SIGIO
);
3844 sigaddset(&set
, SIGALRM
);
3845 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3848 sigaddset(&set
, SIGUSR1
);
3849 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3852 static void qemu_signal_lock(unsigned int msecs
)
3854 qemu_mutex_lock(&qemu_fair_mutex
);
3856 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3857 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3858 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3861 qemu_mutex_unlock(&qemu_fair_mutex
);
3864 static void qemu_mutex_lock_iothread(void)
3866 if (kvm_enabled()) {
3867 qemu_mutex_lock(&qemu_fair_mutex
);
3868 qemu_mutex_lock(&qemu_global_mutex
);
3869 qemu_mutex_unlock(&qemu_fair_mutex
);
3871 qemu_signal_lock(100);
3874 static void qemu_mutex_unlock_iothread(void)
3876 qemu_mutex_unlock(&qemu_global_mutex
);
3879 static int all_vcpus_paused(void)
3881 CPUState
*penv
= first_cpu
;
3886 penv
= (CPUState
*)penv
->next_cpu
;
3892 static void pause_all_vcpus(void)
3894 CPUState
*penv
= first_cpu
;
3898 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3899 qemu_cpu_kick(penv
);
3900 penv
= (CPUState
*)penv
->next_cpu
;
3903 while (!all_vcpus_paused()) {
3904 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3907 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3908 penv
= (CPUState
*)penv
->next_cpu
;
3913 static void resume_all_vcpus(void)
3915 CPUState
*penv
= first_cpu
;
3920 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3921 qemu_cpu_kick(penv
);
3922 penv
= (CPUState
*)penv
->next_cpu
;
3926 static void tcg_init_vcpu(void *_env
)
3928 CPUState
*env
= _env
;
3929 /* share a single thread for all cpus with TCG */
3930 if (!tcg_cpu_thread
) {
3931 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3932 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3933 qemu_cond_init(env
->halt_cond
);
3934 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3935 while (env
->created
== 0)
3936 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3937 tcg_cpu_thread
= env
->thread
;
3938 tcg_halt_cond
= env
->halt_cond
;
3940 env
->thread
= tcg_cpu_thread
;
3941 env
->halt_cond
= tcg_halt_cond
;
3945 static void kvm_start_vcpu(CPUState
*env
)
3948 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3949 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3950 qemu_cond_init(env
->halt_cond
);
3951 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3952 while (env
->created
== 0)
3953 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3956 void qemu_init_vcpu(void *_env
)
3958 CPUState
*env
= _env
;
3961 kvm_start_vcpu(env
);
3964 env
->nr_cores
= smp_cores
;
3965 env
->nr_threads
= smp_threads
;
3968 void qemu_notify_event(void)
3970 qemu_event_increment();
3973 void vm_stop(int reason
)
3976 qemu_thread_self(&me
);
3978 if (!qemu_thread_equal(&me
, &io_thread
)) {
3979 qemu_system_vmstop_request(reason
);
3981 * FIXME: should not return to device code in case
3982 * vm_stop() has been requested.
3984 if (cpu_single_env
) {
3985 cpu_exit(cpu_single_env
);
3986 cpu_single_env
->stop
= 1;
3997 static void host_main_loop_wait(int *timeout
)
4003 /* XXX: need to suppress polling by better using win32 events */
4005 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4006 ret
|= pe
->func(pe
->opaque
);
4010 WaitObjects
*w
= &wait_objects
;
4012 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4013 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4014 if (w
->func
[ret
- WAIT_OBJECT_0
])
4015 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4017 /* Check for additional signaled events */
4018 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4020 /* Check if event is signaled */
4021 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4022 if(ret2
== WAIT_OBJECT_0
) {
4024 w
->func
[i
](w
->opaque
[i
]);
4025 } else if (ret2
== WAIT_TIMEOUT
) {
4027 err
= GetLastError();
4028 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4031 } else if (ret
== WAIT_TIMEOUT
) {
4033 err
= GetLastError();
4034 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4041 static void host_main_loop_wait(int *timeout
)
4046 void main_loop_wait(int timeout
)
4048 IOHandlerRecord
*ioh
;
4049 fd_set rfds
, wfds
, xfds
;
4053 qemu_bh_update_timeout(&timeout
);
4055 host_main_loop_wait(&timeout
);
4057 /* poll any events */
4058 /* XXX: separate device handlers from system ones */
4063 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4067 (!ioh
->fd_read_poll
||
4068 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4069 FD_SET(ioh
->fd
, &rfds
);
4073 if (ioh
->fd_write
) {
4074 FD_SET(ioh
->fd
, &wfds
);
4080 tv
.tv_sec
= timeout
/ 1000;
4081 tv
.tv_usec
= (timeout
% 1000) * 1000;
4083 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4085 qemu_mutex_unlock_iothread();
4086 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4087 qemu_mutex_lock_iothread();
4089 IOHandlerRecord
**pioh
;
4091 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4092 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4093 ioh
->fd_read(ioh
->opaque
);
4095 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4096 ioh
->fd_write(ioh
->opaque
);
4100 /* remove deleted IO handlers */
4101 pioh
= &first_io_handler
;
4112 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4114 /* rearm timer, if not periodic */
4115 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4116 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4117 qemu_rearm_alarm_timer(alarm_timer
);
4120 /* vm time timers */
4122 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4123 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4124 qemu_get_clock(vm_clock
));
4127 /* real time timers */
4128 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4129 qemu_get_clock(rt_clock
));
4131 /* Check bottom-halves last in case any of the earlier events triggered
4137 static int qemu_cpu_exec(CPUState
*env
)
4140 #ifdef CONFIG_PROFILER
4144 #ifdef CONFIG_PROFILER
4145 ti
= profile_getclock();
4150 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4151 env
->icount_decr
.u16
.low
= 0;
4152 env
->icount_extra
= 0;
4153 count
= qemu_next_deadline();
4154 count
= (count
+ (1 << icount_time_shift
) - 1)
4155 >> icount_time_shift
;
4156 qemu_icount
+= count
;
4157 decr
= (count
> 0xffff) ? 0xffff : count
;
4159 env
->icount_decr
.u16
.low
= decr
;
4160 env
->icount_extra
= count
;
4162 ret
= cpu_exec(env
);
4163 #ifdef CONFIG_PROFILER
4164 qemu_time
+= profile_getclock() - ti
;
4167 /* Fold pending instructions back into the
4168 instruction counter, and clear the interrupt flag. */
4169 qemu_icount
-= (env
->icount_decr
.u16
.low
4170 + env
->icount_extra
);
4171 env
->icount_decr
.u32
= 0;
4172 env
->icount_extra
= 0;
4177 static void tcg_cpu_exec(void)
4181 if (next_cpu
== NULL
)
4182 next_cpu
= first_cpu
;
4183 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4184 CPUState
*env
= cur_cpu
= next_cpu
;
4188 if (timer_alarm_pending
) {
4189 timer_alarm_pending
= 0;
4192 if (cpu_can_run(env
))
4193 ret
= qemu_cpu_exec(env
);
4194 if (ret
== EXCP_DEBUG
) {
4195 gdb_set_stop_cpu(env
);
4196 debug_requested
= 1;
4202 static int cpu_has_work(CPUState
*env
)
4210 if (qemu_cpu_has_work(env
))
4215 static int tcg_has_work(void)
4219 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4220 if (cpu_has_work(env
))
4225 static int qemu_calculate_timeout(void)
4227 #ifndef CONFIG_IOTHREAD
4232 else if (tcg_has_work())
4234 else if (!use_icount
)
4237 /* XXX: use timeout computed from timers */
4240 /* Advance virtual time to the next event. */
4241 if (use_icount
== 1) {
4242 /* When not using an adaptive execution frequency
4243 we tend to get badly out of sync with real time,
4244 so just delay for a reasonable amount of time. */
4247 delta
= cpu_get_icount() - cpu_get_clock();
4250 /* If virtual time is ahead of real time then just
4252 timeout
= (delta
/ 1000000) + 1;
4254 /* Wait for either IO to occur or the next
4256 add
= qemu_next_deadline();
4257 /* We advance the timer before checking for IO.
4258 Limit the amount we advance so that early IO
4259 activity won't get the guest too far ahead. */
4263 add
= (add
+ (1 << icount_time_shift
) - 1)
4264 >> icount_time_shift
;
4266 timeout
= delta
/ 1000000;
4273 #else /* CONFIG_IOTHREAD */
4278 static int vm_can_run(void)
4280 if (powerdown_requested
)
4282 if (reset_requested
)
4284 if (shutdown_requested
)
4286 if (debug_requested
)
4291 qemu_irq qemu_system_powerdown
;
4293 static void main_loop(void)
4297 #ifdef CONFIG_IOTHREAD
4298 qemu_system_ready
= 1;
4299 qemu_cond_broadcast(&qemu_system_cond
);
4304 #ifdef CONFIG_PROFILER
4307 #ifndef CONFIG_IOTHREAD
4310 #ifdef CONFIG_PROFILER
4311 ti
= profile_getclock();
4313 main_loop_wait(qemu_calculate_timeout());
4314 #ifdef CONFIG_PROFILER
4315 dev_time
+= profile_getclock() - ti
;
4317 } while (vm_can_run());
4319 if (qemu_debug_requested())
4320 vm_stop(EXCP_DEBUG
);
4321 if (qemu_shutdown_requested()) {
4328 if (qemu_reset_requested()) {
4330 qemu_system_reset();
4333 if (qemu_powerdown_requested()) {
4334 qemu_irq_raise(qemu_system_powerdown
);
4336 if ((r
= qemu_vmstop_requested()))
4342 static void version(void)
4344 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4347 static void help(int exitcode
)
4350 printf("usage: %s [options] [disk_image]\n"
4352 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4354 #define DEF(option, opt_arg, opt_enum, opt_help) \
4356 #define DEFHEADING(text) stringify(text) "\n"
4357 #include "qemu-options.h"
4362 "During emulation, the following keys are useful:\n"
4363 "ctrl-alt-f toggle full screen\n"
4364 "ctrl-alt-n switch to virtual console 'n'\n"
4365 "ctrl-alt toggle mouse and keyboard grab\n"
4367 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4372 DEFAULT_NETWORK_SCRIPT
,
4373 DEFAULT_NETWORK_DOWN_SCRIPT
,
4375 DEFAULT_GDBSTUB_PORT
,
4380 #define HAS_ARG 0x0001
4383 #define DEF(option, opt_arg, opt_enum, opt_help) \
4385 #define DEFHEADING(text)
4386 #include "qemu-options.h"
4392 typedef struct QEMUOption
{
4398 static const QEMUOption qemu_options
[] = {
4399 { "h", 0, QEMU_OPTION_h
},
4400 #define DEF(option, opt_arg, opt_enum, opt_help) \
4401 { option, opt_arg, opt_enum },
4402 #define DEFHEADING(text)
4403 #include "qemu-options.h"
4411 struct soundhw soundhw
[] = {
4412 #ifdef HAS_AUDIO_CHOICE
4413 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4419 { .init_isa
= pcspk_audio_init
}
4426 "Creative Sound Blaster 16",
4429 { .init_isa
= SB16_init
}
4433 #ifdef CONFIG_CS4231A
4439 { .init_isa
= cs4231a_init
}
4447 "Yamaha YMF262 (OPL3)",
4449 "Yamaha YM3812 (OPL2)",
4453 { .init_isa
= Adlib_init
}
4460 "Gravis Ultrasound GF1",
4463 { .init_isa
= GUS_init
}
4470 "Intel 82801AA AC97 Audio",
4473 { .init_pci
= ac97_init
}
4477 #ifdef CONFIG_ES1370
4480 "ENSONIQ AudioPCI ES1370",
4483 { .init_pci
= es1370_init
}
4487 #endif /* HAS_AUDIO_CHOICE */
4489 { NULL
, NULL
, 0, 0, { NULL
} }
4492 static void select_soundhw (const char *optarg
)
4496 if (*optarg
== '?') {
4499 printf ("Valid sound card names (comma separated):\n");
4500 for (c
= soundhw
; c
->name
; ++c
) {
4501 printf ("%-11s %s\n", c
->name
, c
->descr
);
4503 printf ("\n-soundhw all will enable all of the above\n");
4504 exit (*optarg
!= '?');
4512 if (!strcmp (optarg
, "all")) {
4513 for (c
= soundhw
; c
->name
; ++c
) {
4521 e
= strchr (p
, ',');
4522 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4524 for (c
= soundhw
; c
->name
; ++c
) {
4525 if (!strncmp (c
->name
, p
, l
)) {
4534 "Unknown sound card name (too big to show)\n");
4537 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4542 p
+= l
+ (e
!= NULL
);
4546 goto show_valid_cards
;
4551 static void select_vgahw (const char *p
)
4555 vga_interface_type
= VGA_NONE
;
4556 if (strstart(p
, "std", &opts
)) {
4557 vga_interface_type
= VGA_STD
;
4558 } else if (strstart(p
, "cirrus", &opts
)) {
4559 vga_interface_type
= VGA_CIRRUS
;
4560 } else if (strstart(p
, "vmware", &opts
)) {
4561 vga_interface_type
= VGA_VMWARE
;
4562 } else if (strstart(p
, "xenfb", &opts
)) {
4563 vga_interface_type
= VGA_XENFB
;
4564 } else if (!strstart(p
, "none", &opts
)) {
4566 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4570 const char *nextopt
;
4572 if (strstart(opts
, ",retrace=", &nextopt
)) {
4574 if (strstart(opts
, "dumb", &nextopt
))
4575 vga_retrace_method
= VGA_RETRACE_DUMB
;
4576 else if (strstart(opts
, "precise", &nextopt
))
4577 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4578 else goto invalid_vga
;
4579 } else goto invalid_vga
;
4585 static int balloon_parse(const char *arg
)
4589 if (strcmp(arg
, "none") == 0) {
4593 if (!strncmp(arg
, "virtio", 6)) {
4594 if (arg
[6] == ',') {
4595 /* have params -> parse them */
4596 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4600 /* create empty opts */
4601 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4603 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4612 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4614 exit(STATUS_CONTROL_C_EXIT
);
4619 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4623 if(strlen(str
) != 36)
4626 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4627 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4628 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4634 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4640 #define MAX_NET_CLIENTS 32
4644 static void termsig_handler(int signal
)
4646 qemu_system_shutdown_request();
4649 static void sigchld_handler(int signal
)
4651 waitpid(-1, NULL
, WNOHANG
);
4654 static void sighandler_setup(void)
4656 struct sigaction act
;
4658 memset(&act
, 0, sizeof(act
));
4659 act
.sa_handler
= termsig_handler
;
4660 sigaction(SIGINT
, &act
, NULL
);
4661 sigaction(SIGHUP
, &act
, NULL
);
4662 sigaction(SIGTERM
, &act
, NULL
);
4664 act
.sa_handler
= sigchld_handler
;
4665 act
.sa_flags
= SA_NOCLDSTOP
;
4666 sigaction(SIGCHLD
, &act
, NULL
);
4672 /* Look for support files in the same directory as the executable. */
4673 static char *find_datadir(const char *argv0
)
4679 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4686 while (p
!= buf
&& *p
!= '\\')
4689 if (access(buf
, R_OK
) == 0) {
4690 return qemu_strdup(buf
);
4696 /* Find a likely location for support files using the location of the binary.
4697 For installed binaries this will be "$bindir/../share/qemu". When
4698 running from the build tree this will be "$bindir/../pc-bios". */
4699 #define SHARE_SUFFIX "/share/qemu"
4700 #define BUILD_SUFFIX "/pc-bios"
4701 static char *find_datadir(const char *argv0
)
4711 #if defined(__linux__)
4714 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4720 #elif defined(__FreeBSD__)
4723 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4730 /* If we don't have any way of figuring out the actual executable
4731 location then try argv[0]. */
4736 p
= realpath(argv0
, p
);
4744 max_len
= strlen(dir
) +
4745 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4746 res
= qemu_mallocz(max_len
);
4747 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4748 if (access(res
, R_OK
)) {
4749 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4750 if (access(res
, R_OK
)) {
4764 char *qemu_find_file(int type
, const char *name
)
4770 /* If name contains path separators then try it as a straight path. */
4771 if ((strchr(name
, '/') || strchr(name
, '\\'))
4772 && access(name
, R_OK
) == 0) {
4773 return strdup(name
);
4776 case QEMU_FILE_TYPE_BIOS
:
4779 case QEMU_FILE_TYPE_KEYMAP
:
4780 subdir
= "keymaps/";
4785 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4786 buf
= qemu_mallocz(len
);
4787 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4788 if (access(buf
, R_OK
)) {
4795 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4799 dev
= qdev_device_add(opts
);
4805 struct device_config
{
4807 DEV_USB
, /* -usbdevice */
4810 const char *cmdline
;
4811 TAILQ_ENTRY(device_config
) next
;
4813 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4815 static void add_device_config(int type
, const char *cmdline
)
4817 struct device_config
*conf
;
4819 conf
= qemu_mallocz(sizeof(*conf
));
4821 conf
->cmdline
= cmdline
;
4822 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4825 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4827 struct device_config
*conf
;
4830 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4831 if (conf
->type
!= type
)
4833 rc
= func(conf
->cmdline
);
4840 int main(int argc
, char **argv
, char **envp
)
4842 const char *gdbstub_dev
= NULL
;
4843 uint32_t boot_devices_bitmap
= 0;
4845 int snapshot
, linux_boot
, net_boot
;
4846 const char *initrd_filename
;
4847 const char *kernel_filename
, *kernel_cmdline
;
4848 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4850 DisplayChangeListener
*dcl
;
4851 int cyls
, heads
, secs
, translation
;
4852 const char *net_clients
[MAX_NET_CLIENTS
];
4854 QemuOpts
*hda_opts
= NULL
, *opts
;
4856 const char *r
, *optarg
;
4857 CharDriverState
*monitor_hd
= NULL
;
4858 const char *monitor_device
;
4859 const char *serial_devices
[MAX_SERIAL_PORTS
];
4860 int serial_device_index
;
4861 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4862 int parallel_device_index
;
4863 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4864 int virtio_console_index
;
4865 const char *loadvm
= NULL
;
4866 QEMUMachine
*machine
;
4867 const char *cpu_model
;
4872 const char *pid_file
= NULL
;
4873 const char *incoming
= NULL
;
4876 struct passwd
*pwd
= NULL
;
4877 const char *chroot_dir
= NULL
;
4878 const char *run_as
= NULL
;
4881 int show_vnc_port
= 0;
4883 qemu_errors_to_file(stderr
);
4884 qemu_cache_utils_init(envp
);
4886 LIST_INIT (&vm_change_state_head
);
4889 struct sigaction act
;
4890 sigfillset(&act
.sa_mask
);
4892 act
.sa_handler
= SIG_IGN
;
4893 sigaction(SIGPIPE
, &act
, NULL
);
4896 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4897 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4898 QEMU to run on a single CPU */
4903 h
= GetCurrentProcess();
4904 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4905 for(i
= 0; i
< 32; i
++) {
4906 if (mask
& (1 << i
))
4911 SetProcessAffinityMask(h
, mask
);
4917 module_call_init(MODULE_INIT_MACHINE
);
4918 machine
= find_default_machine();
4920 initrd_filename
= NULL
;
4923 kernel_filename
= NULL
;
4924 kernel_cmdline
= "";
4925 cyls
= heads
= secs
= 0;
4926 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4927 monitor_device
= "vc:80Cx24C";
4929 serial_devices
[0] = "vc:80Cx24C";
4930 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4931 serial_devices
[i
] = NULL
;
4932 serial_device_index
= 0;
4934 parallel_devices
[0] = "vc:80Cx24C";
4935 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4936 parallel_devices
[i
] = NULL
;
4937 parallel_device_index
= 0;
4939 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4940 virtio_consoles
[i
] = NULL
;
4941 virtio_console_index
= 0;
4943 for (i
= 0; i
< MAX_NODES
; i
++) {
4945 node_cpumask
[i
] = 0;
4961 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4963 const QEMUOption
*popt
;
4966 /* Treat --foo the same as -foo. */
4969 popt
= qemu_options
;
4972 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4976 if (!strcmp(popt
->name
, r
+ 1))
4980 if (popt
->flags
& HAS_ARG
) {
4981 if (optind
>= argc
) {
4982 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4986 optarg
= argv
[optind
++];
4991 switch(popt
->index
) {
4993 machine
= find_machine(optarg
);
4996 printf("Supported machines are:\n");
4997 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4999 printf("%-10s %s (alias of %s)\n",
5000 m
->alias
, m
->desc
, m
->name
);
5001 printf("%-10s %s%s\n",
5003 m
->is_default
? " (default)" : "");
5005 exit(*optarg
!= '?');
5008 case QEMU_OPTION_cpu
:
5009 /* hw initialization will check this */
5010 if (*optarg
== '?') {
5011 /* XXX: implement xxx_cpu_list for targets that still miss it */
5012 #if defined(cpu_list)
5013 cpu_list(stdout
, &fprintf
);
5020 case QEMU_OPTION_initrd
:
5021 initrd_filename
= optarg
;
5023 case QEMU_OPTION_hda
:
5025 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5027 hda_opts
= drive_add(optarg
, HD_ALIAS
5028 ",cyls=%d,heads=%d,secs=%d%s",
5029 0, cyls
, heads
, secs
,
5030 translation
== BIOS_ATA_TRANSLATION_LBA
?
5032 translation
== BIOS_ATA_TRANSLATION_NONE
?
5033 ",trans=none" : "");
5035 case QEMU_OPTION_hdb
:
5036 case QEMU_OPTION_hdc
:
5037 case QEMU_OPTION_hdd
:
5038 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5040 case QEMU_OPTION_drive
:
5041 drive_add(NULL
, "%s", optarg
);
5043 case QEMU_OPTION_set
:
5044 if (qemu_set_option(optarg
) != 0)
5047 case QEMU_OPTION_mtdblock
:
5048 drive_add(optarg
, MTD_ALIAS
);
5050 case QEMU_OPTION_sd
:
5051 drive_add(optarg
, SD_ALIAS
);
5053 case QEMU_OPTION_pflash
:
5054 drive_add(optarg
, PFLASH_ALIAS
);
5056 case QEMU_OPTION_snapshot
:
5059 case QEMU_OPTION_hdachs
:
5063 cyls
= strtol(p
, (char **)&p
, 0);
5064 if (cyls
< 1 || cyls
> 16383)
5069 heads
= strtol(p
, (char **)&p
, 0);
5070 if (heads
< 1 || heads
> 16)
5075 secs
= strtol(p
, (char **)&p
, 0);
5076 if (secs
< 1 || secs
> 63)
5080 if (!strcmp(p
, "none"))
5081 translation
= BIOS_ATA_TRANSLATION_NONE
;
5082 else if (!strcmp(p
, "lba"))
5083 translation
= BIOS_ATA_TRANSLATION_LBA
;
5084 else if (!strcmp(p
, "auto"))
5085 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5088 } else if (*p
!= '\0') {
5090 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5093 if (hda_opts
!= NULL
) {
5095 snprintf(num
, sizeof(num
), "%d", cyls
);
5096 qemu_opt_set(hda_opts
, "cyls", num
);
5097 snprintf(num
, sizeof(num
), "%d", heads
);
5098 qemu_opt_set(hda_opts
, "heads", num
);
5099 snprintf(num
, sizeof(num
), "%d", secs
);
5100 qemu_opt_set(hda_opts
, "secs", num
);
5101 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5102 qemu_opt_set(hda_opts
, "trans", "lba");
5103 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5104 qemu_opt_set(hda_opts
, "trans", "none");
5108 case QEMU_OPTION_numa
:
5109 if (nb_numa_nodes
>= MAX_NODES
) {
5110 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5115 case QEMU_OPTION_nographic
:
5116 display_type
= DT_NOGRAPHIC
;
5118 #ifdef CONFIG_CURSES
5119 case QEMU_OPTION_curses
:
5120 display_type
= DT_CURSES
;
5123 case QEMU_OPTION_portrait
:
5126 case QEMU_OPTION_kernel
:
5127 kernel_filename
= optarg
;
5129 case QEMU_OPTION_append
:
5130 kernel_cmdline
= optarg
;
5132 case QEMU_OPTION_cdrom
:
5133 drive_add(optarg
, CDROM_ALIAS
);
5135 case QEMU_OPTION_boot
:
5137 static const char * const params
[] = {
5138 "order", "once", "menu", NULL
5140 char buf
[sizeof(boot_devices
)];
5141 char *standard_boot_devices
;
5144 if (!strchr(optarg
, '=')) {
5146 pstrcpy(buf
, sizeof(buf
), optarg
);
5147 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5149 "qemu: unknown boot parameter '%s' in '%s'\n",
5155 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5156 boot_devices_bitmap
= parse_bootdevices(buf
);
5157 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5160 if (get_param_value(buf
, sizeof(buf
),
5162 boot_devices_bitmap
|= parse_bootdevices(buf
);
5163 standard_boot_devices
= qemu_strdup(boot_devices
);
5164 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5165 qemu_register_reset(restore_boot_devices
,
5166 standard_boot_devices
);
5168 if (get_param_value(buf
, sizeof(buf
),
5170 if (!strcmp(buf
, "on")) {
5172 } else if (!strcmp(buf
, "off")) {
5176 "qemu: invalid option value '%s'\n",
5184 case QEMU_OPTION_fda
:
5185 case QEMU_OPTION_fdb
:
5186 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5189 case QEMU_OPTION_no_fd_bootchk
:
5193 case QEMU_OPTION_net
:
5194 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5195 fprintf(stderr
, "qemu: too many network clients\n");
5198 net_clients
[nb_net_clients
] = optarg
;
5202 case QEMU_OPTION_tftp
:
5203 legacy_tftp_prefix
= optarg
;
5205 case QEMU_OPTION_bootp
:
5206 legacy_bootp_filename
= optarg
;
5209 case QEMU_OPTION_smb
:
5210 net_slirp_smb(optarg
);
5213 case QEMU_OPTION_redir
:
5214 net_slirp_redir(optarg
);
5217 case QEMU_OPTION_bt
:
5218 add_device_config(DEV_BT
, optarg
);
5221 case QEMU_OPTION_audio_help
:
5225 case QEMU_OPTION_soundhw
:
5226 select_soundhw (optarg
);
5232 case QEMU_OPTION_version
:
5236 case QEMU_OPTION_m
: {
5240 value
= strtoul(optarg
, &ptr
, 10);
5242 case 0: case 'M': case 'm':
5249 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5253 /* On 32-bit hosts, QEMU is limited by virtual address space */
5254 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5255 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5258 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5259 fprintf(stderr
, "qemu: ram size too large\n");
5268 const CPULogItem
*item
;
5270 mask
= cpu_str_to_log_mask(optarg
);
5272 printf("Log items (comma separated):\n");
5273 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5274 printf("%-10s %s\n", item
->name
, item
->help
);
5282 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5284 case QEMU_OPTION_gdb
:
5285 gdbstub_dev
= optarg
;
5290 case QEMU_OPTION_bios
:
5293 case QEMU_OPTION_singlestep
:
5301 keyboard_layout
= optarg
;
5304 case QEMU_OPTION_localtime
:
5307 case QEMU_OPTION_vga
:
5308 select_vgahw (optarg
);
5310 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5316 w
= strtol(p
, (char **)&p
, 10);
5319 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5325 h
= strtol(p
, (char **)&p
, 10);
5330 depth
= strtol(p
, (char **)&p
, 10);
5331 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5332 depth
!= 24 && depth
!= 32)
5334 } else if (*p
== '\0') {
5335 depth
= graphic_depth
;
5342 graphic_depth
= depth
;
5346 case QEMU_OPTION_echr
:
5349 term_escape_char
= strtol(optarg
, &r
, 0);
5351 printf("Bad argument to echr\n");
5354 case QEMU_OPTION_monitor
:
5355 monitor_device
= optarg
;
5357 case QEMU_OPTION_serial
:
5358 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5359 fprintf(stderr
, "qemu: too many serial ports\n");
5362 serial_devices
[serial_device_index
] = optarg
;
5363 serial_device_index
++;
5365 case QEMU_OPTION_watchdog
:
5368 "qemu: only one watchdog option may be given\n");
5373 case QEMU_OPTION_watchdog_action
:
5374 if (select_watchdog_action(optarg
) == -1) {
5375 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5379 case QEMU_OPTION_virtiocon
:
5380 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5381 fprintf(stderr
, "qemu: too many virtio consoles\n");
5384 virtio_consoles
[virtio_console_index
] = optarg
;
5385 virtio_console_index
++;
5387 case QEMU_OPTION_parallel
:
5388 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5389 fprintf(stderr
, "qemu: too many parallel ports\n");
5392 parallel_devices
[parallel_device_index
] = optarg
;
5393 parallel_device_index
++;
5395 case QEMU_OPTION_loadvm
:
5398 case QEMU_OPTION_full_screen
:
5402 case QEMU_OPTION_no_frame
:
5405 case QEMU_OPTION_alt_grab
:
5408 case QEMU_OPTION_no_quit
:
5411 case QEMU_OPTION_sdl
:
5412 display_type
= DT_SDL
;
5415 case QEMU_OPTION_pidfile
:
5419 case QEMU_OPTION_win2k_hack
:
5420 win2k_install_hack
= 1;
5422 case QEMU_OPTION_rtc_td_hack
:
5425 case QEMU_OPTION_acpitable
:
5426 if(acpi_table_add(optarg
) < 0) {
5427 fprintf(stderr
, "Wrong acpi table provided\n");
5431 case QEMU_OPTION_smbios
:
5432 if(smbios_entry_add(optarg
) < 0) {
5433 fprintf(stderr
, "Wrong smbios provided\n");
5439 case QEMU_OPTION_enable_kvm
:
5443 case QEMU_OPTION_usb
:
5446 case QEMU_OPTION_usbdevice
:
5448 add_device_config(DEV_USB
, optarg
);
5450 case QEMU_OPTION_device
:
5451 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5453 fprintf(stderr
, "parse error: %s\n", optarg
);
5457 case QEMU_OPTION_smp
:
5460 fprintf(stderr
, "Invalid number of CPUs\n");
5463 if (max_cpus
< smp_cpus
) {
5464 fprintf(stderr
, "maxcpus must be equal to or greater than "
5468 if (max_cpus
> 255) {
5469 fprintf(stderr
, "Unsupported number of maxcpus\n");
5473 case QEMU_OPTION_vnc
:
5474 display_type
= DT_VNC
;
5475 vnc_display
= optarg
;
5478 case QEMU_OPTION_no_acpi
:
5481 case QEMU_OPTION_no_hpet
:
5484 case QEMU_OPTION_balloon
:
5485 if (balloon_parse(optarg
) < 0) {
5486 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5491 case QEMU_OPTION_no_reboot
:
5494 case QEMU_OPTION_no_shutdown
:
5497 case QEMU_OPTION_show_cursor
:
5500 case QEMU_OPTION_uuid
:
5501 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5502 fprintf(stderr
, "Fail to parse UUID string."
5503 " Wrong format.\n");
5508 case QEMU_OPTION_daemonize
:
5512 case QEMU_OPTION_option_rom
:
5513 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5514 fprintf(stderr
, "Too many option ROMs\n");
5517 option_rom
[nb_option_roms
] = optarg
;
5520 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5521 case QEMU_OPTION_semihosting
:
5522 semihosting_enabled
= 1;
5525 case QEMU_OPTION_name
:
5526 qemu_name
= qemu_strdup(optarg
);
5528 char *p
= strchr(qemu_name
, ',');
5531 if (strncmp(p
, "process=", 8)) {
5532 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5540 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5541 case QEMU_OPTION_prom_env
:
5542 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5543 fprintf(stderr
, "Too many prom variables\n");
5546 prom_envs
[nb_prom_envs
] = optarg
;
5551 case QEMU_OPTION_old_param
:
5555 case QEMU_OPTION_clock
:
5556 configure_alarms(optarg
);
5558 case QEMU_OPTION_startdate
:
5561 time_t rtc_start_date
;
5562 if (!strcmp(optarg
, "now")) {
5563 rtc_date_offset
= -1;
5565 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5573 } else if (sscanf(optarg
, "%d-%d-%d",
5576 &tm
.tm_mday
) == 3) {
5585 rtc_start_date
= mktimegm(&tm
);
5586 if (rtc_start_date
== -1) {
5588 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5589 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5592 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5596 case QEMU_OPTION_tb_size
:
5597 tb_size
= strtol(optarg
, NULL
, 0);
5601 case QEMU_OPTION_icount
:
5603 if (strcmp(optarg
, "auto") == 0) {
5604 icount_time_shift
= -1;
5606 icount_time_shift
= strtol(optarg
, NULL
, 0);
5609 case QEMU_OPTION_incoming
:
5613 case QEMU_OPTION_chroot
:
5614 chroot_dir
= optarg
;
5616 case QEMU_OPTION_runas
:
5621 case QEMU_OPTION_xen_domid
:
5622 xen_domid
= atoi(optarg
);
5624 case QEMU_OPTION_xen_create
:
5625 xen_mode
= XEN_CREATE
;
5627 case QEMU_OPTION_xen_attach
:
5628 xen_mode
= XEN_ATTACH
;
5635 /* If no data_dir is specified then try to find it relative to the
5638 data_dir
= find_datadir(argv
[0]);
5640 /* If all else fails use the install patch specified when building. */
5642 data_dir
= CONFIG_QEMU_SHAREDIR
;
5646 * Default to max_cpus = smp_cpus, in case the user doesn't
5647 * specify a max_cpus value.
5650 max_cpus
= smp_cpus
;
5652 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5653 if (smp_cpus
> machine
->max_cpus
) {
5654 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5655 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5660 if (display_type
== DT_NOGRAPHIC
) {
5661 if (serial_device_index
== 0)
5662 serial_devices
[0] = "stdio";
5663 if (parallel_device_index
== 0)
5664 parallel_devices
[0] = "null";
5665 if (strncmp(monitor_device
, "vc", 2) == 0)
5666 monitor_device
= "stdio";
5673 if (pipe(fds
) == -1)
5684 len
= read(fds
[0], &status
, 1);
5685 if (len
== -1 && (errno
== EINTR
))
5690 else if (status
== 1) {
5691 fprintf(stderr
, "Could not acquire pidfile\n");
5708 signal(SIGTSTP
, SIG_IGN
);
5709 signal(SIGTTOU
, SIG_IGN
);
5710 signal(SIGTTIN
, SIG_IGN
);
5713 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5716 write(fds
[1], &status
, 1);
5718 fprintf(stderr
, "Could not acquire pid file\n");
5723 if (qemu_init_main_loop()) {
5724 fprintf(stderr
, "qemu_init_main_loop failed\n");
5727 linux_boot
= (kernel_filename
!= NULL
);
5729 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5730 fprintf(stderr
, "-append only allowed with -kernel option\n");
5734 if (!linux_boot
&& initrd_filename
!= NULL
) {
5735 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5740 /* Win32 doesn't support line-buffering and requires size >= 2 */
5741 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5745 if (init_timer_alarm() < 0) {
5746 fprintf(stderr
, "could not initialize alarm timer\n");
5749 if (use_icount
&& icount_time_shift
< 0) {
5751 /* 125MIPS seems a reasonable initial guess at the guest speed.
5752 It will be corrected fairly quickly anyway. */
5753 icount_time_shift
= 3;
5754 init_icount_adjust();
5761 /* init network clients */
5762 if (nb_net_clients
== 0) {
5763 /* if no clients, we use a default config */
5764 net_clients
[nb_net_clients
++] = "nic";
5766 net_clients
[nb_net_clients
++] = "user";
5770 for(i
= 0;i
< nb_net_clients
; i
++) {
5771 if (net_client_parse(net_clients
[i
]) < 0)
5775 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5776 net_set_boot_mask(net_boot
);
5780 /* init the bluetooth world */
5781 if (foreach_device_config(DEV_BT
, bt_parse
))
5784 /* init the memory */
5786 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5788 /* init the dynamic translator */
5789 cpu_exec_init_all(tb_size
* 1024 * 1024);
5793 /* we always create the cdrom drive, even if no disk is there */
5794 drive_add(NULL
, CDROM_ALIAS
);
5796 /* we always create at least one floppy */
5797 drive_add(NULL
, FD_ALIAS
, 0);
5799 /* we always create one sd slot, even if no card is in it */
5800 drive_add(NULL
, SD_ALIAS
);
5802 /* open the virtual block devices */
5804 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5805 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5808 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5809 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5812 /* must be after terminal init, SDL library changes signal handlers */
5816 /* Maintain compatibility with multiple stdio monitors */
5817 if (!strcmp(monitor_device
,"stdio")) {
5818 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5819 const char *devname
= serial_devices
[i
];
5820 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5821 monitor_device
= NULL
;
5823 } else if (devname
&& !strcmp(devname
,"stdio")) {
5824 monitor_device
= NULL
;
5825 serial_devices
[i
] = "mon:stdio";
5831 if (nb_numa_nodes
> 0) {
5834 if (nb_numa_nodes
> smp_cpus
) {
5835 nb_numa_nodes
= smp_cpus
;
5838 /* If no memory size if given for any node, assume the default case
5839 * and distribute the available memory equally across all nodes
5841 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5842 if (node_mem
[i
] != 0)
5845 if (i
== nb_numa_nodes
) {
5846 uint64_t usedmem
= 0;
5848 /* On Linux, the each node's border has to be 8MB aligned,
5849 * the final node gets the rest.
5851 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5852 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5853 usedmem
+= node_mem
[i
];
5855 node_mem
[i
] = ram_size
- usedmem
;
5858 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5859 if (node_cpumask
[i
] != 0)
5862 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5863 * must cope with this anyway, because there are BIOSes out there in
5864 * real machines which also use this scheme.
5866 if (i
== nb_numa_nodes
) {
5867 for (i
= 0; i
< smp_cpus
; i
++) {
5868 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5873 if (kvm_enabled()) {
5876 ret
= kvm_init(smp_cpus
);
5878 fprintf(stderr
, "failed to initialize KVM\n");
5883 if (monitor_device
) {
5884 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5886 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5891 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5892 const char *devname
= serial_devices
[i
];
5893 if (devname
&& strcmp(devname
, "none")) {
5895 snprintf(label
, sizeof(label
), "serial%d", i
);
5896 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5897 if (!serial_hds
[i
]) {
5898 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5905 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5906 const char *devname
= parallel_devices
[i
];
5907 if (devname
&& strcmp(devname
, "none")) {
5909 snprintf(label
, sizeof(label
), "parallel%d", i
);
5910 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5911 if (!parallel_hds
[i
]) {
5912 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5919 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5920 const char *devname
= virtio_consoles
[i
];
5921 if (devname
&& strcmp(devname
, "none")) {
5923 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5924 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5925 if (!virtcon_hds
[i
]) {
5926 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5933 module_call_init(MODULE_INIT_DEVICE
);
5936 i
= select_watchdog(watchdog
);
5938 exit (i
== 1 ? 1 : 0);
5941 if (machine
->compat_props
) {
5942 qdev_prop_register_compat(machine
->compat_props
);
5944 machine
->init(ram_size
, boot_devices
,
5945 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5948 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5949 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5950 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5956 current_machine
= machine
;
5958 /* init USB devices */
5960 foreach_device_config(DEV_USB
, usb_parse
);
5963 /* init generic devices */
5964 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5968 dumb_display_init();
5969 /* just use the first displaystate for the moment */
5972 if (display_type
== DT_DEFAULT
) {
5973 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5974 display_type
= DT_SDL
;
5976 display_type
= DT_VNC
;
5977 vnc_display
= "localhost:0,to=99";
5983 switch (display_type
) {
5986 #if defined(CONFIG_CURSES)
5988 curses_display_init(ds
, full_screen
);
5991 #if defined(CONFIG_SDL)
5993 sdl_display_init(ds
, full_screen
, no_frame
);
5995 #elif defined(CONFIG_COCOA)
5997 cocoa_display_init(ds
, full_screen
);
6001 vnc_display_init(ds
);
6002 if (vnc_display_open(ds
, vnc_display
) < 0)
6005 if (show_vnc_port
) {
6006 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6014 dcl
= ds
->listeners
;
6015 while (dcl
!= NULL
) {
6016 if (dcl
->dpy_refresh
!= NULL
) {
6017 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6018 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6023 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6024 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6025 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6028 text_consoles_set_display(display_state
);
6029 qemu_chr_initial_reset();
6031 if (monitor_device
&& monitor_hd
)
6032 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6034 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6035 const char *devname
= serial_devices
[i
];
6036 if (devname
&& strcmp(devname
, "none")) {
6037 if (strstart(devname
, "vc", 0))
6038 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6042 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6043 const char *devname
= parallel_devices
[i
];
6044 if (devname
&& strcmp(devname
, "none")) {
6045 if (strstart(devname
, "vc", 0))
6046 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6050 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6051 const char *devname
= virtio_consoles
[i
];
6052 if (virtcon_hds
[i
] && devname
) {
6053 if (strstart(devname
, "vc", 0))
6054 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6058 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6059 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6065 if (load_vmstate(cur_mon
, loadvm
) < 0) {
6071 qemu_start_incoming_migration(incoming
);
6072 } else if (autostart
) {
6082 len
= write(fds
[1], &status
, 1);
6083 if (len
== -1 && (errno
== EINTR
))
6090 TFR(fd
= open("/dev/null", O_RDWR
));
6096 pwd
= getpwnam(run_as
);
6098 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6104 if (chroot(chroot_dir
) < 0) {
6105 fprintf(stderr
, "chroot failed\n");
6112 if (setgid(pwd
->pw_gid
) < 0) {
6113 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6116 if (setuid(pwd
->pw_uid
) < 0) {
6117 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6120 if (setuid(0) != -1) {
6121 fprintf(stderr
, "Dropping privileges failed\n");