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"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <linux/rtc.h>
63 #include <sys/prctl.h>
65 /* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67 /* #include <linux/hpet.h> */
70 #include <linux/ppdev.h>
71 #include <linux/parport.h>
75 #include <sys/ethernet.h>
76 #include <sys/sockio.h>
77 #include <netinet/arp.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> // must come after ip.h
82 #include <netinet/udp.h>
83 #include <netinet/tcp.h>
87 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89 extern int madvise(caddr_t
, size_t, int);
94 #if defined(__OpenBSD__)
98 #if defined(CONFIG_VDE)
99 #include <libvdeplug.h>
104 #include <mmsystem.h>
108 #if defined(__APPLE__) || defined(main)
110 int qemu_main(int argc
, char **argv
, char **envp
);
111 int main(int argc
, char **argv
)
113 return qemu_main(argc
, argv
, NULL
);
116 #define main qemu_main
118 #endif /* CONFIG_SDL */
122 #define main qemu_main
123 #endif /* CONFIG_COCOA */
126 #include "hw/boards.h"
128 #include "hw/pcmcia.h"
130 #include "hw/audiodev.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "block_int.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
157 #include "qemu-option.h"
158 #include "qemu-config.h"
162 #include "exec-all.h"
164 #include "qemu_socket.h"
166 #include "slirp/libslirp.h"
168 #include "qemu-queue.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 /* Maximum number of monitor devices */
176 #define MAX_MONITOR_DEVICES 10
178 static const char *data_dir
;
179 const char *bios_name
= NULL
;
180 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
181 to store the VM snapshots */
182 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
183 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
184 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
185 static DisplayState
*display_state
;
186 DisplayType display_type
= DT_DEFAULT
;
187 const char* keyboard_layout
= NULL
;
190 NICInfo nd_table
[MAX_NICS
];
193 static int rtc_utc
= 1;
194 static int rtc_date_offset
= -1; /* -1 means no change */
195 QEMUClock
*rtc_clock
;
196 int vga_interface_type
= VGA_CIRRUS
;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static int timer_alarm_pending
= 1;
259 /* Conversion factor from emulated instructions to virtual clock ticks. */
260 static int icount_time_shift
;
261 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
262 #define MAX_ICOUNT_SHIFT 10
263 /* Compensate for varying guest execution speed. */
264 static int64_t qemu_icount_bias
;
265 static QEMUTimer
*icount_rt_timer
;
266 static QEMUTimer
*icount_vm_timer
;
267 static QEMUTimer
*nographic_timer
;
269 uint8_t qemu_uuid
[16];
271 static QEMUBootSetHandler
*boot_set_handler
;
272 static void *boot_set_opaque
;
274 static int default_serial
= 1;
280 { .driver
= "isa-serial", .flag
= &default_serial
},
283 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
285 const char *driver
= qemu_opt_get(opts
, "driver");
290 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
291 if (strcmp(default_list
[i
].driver
, driver
) != 0)
293 *(default_list
[i
].flag
) = 0;
298 /***********************************************************/
299 /* x86 ISA bus support */
301 target_phys_addr_t isa_mem_base
= 0;
304 /***********************************************************/
305 void hw_error(const char *fmt
, ...)
311 fprintf(stderr
, "qemu: hardware error: ");
312 vfprintf(stderr
, fmt
, ap
);
313 fprintf(stderr
, "\n");
314 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
315 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
317 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
319 cpu_dump_state(env
, stderr
, fprintf
, 0);
326 static void set_proc_name(const char *s
)
328 #if defined(__linux__) && defined(PR_SET_NAME)
332 name
[sizeof(name
) - 1] = 0;
333 strncpy(name
, s
, sizeof(name
));
334 /* Could rewrite argv[0] too, but that's a bit more complicated.
335 This simple way is enough for `top'. */
336 prctl(PR_SET_NAME
, name
);
343 static QEMUBalloonEvent
*qemu_balloon_event
;
344 void *qemu_balloon_event_opaque
;
346 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
348 qemu_balloon_event
= func
;
349 qemu_balloon_event_opaque
= opaque
;
352 void qemu_balloon(ram_addr_t target
)
354 if (qemu_balloon_event
)
355 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
358 ram_addr_t
qemu_balloon_status(void)
360 if (qemu_balloon_event
)
361 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
365 /***********************************************************/
368 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
369 static void *qemu_put_kbd_event_opaque
;
370 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
371 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
373 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
375 qemu_put_kbd_event_opaque
= opaque
;
376 qemu_put_kbd_event
= func
;
379 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
380 void *opaque
, int absolute
,
383 QEMUPutMouseEntry
*s
, *cursor
;
385 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
387 s
->qemu_put_mouse_event
= func
;
388 s
->qemu_put_mouse_event_opaque
= opaque
;
389 s
->qemu_put_mouse_event_absolute
= absolute
;
390 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
393 if (!qemu_put_mouse_event_head
) {
394 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
398 cursor
= qemu_put_mouse_event_head
;
399 while (cursor
->next
!= NULL
)
400 cursor
= cursor
->next
;
403 qemu_put_mouse_event_current
= s
;
408 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
410 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
412 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
415 cursor
= qemu_put_mouse_event_head
;
416 while (cursor
!= NULL
&& cursor
!= entry
) {
418 cursor
= cursor
->next
;
421 if (cursor
== NULL
) // does not exist or list empty
423 else if (prev
== NULL
) { // entry is head
424 qemu_put_mouse_event_head
= cursor
->next
;
425 if (qemu_put_mouse_event_current
== entry
)
426 qemu_put_mouse_event_current
= cursor
->next
;
427 qemu_free(entry
->qemu_put_mouse_event_name
);
432 prev
->next
= entry
->next
;
434 if (qemu_put_mouse_event_current
== entry
)
435 qemu_put_mouse_event_current
= prev
;
437 qemu_free(entry
->qemu_put_mouse_event_name
);
441 void kbd_put_keycode(int keycode
)
443 if (qemu_put_kbd_event
) {
444 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
448 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
450 QEMUPutMouseEvent
*mouse_event
;
451 void *mouse_event_opaque
;
454 if (!qemu_put_mouse_event_current
) {
459 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
461 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
464 if (graphic_rotate
) {
465 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
468 width
= graphic_width
- 1;
469 mouse_event(mouse_event_opaque
,
470 width
- dy
, dx
, dz
, buttons_state
);
472 mouse_event(mouse_event_opaque
,
473 dx
, dy
, dz
, buttons_state
);
477 int kbd_mouse_is_absolute(void)
479 if (!qemu_put_mouse_event_current
)
482 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
485 void do_info_mice(Monitor
*mon
)
487 QEMUPutMouseEntry
*cursor
;
490 if (!qemu_put_mouse_event_head
) {
491 monitor_printf(mon
, "No mouse devices connected\n");
495 monitor_printf(mon
, "Mouse devices available:\n");
496 cursor
= qemu_put_mouse_event_head
;
497 while (cursor
!= NULL
) {
498 monitor_printf(mon
, "%c Mouse #%d: %s\n",
499 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
500 index
, cursor
->qemu_put_mouse_event_name
);
502 cursor
= cursor
->next
;
506 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
508 QEMUPutMouseEntry
*cursor
;
510 int index
= qdict_get_int(qdict
, "index");
512 if (!qemu_put_mouse_event_head
) {
513 monitor_printf(mon
, "No mouse devices connected\n");
517 cursor
= qemu_put_mouse_event_head
;
518 while (cursor
!= NULL
&& index
!= i
) {
520 cursor
= cursor
->next
;
524 qemu_put_mouse_event_current
= cursor
;
526 monitor_printf(mon
, "Mouse at given index not found\n");
529 /* compute with 96 bit intermediate result: (a*b)/c */
530 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
535 #ifdef HOST_WORDS_BIGENDIAN
545 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
546 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
549 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
553 /***********************************************************/
554 /* real time host monotonic timer */
556 static int64_t get_clock_realtime(void)
560 gettimeofday(&tv
, NULL
);
561 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
566 static int64_t clock_freq
;
568 static void init_get_clock(void)
572 ret
= QueryPerformanceFrequency(&freq
);
574 fprintf(stderr
, "Could not calibrate ticks\n");
577 clock_freq
= freq
.QuadPart
;
580 static int64_t get_clock(void)
583 QueryPerformanceCounter(&ti
);
584 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
589 static int use_rt_clock
;
591 static void init_get_clock(void)
594 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
595 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
598 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
605 static int64_t get_clock(void)
607 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
608 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
611 clock_gettime(CLOCK_MONOTONIC
, &ts
);
612 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
616 /* XXX: using gettimeofday leads to problems if the date
617 changes, so it should be avoided. */
618 return get_clock_realtime();
623 /* Return the virtual CPU time, based on the instruction counter. */
624 static int64_t cpu_get_icount(void)
627 CPUState
*env
= cpu_single_env
;;
628 icount
= qemu_icount
;
631 fprintf(stderr
, "Bad clock read\n");
632 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
634 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
637 /***********************************************************/
638 /* guest cycle counter */
640 typedef struct TimersState
{
641 int64_t cpu_ticks_prev
;
642 int64_t cpu_ticks_offset
;
643 int64_t cpu_clock_offset
;
644 int32_t cpu_ticks_enabled
;
648 TimersState timers_state
;
650 /* return the host CPU cycle counter and handle stop/restart */
651 int64_t cpu_get_ticks(void)
654 return cpu_get_icount();
656 if (!timers_state
.cpu_ticks_enabled
) {
657 return timers_state
.cpu_ticks_offset
;
660 ticks
= cpu_get_real_ticks();
661 if (timers_state
.cpu_ticks_prev
> ticks
) {
662 /* Note: non increasing ticks may happen if the host uses
664 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
666 timers_state
.cpu_ticks_prev
= ticks
;
667 return ticks
+ timers_state
.cpu_ticks_offset
;
671 /* return the host CPU monotonic timer and handle stop/restart */
672 static int64_t cpu_get_clock(void)
675 if (!timers_state
.cpu_ticks_enabled
) {
676 return timers_state
.cpu_clock_offset
;
679 return ti
+ timers_state
.cpu_clock_offset
;
683 /* enable cpu_get_ticks() */
684 void cpu_enable_ticks(void)
686 if (!timers_state
.cpu_ticks_enabled
) {
687 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
688 timers_state
.cpu_clock_offset
-= get_clock();
689 timers_state
.cpu_ticks_enabled
= 1;
693 /* disable cpu_get_ticks() : the clock is stopped. You must not call
694 cpu_get_ticks() after that. */
695 void cpu_disable_ticks(void)
697 if (timers_state
.cpu_ticks_enabled
) {
698 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
699 timers_state
.cpu_clock_offset
= cpu_get_clock();
700 timers_state
.cpu_ticks_enabled
= 0;
704 /***********************************************************/
707 #define QEMU_CLOCK_REALTIME 0
708 #define QEMU_CLOCK_VIRTUAL 1
709 #define QEMU_CLOCK_HOST 2
713 /* XXX: add frequency */
721 struct QEMUTimer
*next
;
724 struct qemu_alarm_timer
{
728 int (*start
)(struct qemu_alarm_timer
*t
);
729 void (*stop
)(struct qemu_alarm_timer
*t
);
730 void (*rearm
)(struct qemu_alarm_timer
*t
);
734 #define ALARM_FLAG_DYNTICKS 0x1
735 #define ALARM_FLAG_EXPIRED 0x2
737 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
739 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
742 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
744 if (!alarm_has_dynticks(t
))
750 /* TODO: MIN_TIMER_REARM_US should be optimized */
751 #define MIN_TIMER_REARM_US 250
753 static struct qemu_alarm_timer
*alarm_timer
;
757 struct qemu_alarm_win32
{
760 } alarm_win32_data
= {0, -1};
762 static int win32_start_timer(struct qemu_alarm_timer
*t
);
763 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
764 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
768 static int unix_start_timer(struct qemu_alarm_timer
*t
);
769 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
773 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
774 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
775 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
777 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
778 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
780 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
781 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
783 #endif /* __linux__ */
787 /* Correlation between real and virtual time is always going to be
788 fairly approximate, so ignore small variation.
789 When the guest is idle real and virtual time will be aligned in
791 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
793 static void icount_adjust(void)
798 static int64_t last_delta
;
799 /* If the VM is not running, then do nothing. */
803 cur_time
= cpu_get_clock();
804 cur_icount
= qemu_get_clock(vm_clock
);
805 delta
= cur_icount
- cur_time
;
806 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
808 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
809 && icount_time_shift
> 0) {
810 /* The guest is getting too far ahead. Slow time down. */
814 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
815 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
816 /* The guest is getting too far behind. Speed time up. */
820 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
823 static void icount_adjust_rt(void * opaque
)
825 qemu_mod_timer(icount_rt_timer
,
826 qemu_get_clock(rt_clock
) + 1000);
830 static void icount_adjust_vm(void * opaque
)
832 qemu_mod_timer(icount_vm_timer
,
833 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
837 static void init_icount_adjust(void)
839 /* Have both realtime and virtual time triggers for speed adjustment.
840 The realtime trigger catches emulated time passing too slowly,
841 the virtual time trigger catches emulated time passing too fast.
842 Realtime triggers occur even when idle, so use them less frequently
844 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
845 qemu_mod_timer(icount_rt_timer
,
846 qemu_get_clock(rt_clock
) + 1000);
847 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
848 qemu_mod_timer(icount_vm_timer
,
849 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
852 static struct qemu_alarm_timer alarm_timers
[] = {
855 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
856 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
857 /* HPET - if available - is preferred */
858 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
859 /* ...otherwise try RTC */
860 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
862 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
864 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
865 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
866 {"win32", 0, win32_start_timer
,
867 win32_stop_timer
, NULL
, &alarm_win32_data
},
872 static void show_available_alarms(void)
876 printf("Available alarm timers, in order of precedence:\n");
877 for (i
= 0; alarm_timers
[i
].name
; i
++)
878 printf("%s\n", alarm_timers
[i
].name
);
881 static void configure_alarms(char const *opt
)
885 int count
= ARRAY_SIZE(alarm_timers
) - 1;
888 struct qemu_alarm_timer tmp
;
890 if (!strcmp(opt
, "?")) {
891 show_available_alarms();
895 arg
= qemu_strdup(opt
);
897 /* Reorder the array */
898 name
= strtok(arg
, ",");
900 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
901 if (!strcmp(alarm_timers
[i
].name
, name
))
906 fprintf(stderr
, "Unknown clock %s\n", name
);
915 tmp
= alarm_timers
[i
];
916 alarm_timers
[i
] = alarm_timers
[cur
];
917 alarm_timers
[cur
] = tmp
;
921 name
= strtok(NULL
, ",");
927 /* Disable remaining timers */
928 for (i
= cur
; i
< count
; i
++)
929 alarm_timers
[i
].name
= NULL
;
931 show_available_alarms();
936 #define QEMU_NUM_CLOCKS 3
940 QEMUClock
*host_clock
;
942 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
944 static QEMUClock
*qemu_new_clock(int type
)
947 clock
= qemu_mallocz(sizeof(QEMUClock
));
952 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
956 ts
= qemu_mallocz(sizeof(QEMUTimer
));
963 void qemu_free_timer(QEMUTimer
*ts
)
968 /* stop a timer, but do not dealloc it */
969 void qemu_del_timer(QEMUTimer
*ts
)
973 /* NOTE: this code must be signal safe because
974 qemu_timer_expired() can be called from a signal. */
975 pt
= &active_timers
[ts
->clock
->type
];
988 /* modify the current timer so that it will be fired when current_time
989 >= expire_time. The corresponding callback will be called. */
990 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
996 /* add the timer in the sorted list */
997 /* NOTE: this code must be signal safe because
998 qemu_timer_expired() can be called from a signal. */
999 pt
= &active_timers
[ts
->clock
->type
];
1004 if (t
->expire_time
> expire_time
)
1008 ts
->expire_time
= expire_time
;
1012 /* Rearm if necessary */
1013 if (pt
== &active_timers
[ts
->clock
->type
]) {
1014 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1015 qemu_rearm_alarm_timer(alarm_timer
);
1017 /* Interrupt execution to force deadline recalculation. */
1019 qemu_notify_event();
1023 int qemu_timer_pending(QEMUTimer
*ts
)
1026 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1033 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1037 return (timer_head
->expire_time
<= current_time
);
1040 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1046 if (!ts
|| ts
->expire_time
> current_time
)
1048 /* remove timer from the list before calling the callback */
1049 *ptimer_head
= ts
->next
;
1052 /* run the callback (the timer list can be modified) */
1057 int64_t qemu_get_clock(QEMUClock
*clock
)
1059 switch(clock
->type
) {
1060 case QEMU_CLOCK_REALTIME
:
1061 return get_clock() / 1000000;
1063 case QEMU_CLOCK_VIRTUAL
:
1065 return cpu_get_icount();
1067 return cpu_get_clock();
1069 case QEMU_CLOCK_HOST
:
1070 return get_clock_realtime();
1074 static void init_clocks(void)
1077 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1078 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1079 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1081 rtc_clock
= host_clock
;
1085 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1087 uint64_t expire_time
;
1089 if (qemu_timer_pending(ts
)) {
1090 expire_time
= ts
->expire_time
;
1094 qemu_put_be64(f
, expire_time
);
1097 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1099 uint64_t expire_time
;
1101 expire_time
= qemu_get_be64(f
);
1102 if (expire_time
!= -1) {
1103 qemu_mod_timer(ts
, expire_time
);
1109 static const VMStateDescription vmstate_timers
= {
1112 .minimum_version_id
= 1,
1113 .minimum_version_id_old
= 1,
1114 .fields
= (VMStateField
[]) {
1115 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1116 VMSTATE_INT64(dummy
, TimersState
),
1117 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1118 VMSTATE_END_OF_LIST()
1122 static void qemu_event_increment(void);
1125 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1126 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1129 static void host_alarm_handler(int host_signum
)
1133 #define DISP_FREQ 1000
1135 static int64_t delta_min
= INT64_MAX
;
1136 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1138 ti
= qemu_get_clock(vm_clock
);
1139 if (last_clock
!= 0) {
1140 delta
= ti
- last_clock
;
1141 if (delta
< delta_min
)
1143 if (delta
> delta_max
)
1146 if (++count
== DISP_FREQ
) {
1147 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1148 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1149 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1150 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1151 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1153 delta_min
= INT64_MAX
;
1161 if (alarm_has_dynticks(alarm_timer
) ||
1163 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1164 qemu_get_clock(vm_clock
))) ||
1165 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1166 qemu_get_clock(rt_clock
)) ||
1167 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1168 qemu_get_clock(host_clock
))) {
1169 qemu_event_increment();
1170 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1172 #ifndef CONFIG_IOTHREAD
1174 /* stop the currently executing cpu because a timer occured */
1178 timer_alarm_pending
= 1;
1179 qemu_notify_event();
1183 static int64_t qemu_next_deadline(void)
1185 /* To avoid problems with overflow limit this to 2^32. */
1186 int64_t delta
= INT32_MAX
;
1188 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1189 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1190 qemu_get_clock(vm_clock
);
1192 if (active_timers
[QEMU_CLOCK_HOST
]) {
1193 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1194 qemu_get_clock(host_clock
);
1205 #if defined(__linux__)
1206 static uint64_t qemu_next_deadline_dyntick(void)
1214 delta
= (qemu_next_deadline() + 999) / 1000;
1216 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1217 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1218 qemu_get_clock(rt_clock
))*1000;
1219 if (rtdelta
< delta
)
1223 if (delta
< MIN_TIMER_REARM_US
)
1224 delta
= MIN_TIMER_REARM_US
;
1232 /* Sets a specific flag */
1233 static int fcntl_setfl(int fd
, int flag
)
1237 flags
= fcntl(fd
, F_GETFL
);
1241 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1247 #if defined(__linux__)
1249 #define RTC_FREQ 1024
1251 static void enable_sigio_timer(int fd
)
1253 struct sigaction act
;
1256 sigfillset(&act
.sa_mask
);
1258 act
.sa_handler
= host_alarm_handler
;
1260 sigaction(SIGIO
, &act
, NULL
);
1261 fcntl_setfl(fd
, O_ASYNC
);
1262 fcntl(fd
, F_SETOWN
, getpid());
1265 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1267 struct hpet_info info
;
1270 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1275 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1277 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1278 "error, but for better emulation accuracy type:\n"
1279 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1283 /* Check capabilities */
1284 r
= ioctl(fd
, HPET_INFO
, &info
);
1288 /* Enable periodic mode */
1289 r
= ioctl(fd
, HPET_EPI
, 0);
1290 if (info
.hi_flags
&& (r
< 0))
1293 /* Enable interrupt */
1294 r
= ioctl(fd
, HPET_IE_ON
, 0);
1298 enable_sigio_timer(fd
);
1299 t
->priv
= (void *)(long)fd
;
1307 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1309 int fd
= (long)t
->priv
;
1314 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1317 unsigned long current_rtc_freq
= 0;
1319 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1322 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1323 if (current_rtc_freq
!= RTC_FREQ
&&
1324 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1325 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1326 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1327 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1330 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1336 enable_sigio_timer(rtc_fd
);
1338 t
->priv
= (void *)(long)rtc_fd
;
1343 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1345 int rtc_fd
= (long)t
->priv
;
1350 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1354 struct sigaction act
;
1356 sigfillset(&act
.sa_mask
);
1358 act
.sa_handler
= host_alarm_handler
;
1360 sigaction(SIGALRM
, &act
, NULL
);
1363 * Initialize ev struct to 0 to avoid valgrind complaining
1364 * about uninitialized data in timer_create call
1366 memset(&ev
, 0, sizeof(ev
));
1367 ev
.sigev_value
.sival_int
= 0;
1368 ev
.sigev_notify
= SIGEV_SIGNAL
;
1369 ev
.sigev_signo
= SIGALRM
;
1371 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1372 perror("timer_create");
1374 /* disable dynticks */
1375 fprintf(stderr
, "Dynamic Ticks disabled\n");
1380 t
->priv
= (void *)(long)host_timer
;
1385 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1387 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1389 timer_delete(host_timer
);
1392 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1394 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1395 struct itimerspec timeout
;
1396 int64_t nearest_delta_us
= INT64_MAX
;
1399 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1400 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1401 !active_timers
[QEMU_CLOCK_HOST
])
1404 nearest_delta_us
= qemu_next_deadline_dyntick();
1406 /* check whether a timer is already running */
1407 if (timer_gettime(host_timer
, &timeout
)) {
1409 fprintf(stderr
, "Internal timer error: aborting\n");
1412 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1413 if (current_us
&& current_us
<= nearest_delta_us
)
1416 timeout
.it_interval
.tv_sec
= 0;
1417 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1418 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1419 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1420 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1422 fprintf(stderr
, "Internal timer error: aborting\n");
1427 #endif /* defined(__linux__) */
1429 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1431 struct sigaction act
;
1432 struct itimerval itv
;
1436 sigfillset(&act
.sa_mask
);
1438 act
.sa_handler
= host_alarm_handler
;
1440 sigaction(SIGALRM
, &act
, NULL
);
1442 itv
.it_interval
.tv_sec
= 0;
1443 /* for i386 kernel 2.6 to get 1 ms */
1444 itv
.it_interval
.tv_usec
= 999;
1445 itv
.it_value
.tv_sec
= 0;
1446 itv
.it_value
.tv_usec
= 10 * 1000;
1448 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1455 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1457 struct itimerval itv
;
1459 memset(&itv
, 0, sizeof(itv
));
1460 setitimer(ITIMER_REAL
, &itv
, NULL
);
1463 #endif /* !defined(_WIN32) */
1468 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1471 struct qemu_alarm_win32
*data
= t
->priv
;
1474 memset(&tc
, 0, sizeof(tc
));
1475 timeGetDevCaps(&tc
, sizeof(tc
));
1477 if (data
->period
< tc
.wPeriodMin
)
1478 data
->period
= tc
.wPeriodMin
;
1480 timeBeginPeriod(data
->period
);
1482 flags
= TIME_CALLBACK_FUNCTION
;
1483 if (alarm_has_dynticks(t
))
1484 flags
|= TIME_ONESHOT
;
1486 flags
|= TIME_PERIODIC
;
1488 data
->timerId
= timeSetEvent(1, // interval (ms)
1489 data
->period
, // resolution
1490 host_alarm_handler
, // function
1491 (DWORD
)t
, // parameter
1494 if (!data
->timerId
) {
1495 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1497 timeEndPeriod(data
->period
);
1504 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1506 struct qemu_alarm_win32
*data
= t
->priv
;
1508 timeKillEvent(data
->timerId
);
1509 timeEndPeriod(data
->period
);
1512 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1514 struct qemu_alarm_win32
*data
= t
->priv
;
1516 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1517 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1518 !active_timers
[QEMU_CLOCK_HOST
])
1521 timeKillEvent(data
->timerId
);
1523 data
->timerId
= timeSetEvent(1,
1527 TIME_ONESHOT
| TIME_PERIODIC
);
1529 if (!data
->timerId
) {
1530 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1533 timeEndPeriod(data
->period
);
1540 static int init_timer_alarm(void)
1542 struct qemu_alarm_timer
*t
= NULL
;
1545 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1546 t
= &alarm_timers
[i
];
1566 static void quit_timers(void)
1568 alarm_timer
->stop(alarm_timer
);
1572 /***********************************************************/
1573 /* host time/date access */
1574 void qemu_get_timedate(struct tm
*tm
, int offset
)
1581 if (rtc_date_offset
== -1) {
1585 ret
= localtime(&ti
);
1587 ti
-= rtc_date_offset
;
1591 memcpy(tm
, ret
, sizeof(struct tm
));
1594 int qemu_timedate_diff(struct tm
*tm
)
1598 if (rtc_date_offset
== -1)
1600 seconds
= mktimegm(tm
);
1602 seconds
= mktime(tm
);
1604 seconds
= mktimegm(tm
) + rtc_date_offset
;
1606 return seconds
- time(NULL
);
1609 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1611 time_t rtc_start_date
;
1614 if (!strcmp(startdate
, "now") && legacy
) {
1615 rtc_date_offset
= -1;
1617 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1625 } else if (sscanf(startdate
, "%d-%d-%d",
1628 &tm
.tm_mday
) == 3) {
1637 rtc_start_date
= mktimegm(&tm
);
1638 if (rtc_start_date
== -1) {
1640 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1641 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1644 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1648 static void configure_rtc(QemuOpts
*opts
)
1652 value
= qemu_opt_get(opts
, "base");
1654 if (!strcmp(value
, "utc")) {
1656 } else if (!strcmp(value
, "localtime")) {
1659 configure_rtc_date_offset(value
, 0);
1662 value
= qemu_opt_get(opts
, "clock");
1664 if (!strcmp(value
, "host")) {
1665 rtc_clock
= host_clock
;
1666 } else if (!strcmp(value
, "vm")) {
1667 rtc_clock
= vm_clock
;
1669 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1673 #ifdef CONFIG_TARGET_I386
1674 value
= qemu_opt_get(opts
, "driftfix");
1676 if (!strcmp(buf
, "slew")) {
1678 } else if (!strcmp(buf
, "none")) {
1681 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1689 static void socket_cleanup(void)
1694 static int socket_init(void)
1699 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1701 err
= WSAGetLastError();
1702 fprintf(stderr
, "WSAStartup: %d\n", err
);
1705 atexit(socket_cleanup
);
1710 /***********************************************************/
1711 /* Bluetooth support */
1714 static struct HCIInfo
*hci_table
[MAX_NICS
];
1716 static struct bt_vlan_s
{
1717 struct bt_scatternet_s net
;
1719 struct bt_vlan_s
*next
;
1722 /* find or alloc a new bluetooth "VLAN" */
1723 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1725 struct bt_vlan_s
**pvlan
, *vlan
;
1726 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1730 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1732 pvlan
= &first_bt_vlan
;
1733 while (*pvlan
!= NULL
)
1734 pvlan
= &(*pvlan
)->next
;
1739 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1743 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1748 static struct HCIInfo null_hci
= {
1749 .cmd_send
= null_hci_send
,
1750 .sco_send
= null_hci_send
,
1751 .acl_send
= null_hci_send
,
1752 .bdaddr_set
= null_hci_addr_set
,
1755 struct HCIInfo
*qemu_next_hci(void)
1757 if (cur_hci
== nb_hcis
)
1760 return hci_table
[cur_hci
++];
1763 static struct HCIInfo
*hci_init(const char *str
)
1766 struct bt_scatternet_s
*vlan
= 0;
1768 if (!strcmp(str
, "null"))
1771 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1773 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1774 else if (!strncmp(str
, "hci", 3)) {
1777 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1778 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1783 vlan
= qemu_find_bt_vlan(0);
1785 return bt_new_hci(vlan
);
1788 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1793 static int bt_hci_parse(const char *str
)
1795 struct HCIInfo
*hci
;
1798 if (nb_hcis
>= MAX_NICS
) {
1799 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1803 hci
= hci_init(str
);
1812 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1813 hci
->bdaddr_set(hci
, bdaddr
.b
);
1815 hci_table
[nb_hcis
++] = hci
;
1820 static void bt_vhci_add(int vlan_id
)
1822 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1825 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1826 "an empty scatternet %i\n", vlan_id
);
1828 bt_vhci_init(bt_new_hci(vlan
));
1831 static struct bt_device_s
*bt_device_add(const char *opt
)
1833 struct bt_scatternet_s
*vlan
;
1835 char *endp
= strstr(opt
, ",vlan=");
1836 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1839 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1842 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1844 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1849 vlan
= qemu_find_bt_vlan(vlan_id
);
1852 fprintf(stderr
, "qemu: warning: adding a slave device to "
1853 "an empty scatternet %i\n", vlan_id
);
1855 if (!strcmp(devname
, "keyboard"))
1856 return bt_keyboard_init(vlan
);
1858 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1862 static int bt_parse(const char *opt
)
1864 const char *endp
, *p
;
1867 if (strstart(opt
, "hci", &endp
)) {
1868 if (!*endp
|| *endp
== ',') {
1870 if (!strstart(endp
, ",vlan=", 0))
1873 return bt_hci_parse(opt
);
1875 } else if (strstart(opt
, "vhci", &endp
)) {
1876 if (!*endp
|| *endp
== ',') {
1878 if (strstart(endp
, ",vlan=", &p
)) {
1879 vlan
= strtol(p
, (char **) &endp
, 0);
1881 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1885 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1894 } else if (strstart(opt
, "device:", &endp
))
1895 return !bt_device_add(endp
);
1897 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1901 /***********************************************************/
1902 /* QEMU Block devices */
1904 #define HD_ALIAS "index=%d,media=disk"
1905 #define CDROM_ALIAS "index=2,media=cdrom"
1906 #define FD_ALIAS "index=%d,if=floppy"
1907 #define PFLASH_ALIAS "if=pflash"
1908 #define MTD_ALIAS "if=mtd"
1909 #define SD_ALIAS "index=0,if=sd"
1911 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1918 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1921 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1923 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1924 __FUNCTION__
, optstr
);
1928 qemu_opt_set(opts
, "file", file
);
1932 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1936 /* seek interface, bus and unit */
1938 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1939 if (dinfo
->type
== type
&&
1940 dinfo
->bus
== bus
&&
1941 dinfo
->unit
== unit
)
1948 DriveInfo
*drive_get_by_id(const char *id
)
1952 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1953 if (strcmp(id
, dinfo
->id
))
1960 int drive_get_max_bus(BlockInterfaceType type
)
1966 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1967 if(dinfo
->type
== type
&&
1968 dinfo
->bus
> max_bus
)
1969 max_bus
= dinfo
->bus
;
1974 const char *drive_get_serial(BlockDriverState
*bdrv
)
1978 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1979 if (dinfo
->bdrv
== bdrv
)
1980 return dinfo
->serial
;
1986 BlockInterfaceErrorAction
drive_get_on_error(
1987 BlockDriverState
*bdrv
, int is_read
)
1992 return BLOCK_ERR_REPORT
;
1995 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1996 if (dinfo
->bdrv
== bdrv
)
1997 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
2000 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
2003 static void bdrv_format_print(void *opaque
, const char *name
)
2005 fprintf(stderr
, " %s", name
);
2008 void drive_uninit(DriveInfo
*dinfo
)
2010 qemu_opts_del(dinfo
->opts
);
2011 bdrv_delete(dinfo
->bdrv
);
2012 QTAILQ_REMOVE(&drives
, dinfo
, next
);
2016 static int parse_block_error_action(const char *buf
, int is_read
)
2018 if (!strcmp(buf
, "ignore")) {
2019 return BLOCK_ERR_IGNORE
;
2020 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
2021 return BLOCK_ERR_STOP_ENOSPC
;
2022 } else if (!strcmp(buf
, "stop")) {
2023 return BLOCK_ERR_STOP_ANY
;
2024 } else if (!strcmp(buf
, "report")) {
2025 return BLOCK_ERR_REPORT
;
2027 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
2028 buf
, is_read
? "read" : "write");
2033 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
2037 const char *file
= NULL
;
2040 const char *mediastr
= "";
2041 BlockInterfaceType type
;
2042 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2043 int bus_id
, unit_id
;
2044 int cyls
, heads
, secs
, translation
;
2045 BlockDriver
*drv
= NULL
;
2046 QEMUMachine
*machine
= opaque
;
2053 int on_read_error
, on_write_error
;
2054 const char *devaddr
;
2060 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2063 if (machine
&& machine
->use_scsi
) {
2065 max_devs
= MAX_SCSI_DEVS
;
2066 pstrcpy(devname
, sizeof(devname
), "scsi");
2069 max_devs
= MAX_IDE_DEVS
;
2070 pstrcpy(devname
, sizeof(devname
), "ide");
2074 /* extract parameters */
2075 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2076 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2077 index
= qemu_opt_get_number(opts
, "index", -1);
2079 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2080 heads
= qemu_opt_get_number(opts
, "heads", 0);
2081 secs
= qemu_opt_get_number(opts
, "secs", 0);
2083 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2084 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2086 file
= qemu_opt_get(opts
, "file");
2087 serial
= qemu_opt_get(opts
, "serial");
2089 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2090 pstrcpy(devname
, sizeof(devname
), buf
);
2091 if (!strcmp(buf
, "ide")) {
2093 max_devs
= MAX_IDE_DEVS
;
2094 } else if (!strcmp(buf
, "scsi")) {
2096 max_devs
= MAX_SCSI_DEVS
;
2097 } else if (!strcmp(buf
, "floppy")) {
2100 } else if (!strcmp(buf
, "pflash")) {
2103 } else if (!strcmp(buf
, "mtd")) {
2106 } else if (!strcmp(buf
, "sd")) {
2109 } else if (!strcmp(buf
, "virtio")) {
2112 } else if (!strcmp(buf
, "xen")) {
2115 } else if (!strcmp(buf
, "none")) {
2119 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2124 if (cyls
|| heads
|| secs
) {
2125 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2126 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2129 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2130 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2133 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2134 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2139 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2142 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2146 if (!strcmp(buf
, "none"))
2147 translation
= BIOS_ATA_TRANSLATION_NONE
;
2148 else if (!strcmp(buf
, "lba"))
2149 translation
= BIOS_ATA_TRANSLATION_LBA
;
2150 else if (!strcmp(buf
, "auto"))
2151 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2153 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2158 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2159 if (!strcmp(buf
, "disk")) {
2161 } else if (!strcmp(buf
, "cdrom")) {
2162 if (cyls
|| secs
|| heads
) {
2164 "qemu: '%s' invalid physical CHS format\n", buf
);
2167 media
= MEDIA_CDROM
;
2169 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2174 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2175 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2177 else if (!strcmp(buf
, "writethrough"))
2179 else if (!strcmp(buf
, "writeback"))
2182 fprintf(stderr
, "qemu: invalid cache option\n");
2187 #ifdef CONFIG_LINUX_AIO
2188 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2189 if (!strcmp(buf
, "threads"))
2191 else if (!strcmp(buf
, "native"))
2194 fprintf(stderr
, "qemu: invalid aio option\n");
2200 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2201 if (strcmp(buf
, "?") == 0) {
2202 fprintf(stderr
, "qemu: Supported formats:");
2203 bdrv_iterate_format(bdrv_format_print
, NULL
);
2204 fprintf(stderr
, "\n");
2207 drv
= bdrv_find_whitelisted_format(buf
);
2209 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2214 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2215 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2216 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2217 fprintf(stderr
, "werror is no supported by this format\n");
2221 on_write_error
= parse_block_error_action(buf
, 0);
2222 if (on_write_error
< 0) {
2227 on_read_error
= BLOCK_ERR_REPORT
;
2228 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2229 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2230 fprintf(stderr
, "rerror is no supported by this format\n");
2234 on_read_error
= parse_block_error_action(buf
, 1);
2235 if (on_read_error
< 0) {
2240 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2241 if (type
!= IF_VIRTIO
) {
2242 fprintf(stderr
, "addr is not supported\n");
2247 /* compute bus and unit according index */
2250 if (bus_id
!= 0 || unit_id
!= -1) {
2252 "qemu: index cannot be used with bus and unit\n");
2260 unit_id
= index
% max_devs
;
2261 bus_id
= index
/ max_devs
;
2265 /* if user doesn't specify a unit_id,
2266 * try to find the first free
2269 if (unit_id
== -1) {
2271 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2273 if (max_devs
&& unit_id
>= max_devs
) {
2274 unit_id
-= max_devs
;
2282 if (max_devs
&& unit_id
>= max_devs
) {
2283 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2284 unit_id
, max_devs
- 1);
2289 * ignore multiple definitions
2292 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2299 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2300 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2301 dinfo
->id
= qemu_strdup(buf
);
2303 /* no id supplied -> create one */
2304 dinfo
->id
= qemu_mallocz(32);
2305 if (type
== IF_IDE
|| type
== IF_SCSI
)
2306 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2308 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2309 devname
, bus_id
, mediastr
, unit_id
);
2311 snprintf(dinfo
->id
, 32, "%s%s%i",
2312 devname
, mediastr
, unit_id
);
2314 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2315 dinfo
->devaddr
= devaddr
;
2317 dinfo
->bus
= bus_id
;
2318 dinfo
->unit
= unit_id
;
2319 dinfo
->on_read_error
= on_read_error
;
2320 dinfo
->on_write_error
= on_write_error
;
2323 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2324 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2334 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2335 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2339 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2344 /* FIXME: This isn't really a floppy, but it's a reasonable
2347 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2353 /* add virtio block device */
2354 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2355 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2356 qemu_opt_set(opts
, "drive", dinfo
->id
);
2358 qemu_opt_set(opts
, "addr", devaddr
);
2369 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2370 cache
= 2; /* always use write-back with snapshot */
2372 if (cache
== 0) /* no caching */
2373 bdrv_flags
|= BDRV_O_NOCACHE
;
2374 else if (cache
== 2) /* write-back */
2375 bdrv_flags
|= BDRV_O_CACHE_WB
;
2378 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2380 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2384 if (type
== IF_IDE
) {
2385 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2388 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2391 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2392 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2393 file
, strerror(errno
));
2397 if (bdrv_key_required(dinfo
->bdrv
))
2403 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2405 QEMUMachine
*machine
= opaque
;
2406 int fatal_error
= 0;
2408 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2415 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2417 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2418 qemu_opt_set(opts
, "snapshot", "on");
2423 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2425 boot_set_handler
= func
;
2426 boot_set_opaque
= opaque
;
2429 int qemu_boot_set(const char *boot_devices
)
2431 if (!boot_set_handler
) {
2434 return boot_set_handler(boot_set_opaque
, boot_devices
);
2437 static int parse_bootdevices(char *devices
)
2439 /* We just do some generic consistency checks */
2443 for (p
= devices
; *p
!= '\0'; p
++) {
2444 /* Allowed boot devices are:
2445 * a-b: floppy disk drives
2446 * c-f: IDE disk drives
2447 * g-m: machine implementation dependant drives
2448 * n-p: network devices
2449 * It's up to each machine implementation to check if the given boot
2450 * devices match the actual hardware implementation and firmware
2453 if (*p
< 'a' || *p
> 'p') {
2454 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2457 if (bitmap
& (1 << (*p
- 'a'))) {
2458 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2461 bitmap
|= 1 << (*p
- 'a');
2466 static void restore_boot_devices(void *opaque
)
2468 char *standard_boot_devices
= opaque
;
2470 qemu_boot_set(standard_boot_devices
);
2472 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2473 qemu_free(standard_boot_devices
);
2476 static void numa_add(const char *optarg
)
2480 unsigned long long value
, endvalue
;
2483 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2484 if (!strcmp(option
, "node")) {
2485 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2486 nodenr
= nb_numa_nodes
;
2488 nodenr
= strtoull(option
, NULL
, 10);
2491 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2492 node_mem
[nodenr
] = 0;
2494 value
= strtoull(option
, &endptr
, 0);
2496 case 0: case 'M': case 'm':
2503 node_mem
[nodenr
] = value
;
2505 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2506 node_cpumask
[nodenr
] = 0;
2508 value
= strtoull(option
, &endptr
, 10);
2511 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2513 if (*endptr
== '-') {
2514 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2515 if (endvalue
>= 63) {
2518 "only 63 CPUs in NUMA mode supported.\n");
2520 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2525 node_cpumask
[nodenr
] = value
;
2532 static void smp_parse(const char *optarg
)
2534 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2538 smp
= strtoul(optarg
, &endptr
, 10);
2539 if (endptr
!= optarg
) {
2540 if (*endptr
== ',') {
2544 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2545 sockets
= strtoull(option
, NULL
, 10);
2546 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2547 cores
= strtoull(option
, NULL
, 10);
2548 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2549 threads
= strtoull(option
, NULL
, 10);
2550 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2551 max_cpus
= strtoull(option
, NULL
, 10);
2553 /* compute missing values, prefer sockets over cores over threads */
2554 if (smp
== 0 || sockets
== 0) {
2555 sockets
= sockets
> 0 ? sockets
: 1;
2556 cores
= cores
> 0 ? cores
: 1;
2557 threads
= threads
> 0 ? threads
: 1;
2559 smp
= cores
* threads
* sockets
;
2561 sockets
= smp
/ (cores
* threads
);
2565 threads
= threads
> 0 ? threads
: 1;
2566 cores
= smp
/ (sockets
* threads
);
2569 sockets
= smp
/ (cores
* threads
);
2571 threads
= smp
/ (cores
* sockets
);
2576 smp_cores
= cores
> 0 ? cores
: 1;
2577 smp_threads
= threads
> 0 ? threads
: 1;
2579 max_cpus
= smp_cpus
;
2582 /***********************************************************/
2585 static int usb_device_add(const char *devname
, int is_hotplug
)
2588 USBDevice
*dev
= NULL
;
2593 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2594 dev
= usbdevice_create(devname
);
2598 /* the other ones */
2599 if (strstart(devname
, "host:", &p
)) {
2600 dev
= usb_host_device_open(p
);
2601 } else if (strstart(devname
, "net:", &p
)) {
2605 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2610 qemu_opt_set(opts
, "type", "nic");
2611 qemu_opt_set(opts
, "model", "usb");
2613 idx
= net_client_init(NULL
, opts
, 0);
2618 dev
= usb_net_init(&nd_table
[idx
]);
2619 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2620 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2621 bt_new_hci(qemu_find_bt_vlan(0)));
2632 static int usb_device_del(const char *devname
)
2637 if (strstart(devname
, "host:", &p
))
2638 return usb_host_device_close(p
);
2643 p
= strchr(devname
, '.');
2646 bus_num
= strtoul(devname
, NULL
, 0);
2647 addr
= strtoul(p
+ 1, NULL
, 0);
2649 return usb_device_delete_addr(bus_num
, addr
);
2652 static int usb_parse(const char *cmdline
)
2654 return usb_device_add(cmdline
, 0);
2657 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2659 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2662 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2664 usb_device_del(qdict_get_str(qdict
, "devname"));
2667 /***********************************************************/
2668 /* PCMCIA/Cardbus */
2670 static struct pcmcia_socket_entry_s
{
2671 PCMCIASocket
*socket
;
2672 struct pcmcia_socket_entry_s
*next
;
2673 } *pcmcia_sockets
= 0;
2675 void pcmcia_socket_register(PCMCIASocket
*socket
)
2677 struct pcmcia_socket_entry_s
*entry
;
2679 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2680 entry
->socket
= socket
;
2681 entry
->next
= pcmcia_sockets
;
2682 pcmcia_sockets
= entry
;
2685 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2687 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2689 ptr
= &pcmcia_sockets
;
2690 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2691 if (entry
->socket
== socket
) {
2697 void pcmcia_info(Monitor
*mon
)
2699 struct pcmcia_socket_entry_s
*iter
;
2701 if (!pcmcia_sockets
)
2702 monitor_printf(mon
, "No PCMCIA sockets\n");
2704 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2705 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2706 iter
->socket
->attached
? iter
->socket
->card_string
:
2710 /***********************************************************/
2711 /* register display */
2713 struct DisplayAllocator default_allocator
= {
2714 defaultallocator_create_displaysurface
,
2715 defaultallocator_resize_displaysurface
,
2716 defaultallocator_free_displaysurface
2719 void register_displaystate(DisplayState
*ds
)
2729 DisplayState
*get_displaystate(void)
2731 return display_state
;
2734 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2736 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2737 return ds
->allocator
;
2742 static void dumb_display_init(void)
2744 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2745 ds
->allocator
= &default_allocator
;
2746 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2747 register_displaystate(ds
);
2750 /***********************************************************/
2753 typedef struct IOHandlerRecord
{
2755 IOCanRWHandler
*fd_read_poll
;
2757 IOHandler
*fd_write
;
2760 /* temporary data */
2762 struct IOHandlerRecord
*next
;
2765 static IOHandlerRecord
*first_io_handler
;
2767 /* XXX: fd_read_poll should be suppressed, but an API change is
2768 necessary in the character devices to suppress fd_can_read(). */
2769 int qemu_set_fd_handler2(int fd
,
2770 IOCanRWHandler
*fd_read_poll
,
2772 IOHandler
*fd_write
,
2775 IOHandlerRecord
**pioh
, *ioh
;
2777 if (!fd_read
&& !fd_write
) {
2778 pioh
= &first_io_handler
;
2783 if (ioh
->fd
== fd
) {
2790 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2794 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2795 ioh
->next
= first_io_handler
;
2796 first_io_handler
= ioh
;
2799 ioh
->fd_read_poll
= fd_read_poll
;
2800 ioh
->fd_read
= fd_read
;
2801 ioh
->fd_write
= fd_write
;
2802 ioh
->opaque
= opaque
;
2808 int qemu_set_fd_handler(int fd
,
2810 IOHandler
*fd_write
,
2813 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2817 /***********************************************************/
2818 /* Polling handling */
2820 typedef struct PollingEntry
{
2823 struct PollingEntry
*next
;
2826 static PollingEntry
*first_polling_entry
;
2828 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2830 PollingEntry
**ppe
, *pe
;
2831 pe
= qemu_mallocz(sizeof(PollingEntry
));
2833 pe
->opaque
= opaque
;
2834 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2839 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2841 PollingEntry
**ppe
, *pe
;
2842 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2844 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2852 /***********************************************************/
2853 /* Wait objects support */
2854 typedef struct WaitObjects
{
2856 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2857 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2858 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2861 static WaitObjects wait_objects
= {0};
2863 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2865 WaitObjects
*w
= &wait_objects
;
2867 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2869 w
->events
[w
->num
] = handle
;
2870 w
->func
[w
->num
] = func
;
2871 w
->opaque
[w
->num
] = opaque
;
2876 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2879 WaitObjects
*w
= &wait_objects
;
2882 for (i
= 0; i
< w
->num
; i
++) {
2883 if (w
->events
[i
] == handle
)
2886 w
->events
[i
] = w
->events
[i
+ 1];
2887 w
->func
[i
] = w
->func
[i
+ 1];
2888 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2896 /***********************************************************/
2897 /* ram save/restore */
2899 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2900 #define RAM_SAVE_FLAG_COMPRESS 0x02
2901 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2902 #define RAM_SAVE_FLAG_PAGE 0x08
2903 #define RAM_SAVE_FLAG_EOS 0x10
2905 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2907 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2908 uint32_t *array
= (uint32_t *)page
;
2911 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2912 if (array
[i
] != val
)
2919 static int ram_save_block(QEMUFile
*f
)
2921 static ram_addr_t current_addr
= 0;
2922 ram_addr_t saved_addr
= current_addr
;
2923 ram_addr_t addr
= 0;
2926 while (addr
< last_ram_offset
) {
2927 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2930 cpu_physical_memory_reset_dirty(current_addr
,
2931 current_addr
+ TARGET_PAGE_SIZE
,
2932 MIGRATION_DIRTY_FLAG
);
2934 p
= qemu_get_ram_ptr(current_addr
);
2936 if (is_dup_page(p
, *p
)) {
2937 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2938 qemu_put_byte(f
, *p
);
2940 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2941 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2947 addr
+= TARGET_PAGE_SIZE
;
2948 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2954 static uint64_t bytes_transferred
;
2956 static ram_addr_t
ram_save_remaining(void)
2959 ram_addr_t count
= 0;
2961 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2962 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2969 uint64_t ram_bytes_remaining(void)
2971 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2974 uint64_t ram_bytes_transferred(void)
2976 return bytes_transferred
;
2979 uint64_t ram_bytes_total(void)
2981 return last_ram_offset
;
2984 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2987 uint64_t bytes_transferred_last
;
2989 uint64_t expected_time
= 0;
2992 cpu_physical_memory_set_dirty_tracking(0);
2996 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2997 qemu_file_set_error(f
);
3002 bytes_transferred
= 0;
3004 /* Make sure all dirty bits are set */
3005 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3006 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3007 cpu_physical_memory_set_dirty(addr
);
3010 /* Enable dirty memory tracking */
3011 cpu_physical_memory_set_dirty_tracking(1);
3013 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3016 bytes_transferred_last
= bytes_transferred
;
3017 bwidth
= get_clock();
3019 while (!qemu_file_rate_limit(f
)) {
3022 ret
= ram_save_block(f
);
3023 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3024 if (ret
== 0) /* no more blocks */
3028 bwidth
= get_clock() - bwidth
;
3029 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3031 /* if we haven't transferred anything this round, force expected_time to a
3032 * a very high value, but without crashing */
3036 /* try transferring iterative blocks of memory */
3038 /* flush all remaining blocks regardless of rate limiting */
3039 while (ram_save_block(f
) != 0) {
3040 bytes_transferred
+= TARGET_PAGE_SIZE
;
3042 cpu_physical_memory_set_dirty_tracking(0);
3045 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3047 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3049 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3052 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3057 if (version_id
!= 3)
3061 addr
= qemu_get_be64(f
);
3063 flags
= addr
& ~TARGET_PAGE_MASK
;
3064 addr
&= TARGET_PAGE_MASK
;
3066 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3067 if (addr
!= last_ram_offset
)
3071 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3072 uint8_t ch
= qemu_get_byte(f
);
3073 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3076 (!kvm_enabled() || kvm_has_sync_mmu())) {
3077 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3080 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
3081 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3083 if (qemu_file_has_error(f
)) {
3086 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3091 void qemu_service_io(void)
3093 qemu_notify_event();
3096 /***********************************************************/
3097 /* machine registration */
3099 static QEMUMachine
*first_machine
= NULL
;
3100 QEMUMachine
*current_machine
= NULL
;
3102 int qemu_register_machine(QEMUMachine
*m
)
3105 pm
= &first_machine
;
3113 static QEMUMachine
*find_machine(const char *name
)
3117 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3118 if (!strcmp(m
->name
, name
))
3120 if (m
->alias
&& !strcmp(m
->alias
, name
))
3126 static QEMUMachine
*find_default_machine(void)
3130 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3131 if (m
->is_default
) {
3138 /***********************************************************/
3139 /* main execution loop */
3141 static void gui_update(void *opaque
)
3143 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3144 DisplayState
*ds
= opaque
;
3145 DisplayChangeListener
*dcl
= ds
->listeners
;
3149 while (dcl
!= NULL
) {
3150 if (dcl
->gui_timer_interval
&&
3151 dcl
->gui_timer_interval
< interval
)
3152 interval
= dcl
->gui_timer_interval
;
3155 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3158 static void nographic_update(void *opaque
)
3160 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3162 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3165 struct vm_change_state_entry
{
3166 VMChangeStateHandler
*cb
;
3168 QLIST_ENTRY (vm_change_state_entry
) entries
;
3171 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3173 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3176 VMChangeStateEntry
*e
;
3178 e
= qemu_mallocz(sizeof (*e
));
3182 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3186 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3188 QLIST_REMOVE (e
, entries
);
3192 static void vm_state_notify(int running
, int reason
)
3194 VMChangeStateEntry
*e
;
3196 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3197 e
->cb(e
->opaque
, running
, reason
);
3201 static void resume_all_vcpus(void);
3202 static void pause_all_vcpus(void);
3209 vm_state_notify(1, 0);
3210 qemu_rearm_alarm_timer(alarm_timer
);
3215 /* reset/shutdown handler */
3217 typedef struct QEMUResetEntry
{
3218 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3219 QEMUResetHandler
*func
;
3223 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3224 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3225 static int reset_requested
;
3226 static int shutdown_requested
;
3227 static int powerdown_requested
;
3228 static int debug_requested
;
3229 static int vmstop_requested
;
3231 int qemu_shutdown_requested(void)
3233 int r
= shutdown_requested
;
3234 shutdown_requested
= 0;
3238 int qemu_reset_requested(void)
3240 int r
= reset_requested
;
3241 reset_requested
= 0;
3245 int qemu_powerdown_requested(void)
3247 int r
= powerdown_requested
;
3248 powerdown_requested
= 0;
3252 static int qemu_debug_requested(void)
3254 int r
= debug_requested
;
3255 debug_requested
= 0;
3259 static int qemu_vmstop_requested(void)
3261 int r
= vmstop_requested
;
3262 vmstop_requested
= 0;
3266 static void do_vm_stop(int reason
)
3269 cpu_disable_ticks();
3272 vm_state_notify(0, reason
);
3276 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3278 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3281 re
->opaque
= opaque
;
3282 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3285 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3289 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3290 if (re
->func
== func
&& re
->opaque
== opaque
) {
3291 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3298 void qemu_system_reset(void)
3300 QEMUResetEntry
*re
, *nre
;
3302 /* reset all devices */
3303 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3304 re
->func(re
->opaque
);
3308 void qemu_system_reset_request(void)
3311 shutdown_requested
= 1;
3313 reset_requested
= 1;
3315 qemu_notify_event();
3318 void qemu_system_shutdown_request(void)
3320 shutdown_requested
= 1;
3321 qemu_notify_event();
3324 void qemu_system_powerdown_request(void)
3326 powerdown_requested
= 1;
3327 qemu_notify_event();
3330 #ifdef CONFIG_IOTHREAD
3331 static void qemu_system_vmstop_request(int reason
)
3333 vmstop_requested
= reason
;
3334 qemu_notify_event();
3339 static int io_thread_fd
= -1;
3341 static void qemu_event_increment(void)
3343 static const char byte
= 0;
3345 if (io_thread_fd
== -1)
3348 write(io_thread_fd
, &byte
, sizeof(byte
));
3351 static void qemu_event_read(void *opaque
)
3353 int fd
= (unsigned long)opaque
;
3356 /* Drain the notify pipe */
3359 len
= read(fd
, buffer
, sizeof(buffer
));
3360 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3363 static int qemu_event_init(void)
3368 err
= qemu_pipe(fds
);
3372 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3376 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3380 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3381 (void *)(unsigned long)fds
[0]);
3383 io_thread_fd
= fds
[1];
3392 HANDLE qemu_event_handle
;
3394 static void dummy_event_handler(void *opaque
)
3398 static int qemu_event_init(void)
3400 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3401 if (!qemu_event_handle
) {
3402 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3405 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3409 static void qemu_event_increment(void)
3411 if (!SetEvent(qemu_event_handle
)) {
3412 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3419 static int cpu_can_run(CPUState
*env
)
3428 #ifndef CONFIG_IOTHREAD
3429 static int qemu_init_main_loop(void)
3431 return qemu_event_init();
3434 void qemu_init_vcpu(void *_env
)
3436 CPUState
*env
= _env
;
3440 env
->nr_cores
= smp_cores
;
3441 env
->nr_threads
= smp_threads
;
3445 int qemu_cpu_self(void *env
)
3450 static void resume_all_vcpus(void)
3454 static void pause_all_vcpus(void)
3458 void qemu_cpu_kick(void *env
)
3463 void qemu_notify_event(void)
3465 CPUState
*env
= cpu_single_env
;
3472 void qemu_mutex_lock_iothread(void) {}
3473 void qemu_mutex_unlock_iothread(void) {}
3475 void vm_stop(int reason
)
3480 #else /* CONFIG_IOTHREAD */
3482 #include "qemu-thread.h"
3484 QemuMutex qemu_global_mutex
;
3485 static QemuMutex qemu_fair_mutex
;
3487 static QemuThread io_thread
;
3489 static QemuThread
*tcg_cpu_thread
;
3490 static QemuCond
*tcg_halt_cond
;
3492 static int qemu_system_ready
;
3494 static QemuCond qemu_cpu_cond
;
3496 static QemuCond qemu_system_cond
;
3497 static QemuCond qemu_pause_cond
;
3499 static void block_io_signals(void);
3500 static void unblock_io_signals(void);
3501 static int tcg_has_work(void);
3503 static int qemu_init_main_loop(void)
3507 ret
= qemu_event_init();
3511 qemu_cond_init(&qemu_pause_cond
);
3512 qemu_mutex_init(&qemu_fair_mutex
);
3513 qemu_mutex_init(&qemu_global_mutex
);
3514 qemu_mutex_lock(&qemu_global_mutex
);
3516 unblock_io_signals();
3517 qemu_thread_self(&io_thread
);
3522 static void qemu_wait_io_event(CPUState
*env
)
3524 while (!tcg_has_work())
3525 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3527 qemu_mutex_unlock(&qemu_global_mutex
);
3530 * Users of qemu_global_mutex can be starved, having no chance
3531 * to acquire it since this path will get to it first.
3532 * So use another lock to provide fairness.
3534 qemu_mutex_lock(&qemu_fair_mutex
);
3535 qemu_mutex_unlock(&qemu_fair_mutex
);
3537 qemu_mutex_lock(&qemu_global_mutex
);
3541 qemu_cond_signal(&qemu_pause_cond
);
3545 static int qemu_cpu_exec(CPUState
*env
);
3547 static void *kvm_cpu_thread_fn(void *arg
)
3549 CPUState
*env
= arg
;
3552 qemu_thread_self(env
->thread
);
3556 /* signal CPU creation */
3557 qemu_mutex_lock(&qemu_global_mutex
);
3559 qemu_cond_signal(&qemu_cpu_cond
);
3561 /* and wait for machine initialization */
3562 while (!qemu_system_ready
)
3563 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3566 if (cpu_can_run(env
))
3568 qemu_wait_io_event(env
);
3574 static void tcg_cpu_exec(void);
3576 static void *tcg_cpu_thread_fn(void *arg
)
3578 CPUState
*env
= arg
;
3581 qemu_thread_self(env
->thread
);
3583 /* signal CPU creation */
3584 qemu_mutex_lock(&qemu_global_mutex
);
3585 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3587 qemu_cond_signal(&qemu_cpu_cond
);
3589 /* and wait for machine initialization */
3590 while (!qemu_system_ready
)
3591 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3595 qemu_wait_io_event(cur_cpu
);
3601 void qemu_cpu_kick(void *_env
)
3603 CPUState
*env
= _env
;
3604 qemu_cond_broadcast(env
->halt_cond
);
3606 qemu_thread_signal(env
->thread
, SIGUSR1
);
3609 int qemu_cpu_self(void *_env
)
3611 CPUState
*env
= _env
;
3614 qemu_thread_self(&this);
3616 return qemu_thread_equal(&this, env
->thread
);
3619 static void cpu_signal(int sig
)
3622 cpu_exit(cpu_single_env
);
3625 static void block_io_signals(void)
3628 struct sigaction sigact
;
3631 sigaddset(&set
, SIGUSR2
);
3632 sigaddset(&set
, SIGIO
);
3633 sigaddset(&set
, SIGALRM
);
3634 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3637 sigaddset(&set
, SIGUSR1
);
3638 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3640 memset(&sigact
, 0, sizeof(sigact
));
3641 sigact
.sa_handler
= cpu_signal
;
3642 sigaction(SIGUSR1
, &sigact
, NULL
);
3645 static void unblock_io_signals(void)
3650 sigaddset(&set
, SIGUSR2
);
3651 sigaddset(&set
, SIGIO
);
3652 sigaddset(&set
, SIGALRM
);
3653 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3656 sigaddset(&set
, SIGUSR1
);
3657 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3660 static void qemu_signal_lock(unsigned int msecs
)
3662 qemu_mutex_lock(&qemu_fair_mutex
);
3664 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3665 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3666 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3669 qemu_mutex_unlock(&qemu_fair_mutex
);
3672 void qemu_mutex_lock_iothread(void)
3674 if (kvm_enabled()) {
3675 qemu_mutex_lock(&qemu_fair_mutex
);
3676 qemu_mutex_lock(&qemu_global_mutex
);
3677 qemu_mutex_unlock(&qemu_fair_mutex
);
3679 qemu_signal_lock(100);
3682 void qemu_mutex_unlock_iothread(void)
3684 qemu_mutex_unlock(&qemu_global_mutex
);
3687 static int all_vcpus_paused(void)
3689 CPUState
*penv
= first_cpu
;
3694 penv
= (CPUState
*)penv
->next_cpu
;
3700 static void pause_all_vcpus(void)
3702 CPUState
*penv
= first_cpu
;
3706 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3707 qemu_cpu_kick(penv
);
3708 penv
= (CPUState
*)penv
->next_cpu
;
3711 while (!all_vcpus_paused()) {
3712 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3715 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3716 penv
= (CPUState
*)penv
->next_cpu
;
3721 static void resume_all_vcpus(void)
3723 CPUState
*penv
= first_cpu
;
3728 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3729 qemu_cpu_kick(penv
);
3730 penv
= (CPUState
*)penv
->next_cpu
;
3734 static void tcg_init_vcpu(void *_env
)
3736 CPUState
*env
= _env
;
3737 /* share a single thread for all cpus with TCG */
3738 if (!tcg_cpu_thread
) {
3739 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3740 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3741 qemu_cond_init(env
->halt_cond
);
3742 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3743 while (env
->created
== 0)
3744 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3745 tcg_cpu_thread
= env
->thread
;
3746 tcg_halt_cond
= env
->halt_cond
;
3748 env
->thread
= tcg_cpu_thread
;
3749 env
->halt_cond
= tcg_halt_cond
;
3753 static void kvm_start_vcpu(CPUState
*env
)
3755 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3756 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3757 qemu_cond_init(env
->halt_cond
);
3758 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3759 while (env
->created
== 0)
3760 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3763 void qemu_init_vcpu(void *_env
)
3765 CPUState
*env
= _env
;
3768 kvm_start_vcpu(env
);
3771 env
->nr_cores
= smp_cores
;
3772 env
->nr_threads
= smp_threads
;
3775 void qemu_notify_event(void)
3777 qemu_event_increment();
3780 void vm_stop(int reason
)
3783 qemu_thread_self(&me
);
3785 if (!qemu_thread_equal(&me
, &io_thread
)) {
3786 qemu_system_vmstop_request(reason
);
3788 * FIXME: should not return to device code in case
3789 * vm_stop() has been requested.
3791 if (cpu_single_env
) {
3792 cpu_exit(cpu_single_env
);
3793 cpu_single_env
->stop
= 1;
3804 static void host_main_loop_wait(int *timeout
)
3810 /* XXX: need to suppress polling by better using win32 events */
3812 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3813 ret
|= pe
->func(pe
->opaque
);
3817 WaitObjects
*w
= &wait_objects
;
3819 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3820 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3821 if (w
->func
[ret
- WAIT_OBJECT_0
])
3822 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3824 /* Check for additional signaled events */
3825 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3827 /* Check if event is signaled */
3828 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3829 if(ret2
== WAIT_OBJECT_0
) {
3831 w
->func
[i
](w
->opaque
[i
]);
3832 } else if (ret2
== WAIT_TIMEOUT
) {
3834 err
= GetLastError();
3835 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3838 } else if (ret
== WAIT_TIMEOUT
) {
3840 err
= GetLastError();
3841 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3848 static void host_main_loop_wait(int *timeout
)
3853 void main_loop_wait(int timeout
)
3855 IOHandlerRecord
*ioh
;
3856 fd_set rfds
, wfds
, xfds
;
3860 qemu_bh_update_timeout(&timeout
);
3862 host_main_loop_wait(&timeout
);
3864 /* poll any events */
3865 /* XXX: separate device handlers from system ones */
3870 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3874 (!ioh
->fd_read_poll
||
3875 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3876 FD_SET(ioh
->fd
, &rfds
);
3880 if (ioh
->fd_write
) {
3881 FD_SET(ioh
->fd
, &wfds
);
3887 tv
.tv_sec
= timeout
/ 1000;
3888 tv
.tv_usec
= (timeout
% 1000) * 1000;
3890 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3892 qemu_mutex_unlock_iothread();
3893 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3894 qemu_mutex_lock_iothread();
3896 IOHandlerRecord
**pioh
;
3898 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3899 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3900 ioh
->fd_read(ioh
->opaque
);
3902 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3903 ioh
->fd_write(ioh
->opaque
);
3907 /* remove deleted IO handlers */
3908 pioh
= &first_io_handler
;
3919 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3921 /* rearm timer, if not periodic */
3922 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3923 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3924 qemu_rearm_alarm_timer(alarm_timer
);
3927 /* vm time timers */
3929 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3930 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3931 qemu_get_clock(vm_clock
));
3934 /* real time timers */
3935 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3936 qemu_get_clock(rt_clock
));
3938 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3939 qemu_get_clock(host_clock
));
3941 /* Check bottom-halves last in case any of the earlier events triggered
3947 static int qemu_cpu_exec(CPUState
*env
)
3950 #ifdef CONFIG_PROFILER
3954 #ifdef CONFIG_PROFILER
3955 ti
= profile_getclock();
3960 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3961 env
->icount_decr
.u16
.low
= 0;
3962 env
->icount_extra
= 0;
3963 count
= qemu_next_deadline();
3964 count
= (count
+ (1 << icount_time_shift
) - 1)
3965 >> icount_time_shift
;
3966 qemu_icount
+= count
;
3967 decr
= (count
> 0xffff) ? 0xffff : count
;
3969 env
->icount_decr
.u16
.low
= decr
;
3970 env
->icount_extra
= count
;
3972 ret
= cpu_exec(env
);
3973 #ifdef CONFIG_PROFILER
3974 qemu_time
+= profile_getclock() - ti
;
3977 /* Fold pending instructions back into the
3978 instruction counter, and clear the interrupt flag. */
3979 qemu_icount
-= (env
->icount_decr
.u16
.low
3980 + env
->icount_extra
);
3981 env
->icount_decr
.u32
= 0;
3982 env
->icount_extra
= 0;
3987 static void tcg_cpu_exec(void)
3991 if (next_cpu
== NULL
)
3992 next_cpu
= first_cpu
;
3993 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3994 CPUState
*env
= cur_cpu
= next_cpu
;
3998 if (timer_alarm_pending
) {
3999 timer_alarm_pending
= 0;
4002 if (cpu_can_run(env
))
4003 ret
= qemu_cpu_exec(env
);
4004 if (ret
== EXCP_DEBUG
) {
4005 gdb_set_stop_cpu(env
);
4006 debug_requested
= 1;
4012 static int cpu_has_work(CPUState
*env
)
4020 if (qemu_cpu_has_work(env
))
4025 static int tcg_has_work(void)
4029 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4030 if (cpu_has_work(env
))
4035 static int qemu_calculate_timeout(void)
4037 #ifndef CONFIG_IOTHREAD
4042 else if (tcg_has_work())
4044 else if (!use_icount
)
4047 /* XXX: use timeout computed from timers */
4050 /* Advance virtual time to the next event. */
4051 if (use_icount
== 1) {
4052 /* When not using an adaptive execution frequency
4053 we tend to get badly out of sync with real time,
4054 so just delay for a reasonable amount of time. */
4057 delta
= cpu_get_icount() - cpu_get_clock();
4060 /* If virtual time is ahead of real time then just
4062 timeout
= (delta
/ 1000000) + 1;
4064 /* Wait for either IO to occur or the next
4066 add
= qemu_next_deadline();
4067 /* We advance the timer before checking for IO.
4068 Limit the amount we advance so that early IO
4069 activity won't get the guest too far ahead. */
4073 add
= (add
+ (1 << icount_time_shift
) - 1)
4074 >> icount_time_shift
;
4076 timeout
= delta
/ 1000000;
4083 #else /* CONFIG_IOTHREAD */
4088 static int vm_can_run(void)
4090 if (powerdown_requested
)
4092 if (reset_requested
)
4094 if (shutdown_requested
)
4096 if (debug_requested
)
4101 qemu_irq qemu_system_powerdown
;
4103 static void main_loop(void)
4107 #ifdef CONFIG_IOTHREAD
4108 qemu_system_ready
= 1;
4109 qemu_cond_broadcast(&qemu_system_cond
);
4114 #ifdef CONFIG_PROFILER
4117 #ifndef CONFIG_IOTHREAD
4120 #ifdef CONFIG_PROFILER
4121 ti
= profile_getclock();
4123 main_loop_wait(qemu_calculate_timeout());
4124 #ifdef CONFIG_PROFILER
4125 dev_time
+= profile_getclock() - ti
;
4127 } while (vm_can_run());
4129 if (qemu_debug_requested()) {
4130 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
4131 vm_stop(EXCP_DEBUG
);
4133 if (qemu_shutdown_requested()) {
4134 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4141 if (qemu_reset_requested()) {
4142 monitor_protocol_event(QEVENT_RESET
, NULL
);
4144 qemu_system_reset();
4147 if (qemu_powerdown_requested()) {
4148 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4149 qemu_irq_raise(qemu_system_powerdown
);
4151 if ((r
= qemu_vmstop_requested())) {
4152 monitor_protocol_event(QEVENT_STOP
, NULL
);
4159 static void version(void)
4161 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4164 static void help(int exitcode
)
4167 printf("usage: %s [options] [disk_image]\n"
4169 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4171 #define DEF(option, opt_arg, opt_enum, opt_help) \
4173 #define DEFHEADING(text) stringify(text) "\n"
4174 #include "qemu-options.h"
4179 "During emulation, the following keys are useful:\n"
4180 "ctrl-alt-f toggle full screen\n"
4181 "ctrl-alt-n switch to virtual console 'n'\n"
4182 "ctrl-alt toggle mouse and keyboard grab\n"
4184 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4189 DEFAULT_NETWORK_SCRIPT
,
4190 DEFAULT_NETWORK_DOWN_SCRIPT
,
4192 DEFAULT_GDBSTUB_PORT
,
4197 #define HAS_ARG 0x0001
4200 #define DEF(option, opt_arg, opt_enum, opt_help) \
4202 #define DEFHEADING(text)
4203 #include "qemu-options.h"
4209 typedef struct QEMUOption
{
4215 static const QEMUOption qemu_options
[] = {
4216 { "h", 0, QEMU_OPTION_h
},
4217 #define DEF(option, opt_arg, opt_enum, opt_help) \
4218 { option, opt_arg, opt_enum },
4219 #define DEFHEADING(text)
4220 #include "qemu-options.h"
4228 struct soundhw soundhw
[] = {
4229 #ifdef HAS_AUDIO_CHOICE
4230 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4236 { .init_isa
= pcspk_audio_init
}
4243 "Creative Sound Blaster 16",
4246 { .init_isa
= SB16_init
}
4250 #ifdef CONFIG_CS4231A
4256 { .init_isa
= cs4231a_init
}
4264 "Yamaha YMF262 (OPL3)",
4266 "Yamaha YM3812 (OPL2)",
4270 { .init_isa
= Adlib_init
}
4277 "Gravis Ultrasound GF1",
4280 { .init_isa
= GUS_init
}
4287 "Intel 82801AA AC97 Audio",
4290 { .init_pci
= ac97_init
}
4294 #ifdef CONFIG_ES1370
4297 "ENSONIQ AudioPCI ES1370",
4300 { .init_pci
= es1370_init
}
4304 #endif /* HAS_AUDIO_CHOICE */
4306 { NULL
, NULL
, 0, 0, { NULL
} }
4309 static void select_soundhw (const char *optarg
)
4313 if (*optarg
== '?') {
4316 printf ("Valid sound card names (comma separated):\n");
4317 for (c
= soundhw
; c
->name
; ++c
) {
4318 printf ("%-11s %s\n", c
->name
, c
->descr
);
4320 printf ("\n-soundhw all will enable all of the above\n");
4321 exit (*optarg
!= '?');
4329 if (!strcmp (optarg
, "all")) {
4330 for (c
= soundhw
; c
->name
; ++c
) {
4338 e
= strchr (p
, ',');
4339 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4341 for (c
= soundhw
; c
->name
; ++c
) {
4342 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4351 "Unknown sound card name (too big to show)\n");
4354 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4359 p
+= l
+ (e
!= NULL
);
4363 goto show_valid_cards
;
4368 static void select_vgahw (const char *p
)
4372 vga_interface_type
= VGA_NONE
;
4373 if (strstart(p
, "std", &opts
)) {
4374 vga_interface_type
= VGA_STD
;
4375 } else if (strstart(p
, "cirrus", &opts
)) {
4376 vga_interface_type
= VGA_CIRRUS
;
4377 } else if (strstart(p
, "vmware", &opts
)) {
4378 vga_interface_type
= VGA_VMWARE
;
4379 } else if (strstart(p
, "xenfb", &opts
)) {
4380 vga_interface_type
= VGA_XENFB
;
4381 } else if (!strstart(p
, "none", &opts
)) {
4383 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4387 const char *nextopt
;
4389 if (strstart(opts
, ",retrace=", &nextopt
)) {
4391 if (strstart(opts
, "dumb", &nextopt
))
4392 vga_retrace_method
= VGA_RETRACE_DUMB
;
4393 else if (strstart(opts
, "precise", &nextopt
))
4394 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4395 else goto invalid_vga
;
4396 } else goto invalid_vga
;
4402 static int balloon_parse(const char *arg
)
4406 if (strcmp(arg
, "none") == 0) {
4410 if (!strncmp(arg
, "virtio", 6)) {
4411 if (arg
[6] == ',') {
4412 /* have params -> parse them */
4413 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4417 /* create empty opts */
4418 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4420 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4429 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4431 exit(STATUS_CONTROL_C_EXIT
);
4436 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4440 if(strlen(str
) != 36)
4443 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4444 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4445 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4451 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4459 static void termsig_handler(int signal
)
4461 qemu_system_shutdown_request();
4464 static void sigchld_handler(int signal
)
4466 waitpid(-1, NULL
, WNOHANG
);
4469 static void sighandler_setup(void)
4471 struct sigaction act
;
4473 memset(&act
, 0, sizeof(act
));
4474 act
.sa_handler
= termsig_handler
;
4475 sigaction(SIGINT
, &act
, NULL
);
4476 sigaction(SIGHUP
, &act
, NULL
);
4477 sigaction(SIGTERM
, &act
, NULL
);
4479 act
.sa_handler
= sigchld_handler
;
4480 act
.sa_flags
= SA_NOCLDSTOP
;
4481 sigaction(SIGCHLD
, &act
, NULL
);
4487 /* Look for support files in the same directory as the executable. */
4488 static char *find_datadir(const char *argv0
)
4494 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4501 while (p
!= buf
&& *p
!= '\\')
4504 if (access(buf
, R_OK
) == 0) {
4505 return qemu_strdup(buf
);
4511 /* Find a likely location for support files using the location of the binary.
4512 For installed binaries this will be "$bindir/../share/qemu". When
4513 running from the build tree this will be "$bindir/../pc-bios". */
4514 #define SHARE_SUFFIX "/share/qemu"
4515 #define BUILD_SUFFIX "/pc-bios"
4516 static char *find_datadir(const char *argv0
)
4524 #if defined(__linux__)
4527 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4533 #elif defined(__FreeBSD__)
4536 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4543 /* If we don't have any way of figuring out the actual executable
4544 location then try argv[0]. */
4546 p
= realpath(argv0
, buf
);
4554 max_len
= strlen(dir
) +
4555 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4556 res
= qemu_mallocz(max_len
);
4557 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4558 if (access(res
, R_OK
)) {
4559 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4560 if (access(res
, R_OK
)) {
4572 char *qemu_find_file(int type
, const char *name
)
4578 /* If name contains path separators then try it as a straight path. */
4579 if ((strchr(name
, '/') || strchr(name
, '\\'))
4580 && access(name
, R_OK
) == 0) {
4581 return qemu_strdup(name
);
4584 case QEMU_FILE_TYPE_BIOS
:
4587 case QEMU_FILE_TYPE_KEYMAP
:
4588 subdir
= "keymaps/";
4593 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4594 buf
= qemu_mallocz(len
);
4595 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4596 if (access(buf
, R_OK
)) {
4603 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4607 dev
= qdev_device_add(opts
);
4613 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4615 CharDriverState
*chr
;
4617 chr
= qemu_chr_open_opts(opts
, NULL
);
4623 struct device_config
{
4625 DEV_USB
, /* -usbdevice */
4627 DEV_SERIAL
, /* -serial */
4629 const char *cmdline
;
4630 QTAILQ_ENTRY(device_config
) next
;
4632 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4634 static void add_device_config(int type
, const char *cmdline
)
4636 struct device_config
*conf
;
4638 conf
= qemu_mallocz(sizeof(*conf
));
4640 conf
->cmdline
= cmdline
;
4641 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4644 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4646 struct device_config
*conf
;
4649 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4650 if (conf
->type
!= type
)
4652 rc
= func(conf
->cmdline
);
4659 static void serial_monitor_mux(const char *monitor_devices
[])
4661 struct device_config
*serial
;
4662 const char *devname
;
4664 if (strcmp(monitor_devices
[0],"stdio") != 0)
4666 QTAILQ_FOREACH(serial
, &device_configs
, next
) {
4667 if (serial
->type
!= DEV_SERIAL
)
4669 devname
= serial
->cmdline
;
4670 if (devname
&& !strcmp(devname
,"mon:stdio")) {
4671 monitor_devices
[0] = NULL
;
4673 } else if (devname
&& !strcmp(devname
,"stdio")) {
4674 monitor_devices
[0] = NULL
;
4675 serial
->cmdline
= "mon:stdio";
4681 static int serial_parse(const char *devname
)
4683 static int index
= 0;
4686 if (strcmp(devname
, "none") == 0)
4688 if (index
== MAX_SERIAL_PORTS
) {
4689 fprintf(stderr
, "qemu: too many serial ports\n");
4692 snprintf(label
, sizeof(label
), "serial%d", index
);
4693 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4694 if (!serial_hds
[index
]) {
4695 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4696 devname
, strerror(errno
));
4703 int main(int argc
, char **argv
, char **envp
)
4705 const char *gdbstub_dev
= NULL
;
4706 uint32_t boot_devices_bitmap
= 0;
4708 int snapshot
, linux_boot
, net_boot
;
4709 const char *initrd_filename
;
4710 const char *kernel_filename
, *kernel_cmdline
;
4711 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4713 DisplayChangeListener
*dcl
;
4714 int cyls
, heads
, secs
, translation
;
4715 QemuOpts
*hda_opts
= NULL
, *opts
;
4717 const char *r
, *optarg
;
4718 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4719 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4720 int monitor_device_index
;
4721 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4722 int parallel_device_index
;
4723 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4724 int virtio_console_index
;
4725 const char *loadvm
= NULL
;
4726 QEMUMachine
*machine
;
4727 const char *cpu_model
;
4732 const char *pid_file
= NULL
;
4733 const char *incoming
= NULL
;
4736 struct passwd
*pwd
= NULL
;
4737 const char *chroot_dir
= NULL
;
4738 const char *run_as
= NULL
;
4741 int show_vnc_port
= 0;
4745 qemu_errors_to_file(stderr
);
4746 qemu_cache_utils_init(envp
);
4748 QLIST_INIT (&vm_change_state_head
);
4751 struct sigaction act
;
4752 sigfillset(&act
.sa_mask
);
4754 act
.sa_handler
= SIG_IGN
;
4755 sigaction(SIGPIPE
, &act
, NULL
);
4758 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4759 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4760 QEMU to run on a single CPU */
4765 h
= GetCurrentProcess();
4766 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4767 for(i
= 0; i
< 32; i
++) {
4768 if (mask
& (1 << i
))
4773 SetProcessAffinityMask(h
, mask
);
4779 module_call_init(MODULE_INIT_MACHINE
);
4780 machine
= find_default_machine();
4782 initrd_filename
= NULL
;
4785 kernel_filename
= NULL
;
4786 kernel_cmdline
= "";
4787 cyls
= heads
= secs
= 0;
4788 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4790 parallel_devices
[0] = "vc:80Cx24C";
4791 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4792 parallel_devices
[i
] = NULL
;
4793 parallel_device_index
= 0;
4795 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4796 virtio_consoles
[i
] = NULL
;
4797 virtio_console_index
= 0;
4799 monitor_devices
[0] = "vc:80Cx24C";
4800 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4801 monitor_devices
[i
] = NULL
;
4803 monitor_device_index
= 0;
4805 for (i
= 0; i
< MAX_NODES
; i
++) {
4807 node_cpumask
[i
] = 0;
4822 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4824 const QEMUOption
*popt
;
4827 /* Treat --foo the same as -foo. */
4830 popt
= qemu_options
;
4833 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4837 if (!strcmp(popt
->name
, r
+ 1))
4841 if (popt
->flags
& HAS_ARG
) {
4842 if (optind
>= argc
) {
4843 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4847 optarg
= argv
[optind
++];
4852 switch(popt
->index
) {
4854 machine
= find_machine(optarg
);
4857 printf("Supported machines are:\n");
4858 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4860 printf("%-10s %s (alias of %s)\n",
4861 m
->alias
, m
->desc
, m
->name
);
4862 printf("%-10s %s%s\n",
4864 m
->is_default
? " (default)" : "");
4866 exit(*optarg
!= '?');
4869 case QEMU_OPTION_cpu
:
4870 /* hw initialization will check this */
4871 if (*optarg
== '?') {
4872 /* XXX: implement xxx_cpu_list for targets that still miss it */
4873 #if defined(cpu_list)
4874 cpu_list(stdout
, &fprintf
);
4881 case QEMU_OPTION_initrd
:
4882 initrd_filename
= optarg
;
4884 case QEMU_OPTION_hda
:
4886 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4888 hda_opts
= drive_add(optarg
, HD_ALIAS
4889 ",cyls=%d,heads=%d,secs=%d%s",
4890 0, cyls
, heads
, secs
,
4891 translation
== BIOS_ATA_TRANSLATION_LBA
?
4893 translation
== BIOS_ATA_TRANSLATION_NONE
?
4894 ",trans=none" : "");
4896 case QEMU_OPTION_hdb
:
4897 case QEMU_OPTION_hdc
:
4898 case QEMU_OPTION_hdd
:
4899 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4901 case QEMU_OPTION_drive
:
4902 drive_add(NULL
, "%s", optarg
);
4904 case QEMU_OPTION_set
:
4905 if (qemu_set_option(optarg
) != 0)
4908 case QEMU_OPTION_global
:
4909 if (qemu_global_option(optarg
) != 0)
4912 case QEMU_OPTION_mtdblock
:
4913 drive_add(optarg
, MTD_ALIAS
);
4915 case QEMU_OPTION_sd
:
4916 drive_add(optarg
, SD_ALIAS
);
4918 case QEMU_OPTION_pflash
:
4919 drive_add(optarg
, PFLASH_ALIAS
);
4921 case QEMU_OPTION_snapshot
:
4924 case QEMU_OPTION_hdachs
:
4928 cyls
= strtol(p
, (char **)&p
, 0);
4929 if (cyls
< 1 || cyls
> 16383)
4934 heads
= strtol(p
, (char **)&p
, 0);
4935 if (heads
< 1 || heads
> 16)
4940 secs
= strtol(p
, (char **)&p
, 0);
4941 if (secs
< 1 || secs
> 63)
4945 if (!strcmp(p
, "none"))
4946 translation
= BIOS_ATA_TRANSLATION_NONE
;
4947 else if (!strcmp(p
, "lba"))
4948 translation
= BIOS_ATA_TRANSLATION_LBA
;
4949 else if (!strcmp(p
, "auto"))
4950 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4953 } else if (*p
!= '\0') {
4955 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4958 if (hda_opts
!= NULL
) {
4960 snprintf(num
, sizeof(num
), "%d", cyls
);
4961 qemu_opt_set(hda_opts
, "cyls", num
);
4962 snprintf(num
, sizeof(num
), "%d", heads
);
4963 qemu_opt_set(hda_opts
, "heads", num
);
4964 snprintf(num
, sizeof(num
), "%d", secs
);
4965 qemu_opt_set(hda_opts
, "secs", num
);
4966 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4967 qemu_opt_set(hda_opts
, "trans", "lba");
4968 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4969 qemu_opt_set(hda_opts
, "trans", "none");
4973 case QEMU_OPTION_numa
:
4974 if (nb_numa_nodes
>= MAX_NODES
) {
4975 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4980 case QEMU_OPTION_nographic
:
4981 display_type
= DT_NOGRAPHIC
;
4983 #ifdef CONFIG_CURSES
4984 case QEMU_OPTION_curses
:
4985 display_type
= DT_CURSES
;
4988 case QEMU_OPTION_portrait
:
4991 case QEMU_OPTION_kernel
:
4992 kernel_filename
= optarg
;
4994 case QEMU_OPTION_append
:
4995 kernel_cmdline
= optarg
;
4997 case QEMU_OPTION_cdrom
:
4998 drive_add(optarg
, CDROM_ALIAS
);
5000 case QEMU_OPTION_boot
:
5002 static const char * const params
[] = {
5003 "order", "once", "menu", NULL
5005 char buf
[sizeof(boot_devices
)];
5006 char *standard_boot_devices
;
5009 if (!strchr(optarg
, '=')) {
5011 pstrcpy(buf
, sizeof(buf
), optarg
);
5012 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5014 "qemu: unknown boot parameter '%s' in '%s'\n",
5020 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5021 boot_devices_bitmap
= parse_bootdevices(buf
);
5022 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5025 if (get_param_value(buf
, sizeof(buf
),
5027 boot_devices_bitmap
|= parse_bootdevices(buf
);
5028 standard_boot_devices
= qemu_strdup(boot_devices
);
5029 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5030 qemu_register_reset(restore_boot_devices
,
5031 standard_boot_devices
);
5033 if (get_param_value(buf
, sizeof(buf
),
5035 if (!strcmp(buf
, "on")) {
5037 } else if (!strcmp(buf
, "off")) {
5041 "qemu: invalid option value '%s'\n",
5049 case QEMU_OPTION_fda
:
5050 case QEMU_OPTION_fdb
:
5051 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5054 case QEMU_OPTION_no_fd_bootchk
:
5058 case QEMU_OPTION_netdev
:
5059 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5063 case QEMU_OPTION_net
:
5064 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5069 case QEMU_OPTION_tftp
:
5070 legacy_tftp_prefix
= optarg
;
5072 case QEMU_OPTION_bootp
:
5073 legacy_bootp_filename
= optarg
;
5076 case QEMU_OPTION_smb
:
5077 if (net_slirp_smb(optarg
) < 0)
5081 case QEMU_OPTION_redir
:
5082 if (net_slirp_redir(optarg
) < 0)
5086 case QEMU_OPTION_bt
:
5087 add_device_config(DEV_BT
, optarg
);
5090 case QEMU_OPTION_audio_help
:
5094 case QEMU_OPTION_soundhw
:
5095 select_soundhw (optarg
);
5101 case QEMU_OPTION_version
:
5105 case QEMU_OPTION_m
: {
5109 value
= strtoul(optarg
, &ptr
, 10);
5111 case 0: case 'M': case 'm':
5118 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5122 /* On 32-bit hosts, QEMU is limited by virtual address space */
5123 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5124 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5127 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5128 fprintf(stderr
, "qemu: ram size too large\n");
5137 const CPULogItem
*item
;
5139 mask
= cpu_str_to_log_mask(optarg
);
5141 printf("Log items (comma separated):\n");
5142 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5143 printf("%-10s %s\n", item
->name
, item
->help
);
5151 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5153 case QEMU_OPTION_gdb
:
5154 gdbstub_dev
= optarg
;
5159 case QEMU_OPTION_bios
:
5162 case QEMU_OPTION_singlestep
:
5169 keyboard_layout
= optarg
;
5171 case QEMU_OPTION_localtime
:
5174 case QEMU_OPTION_vga
:
5175 select_vgahw (optarg
);
5177 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5183 w
= strtol(p
, (char **)&p
, 10);
5186 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5192 h
= strtol(p
, (char **)&p
, 10);
5197 depth
= strtol(p
, (char **)&p
, 10);
5198 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5199 depth
!= 24 && depth
!= 32)
5201 } else if (*p
== '\0') {
5202 depth
= graphic_depth
;
5209 graphic_depth
= depth
;
5213 case QEMU_OPTION_echr
:
5216 term_escape_char
= strtol(optarg
, &r
, 0);
5218 printf("Bad argument to echr\n");
5221 case QEMU_OPTION_monitor
:
5222 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5223 fprintf(stderr
, "qemu: too many monitor devices\n");
5226 monitor_devices
[monitor_device_index
] = optarg
;
5227 monitor_device_index
++;
5229 case QEMU_OPTION_chardev
:
5230 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5232 fprintf(stderr
, "parse error: %s\n", optarg
);
5236 case QEMU_OPTION_serial
:
5237 add_device_config(DEV_SERIAL
, optarg
);
5240 case QEMU_OPTION_watchdog
:
5243 "qemu: only one watchdog option may be given\n");
5248 case QEMU_OPTION_watchdog_action
:
5249 if (select_watchdog_action(optarg
) == -1) {
5250 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5254 case QEMU_OPTION_virtiocon
:
5255 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5256 fprintf(stderr
, "qemu: too many virtio consoles\n");
5259 virtio_consoles
[virtio_console_index
] = optarg
;
5260 virtio_console_index
++;
5262 case QEMU_OPTION_parallel
:
5263 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5264 fprintf(stderr
, "qemu: too many parallel ports\n");
5267 parallel_devices
[parallel_device_index
] = optarg
;
5268 parallel_device_index
++;
5270 case QEMU_OPTION_loadvm
:
5273 case QEMU_OPTION_full_screen
:
5277 case QEMU_OPTION_no_frame
:
5280 case QEMU_OPTION_alt_grab
:
5283 case QEMU_OPTION_ctrl_grab
:
5286 case QEMU_OPTION_no_quit
:
5289 case QEMU_OPTION_sdl
:
5290 display_type
= DT_SDL
;
5293 case QEMU_OPTION_pidfile
:
5297 case QEMU_OPTION_win2k_hack
:
5298 win2k_install_hack
= 1;
5300 case QEMU_OPTION_rtc_td_hack
:
5303 case QEMU_OPTION_acpitable
:
5304 if(acpi_table_add(optarg
) < 0) {
5305 fprintf(stderr
, "Wrong acpi table provided\n");
5309 case QEMU_OPTION_smbios
:
5310 if(smbios_entry_add(optarg
) < 0) {
5311 fprintf(stderr
, "Wrong smbios provided\n");
5317 case QEMU_OPTION_enable_kvm
:
5321 case QEMU_OPTION_usb
:
5324 case QEMU_OPTION_usbdevice
:
5326 add_device_config(DEV_USB
, optarg
);
5328 case QEMU_OPTION_device
:
5329 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5333 case QEMU_OPTION_smp
:
5336 fprintf(stderr
, "Invalid number of CPUs\n");
5339 if (max_cpus
< smp_cpus
) {
5340 fprintf(stderr
, "maxcpus must be equal to or greater than "
5344 if (max_cpus
> 255) {
5345 fprintf(stderr
, "Unsupported number of maxcpus\n");
5349 case QEMU_OPTION_vnc
:
5350 display_type
= DT_VNC
;
5351 vnc_display
= optarg
;
5354 case QEMU_OPTION_no_acpi
:
5357 case QEMU_OPTION_no_hpet
:
5360 case QEMU_OPTION_balloon
:
5361 if (balloon_parse(optarg
) < 0) {
5362 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5367 case QEMU_OPTION_no_reboot
:
5370 case QEMU_OPTION_no_shutdown
:
5373 case QEMU_OPTION_show_cursor
:
5376 case QEMU_OPTION_uuid
:
5377 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5378 fprintf(stderr
, "Fail to parse UUID string."
5379 " Wrong format.\n");
5384 case QEMU_OPTION_daemonize
:
5388 case QEMU_OPTION_option_rom
:
5389 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5390 fprintf(stderr
, "Too many option ROMs\n");
5393 option_rom
[nb_option_roms
] = optarg
;
5396 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5397 case QEMU_OPTION_semihosting
:
5398 semihosting_enabled
= 1;
5401 case QEMU_OPTION_name
:
5402 qemu_name
= qemu_strdup(optarg
);
5404 char *p
= strchr(qemu_name
, ',');
5407 if (strncmp(p
, "process=", 8)) {
5408 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5416 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5417 case QEMU_OPTION_prom_env
:
5418 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5419 fprintf(stderr
, "Too many prom variables\n");
5422 prom_envs
[nb_prom_envs
] = optarg
;
5427 case QEMU_OPTION_old_param
:
5431 case QEMU_OPTION_clock
:
5432 configure_alarms(optarg
);
5434 case QEMU_OPTION_startdate
:
5435 configure_rtc_date_offset(optarg
, 1);
5437 case QEMU_OPTION_rtc
:
5438 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5440 fprintf(stderr
, "parse error: %s\n", optarg
);
5443 configure_rtc(opts
);
5445 case QEMU_OPTION_tb_size
:
5446 tb_size
= strtol(optarg
, NULL
, 0);
5450 case QEMU_OPTION_icount
:
5452 if (strcmp(optarg
, "auto") == 0) {
5453 icount_time_shift
= -1;
5455 icount_time_shift
= strtol(optarg
, NULL
, 0);
5458 case QEMU_OPTION_incoming
:
5462 case QEMU_OPTION_chroot
:
5463 chroot_dir
= optarg
;
5465 case QEMU_OPTION_runas
:
5470 case QEMU_OPTION_xen_domid
:
5471 xen_domid
= atoi(optarg
);
5473 case QEMU_OPTION_xen_create
:
5474 xen_mode
= XEN_CREATE
;
5476 case QEMU_OPTION_xen_attach
:
5477 xen_mode
= XEN_ATTACH
;
5480 case QEMU_OPTION_readconfig
:
5483 fp
= fopen(optarg
, "r");
5485 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5488 if (qemu_config_parse(fp
) != 0) {
5494 case QEMU_OPTION_writeconfig
:
5497 if (strcmp(optarg
, "-") == 0) {
5500 fp
= fopen(optarg
, "w");
5502 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5506 qemu_config_write(fp
);
5514 /* If no data_dir is specified then try to find it relative to the
5517 data_dir
= find_datadir(argv
[0]);
5519 /* If all else fails use the install patch specified when building. */
5521 data_dir
= CONFIG_QEMU_SHAREDIR
;
5525 * Default to max_cpus = smp_cpus, in case the user doesn't
5526 * specify a max_cpus value.
5529 max_cpus
= smp_cpus
;
5531 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5532 if (smp_cpus
> machine
->max_cpus
) {
5533 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5534 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5539 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5541 if (display_type
== DT_NOGRAPHIC
) {
5543 add_device_config(DEV_SERIAL
, "stdio");
5544 if (parallel_device_index
== 0)
5545 parallel_devices
[0] = "null";
5546 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5547 monitor_devices
[0] = "stdio";
5551 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5554 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5561 if (pipe(fds
) == -1)
5572 len
= read(fds
[0], &status
, 1);
5573 if (len
== -1 && (errno
== EINTR
))
5578 else if (status
== 1) {
5579 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5587 qemu_set_cloexec(fds
[1]);
5599 signal(SIGTSTP
, SIG_IGN
);
5600 signal(SIGTTOU
, SIG_IGN
);
5601 signal(SIGTTIN
, SIG_IGN
);
5604 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5607 write(fds
[1], &status
, 1);
5609 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5614 if (kvm_enabled()) {
5617 ret
= kvm_init(smp_cpus
);
5619 fprintf(stderr
, "failed to initialize KVM\n");
5624 if (qemu_init_main_loop()) {
5625 fprintf(stderr
, "qemu_init_main_loop failed\n");
5628 linux_boot
= (kernel_filename
!= NULL
);
5630 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5631 fprintf(stderr
, "-append only allowed with -kernel option\n");
5635 if (!linux_boot
&& initrd_filename
!= NULL
) {
5636 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5641 /* Win32 doesn't support line-buffering and requires size >= 2 */
5642 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5645 if (init_timer_alarm() < 0) {
5646 fprintf(stderr
, "could not initialize alarm timer\n");
5649 if (use_icount
&& icount_time_shift
< 0) {
5651 /* 125MIPS seems a reasonable initial guess at the guest speed.
5652 It will be corrected fairly quickly anyway. */
5653 icount_time_shift
= 3;
5654 init_icount_adjust();
5661 if (net_init_clients() < 0) {
5665 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5666 net_set_boot_mask(net_boot
);
5668 /* init the bluetooth world */
5669 if (foreach_device_config(DEV_BT
, bt_parse
))
5672 /* init the memory */
5674 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5676 /* init the dynamic translator */
5677 cpu_exec_init_all(tb_size
* 1024 * 1024);
5679 bdrv_init_with_whitelist();
5683 /* we always create the cdrom drive, even if no disk is there */
5684 drive_add(NULL
, CDROM_ALIAS
);
5686 /* we always create at least one floppy */
5687 drive_add(NULL
, FD_ALIAS
, 0);
5689 /* we always create one sd slot, even if no card is in it */
5690 drive_add(NULL
, SD_ALIAS
);
5692 /* open the virtual block devices */
5694 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5695 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5698 vmstate_register(0, &vmstate_timers
,&timers_state
);
5699 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5702 /* Maintain compatibility with multiple stdio monitors */
5703 serial_monitor_mux(monitor_devices
);
5705 if (nb_numa_nodes
> 0) {
5708 if (nb_numa_nodes
> smp_cpus
) {
5709 nb_numa_nodes
= smp_cpus
;
5712 /* If no memory size if given for any node, assume the default case
5713 * and distribute the available memory equally across all nodes
5715 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5716 if (node_mem
[i
] != 0)
5719 if (i
== nb_numa_nodes
) {
5720 uint64_t usedmem
= 0;
5722 /* On Linux, the each node's border has to be 8MB aligned,
5723 * the final node gets the rest.
5725 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5726 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5727 usedmem
+= node_mem
[i
];
5729 node_mem
[i
] = ram_size
- usedmem
;
5732 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5733 if (node_cpumask
[i
] != 0)
5736 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5737 * must cope with this anyway, because there are BIOSes out there in
5738 * real machines which also use this scheme.
5740 if (i
== nb_numa_nodes
) {
5741 for (i
= 0; i
< smp_cpus
; i
++) {
5742 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5747 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5748 const char *devname
= monitor_devices
[i
];
5749 if (devname
&& strcmp(devname
, "none")) {
5752 snprintf(label
, sizeof(label
), "monitor");
5754 snprintf(label
, sizeof(label
), "monitor%d", i
);
5756 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5757 if (!monitor_hds
[i
]) {
5758 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5765 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5768 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5769 const char *devname
= parallel_devices
[i
];
5770 if (devname
&& strcmp(devname
, "none")) {
5772 snprintf(label
, sizeof(label
), "parallel%d", i
);
5773 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5774 if (!parallel_hds
[i
]) {
5775 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5776 devname
, strerror(errno
));
5782 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5783 const char *devname
= virtio_consoles
[i
];
5784 if (devname
&& strcmp(devname
, "none")) {
5786 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5787 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5788 if (!virtcon_hds
[i
]) {
5789 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5790 devname
, strerror(errno
));
5796 module_call_init(MODULE_INIT_DEVICE
);
5799 i
= select_watchdog(watchdog
);
5801 exit (i
== 1 ? 1 : 0);
5804 if (machine
->compat_props
) {
5805 qdev_prop_register_global_list(machine
->compat_props
);
5809 machine
->init(ram_size
, boot_devices
,
5810 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5814 /* must be after terminal init, SDL library changes signal handlers */
5818 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5819 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5820 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5826 current_machine
= machine
;
5828 /* init USB devices */
5830 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5834 /* init generic devices */
5835 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5839 dumb_display_init();
5840 /* just use the first displaystate for the moment */
5843 if (display_type
== DT_DEFAULT
) {
5844 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5845 display_type
= DT_SDL
;
5847 display_type
= DT_VNC
;
5848 vnc_display
= "localhost:0,to=99";
5854 switch (display_type
) {
5857 #if defined(CONFIG_CURSES)
5859 curses_display_init(ds
, full_screen
);
5862 #if defined(CONFIG_SDL)
5864 sdl_display_init(ds
, full_screen
, no_frame
);
5866 #elif defined(CONFIG_COCOA)
5868 cocoa_display_init(ds
, full_screen
);
5872 vnc_display_init(ds
);
5873 if (vnc_display_open(ds
, vnc_display
) < 0)
5876 if (show_vnc_port
) {
5877 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5885 dcl
= ds
->listeners
;
5886 while (dcl
!= NULL
) {
5887 if (dcl
->dpy_refresh
!= NULL
) {
5888 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5889 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5894 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5895 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5896 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5899 text_consoles_set_display(display_state
);
5901 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5902 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5903 monitor_init(monitor_hds
[i
],
5904 MONITOR_USE_READLINE
|
5905 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5909 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5910 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5915 qdev_machine_creation_done();
5919 qemu_system_reset();
5921 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5927 qemu_start_incoming_migration(incoming
);
5928 } else if (autostart
) {
5938 len
= write(fds
[1], &status
, 1);
5939 if (len
== -1 && (errno
== EINTR
))
5946 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5952 pwd
= getpwnam(run_as
);
5954 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5960 if (chroot(chroot_dir
) < 0) {
5961 fprintf(stderr
, "chroot failed\n");
5968 if (setgid(pwd
->pw_gid
) < 0) {
5969 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5972 if (setuid(pwd
->pw_uid
) < 0) {
5973 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5976 if (setuid(0) != -1) {
5977 fprintf(stderr
, "Dropping privileges failed\n");