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
];
214 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
216 int win2k_install_hack
= 0;
225 const char *vnc_display
;
226 int acpi_enabled
= 1;
232 int graphic_rotate
= 0;
233 uint8_t irq0override
= 1;
237 const char *watchdog
;
238 const char *option_rom
[MAX_OPTION_ROMS
];
240 int semihosting_enabled
= 0;
244 const char *qemu_name
;
247 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
248 unsigned int nb_prom_envs
= 0;
249 const char *prom_envs
[MAX_PROM_ENVS
];
254 uint64_t node_mem
[MAX_NODES
];
255 uint64_t node_cpumask
[MAX_NODES
];
257 static CPUState
*cur_cpu
;
258 static CPUState
*next_cpu
;
259 static int timer_alarm_pending
= 1;
260 /* Conversion factor from emulated instructions to virtual clock ticks. */
261 static int icount_time_shift
;
262 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
263 #define MAX_ICOUNT_SHIFT 10
264 /* Compensate for varying guest execution speed. */
265 static int64_t qemu_icount_bias
;
266 static QEMUTimer
*icount_rt_timer
;
267 static QEMUTimer
*icount_vm_timer
;
268 static QEMUTimer
*nographic_timer
;
270 uint8_t qemu_uuid
[16];
272 static QEMUBootSetHandler
*boot_set_handler
;
273 static void *boot_set_opaque
;
275 static int default_serial
= 1;
276 static int default_parallel
= 1;
277 static int default_monitor
= 1;
283 { .driver
= "isa-serial", .flag
= &default_serial
},
284 { .driver
= "isa-parallel", .flag
= &default_parallel
},
287 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
289 const char *driver
= qemu_opt_get(opts
, "driver");
294 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
295 if (strcmp(default_list
[i
].driver
, driver
) != 0)
297 *(default_list
[i
].flag
) = 0;
302 /***********************************************************/
303 /* x86 ISA bus support */
305 target_phys_addr_t isa_mem_base
= 0;
308 /***********************************************************/
309 void hw_error(const char *fmt
, ...)
315 fprintf(stderr
, "qemu: hardware error: ");
316 vfprintf(stderr
, fmt
, ap
);
317 fprintf(stderr
, "\n");
318 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
319 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
321 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
323 cpu_dump_state(env
, stderr
, fprintf
, 0);
330 static void set_proc_name(const char *s
)
332 #if defined(__linux__) && defined(PR_SET_NAME)
336 name
[sizeof(name
) - 1] = 0;
337 strncpy(name
, s
, sizeof(name
));
338 /* Could rewrite argv[0] too, but that's a bit more complicated.
339 This simple way is enough for `top'. */
340 prctl(PR_SET_NAME
, name
);
347 static QEMUBalloonEvent
*qemu_balloon_event
;
348 void *qemu_balloon_event_opaque
;
350 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
352 qemu_balloon_event
= func
;
353 qemu_balloon_event_opaque
= opaque
;
356 void qemu_balloon(ram_addr_t target
)
358 if (qemu_balloon_event
)
359 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
362 ram_addr_t
qemu_balloon_status(void)
364 if (qemu_balloon_event
)
365 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
369 /***********************************************************/
372 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
373 static void *qemu_put_kbd_event_opaque
;
374 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
375 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
377 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
379 qemu_put_kbd_event_opaque
= opaque
;
380 qemu_put_kbd_event
= func
;
383 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
384 void *opaque
, int absolute
,
387 QEMUPutMouseEntry
*s
, *cursor
;
389 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
391 s
->qemu_put_mouse_event
= func
;
392 s
->qemu_put_mouse_event_opaque
= opaque
;
393 s
->qemu_put_mouse_event_absolute
= absolute
;
394 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
397 if (!qemu_put_mouse_event_head
) {
398 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
402 cursor
= qemu_put_mouse_event_head
;
403 while (cursor
->next
!= NULL
)
404 cursor
= cursor
->next
;
407 qemu_put_mouse_event_current
= s
;
412 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
414 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
416 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
419 cursor
= qemu_put_mouse_event_head
;
420 while (cursor
!= NULL
&& cursor
!= entry
) {
422 cursor
= cursor
->next
;
425 if (cursor
== NULL
) // does not exist or list empty
427 else if (prev
== NULL
) { // entry is head
428 qemu_put_mouse_event_head
= cursor
->next
;
429 if (qemu_put_mouse_event_current
== entry
)
430 qemu_put_mouse_event_current
= cursor
->next
;
431 qemu_free(entry
->qemu_put_mouse_event_name
);
436 prev
->next
= entry
->next
;
438 if (qemu_put_mouse_event_current
== entry
)
439 qemu_put_mouse_event_current
= prev
;
441 qemu_free(entry
->qemu_put_mouse_event_name
);
445 void kbd_put_keycode(int keycode
)
447 if (qemu_put_kbd_event
) {
448 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
452 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
454 QEMUPutMouseEvent
*mouse_event
;
455 void *mouse_event_opaque
;
458 if (!qemu_put_mouse_event_current
) {
463 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
465 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
468 if (graphic_rotate
) {
469 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
472 width
= graphic_width
- 1;
473 mouse_event(mouse_event_opaque
,
474 width
- dy
, dx
, dz
, buttons_state
);
476 mouse_event(mouse_event_opaque
,
477 dx
, dy
, dz
, buttons_state
);
481 int kbd_mouse_is_absolute(void)
483 if (!qemu_put_mouse_event_current
)
486 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
489 void do_info_mice(Monitor
*mon
)
491 QEMUPutMouseEntry
*cursor
;
494 if (!qemu_put_mouse_event_head
) {
495 monitor_printf(mon
, "No mouse devices connected\n");
499 monitor_printf(mon
, "Mouse devices available:\n");
500 cursor
= qemu_put_mouse_event_head
;
501 while (cursor
!= NULL
) {
502 monitor_printf(mon
, "%c Mouse #%d: %s\n",
503 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
504 index
, cursor
->qemu_put_mouse_event_name
);
506 cursor
= cursor
->next
;
510 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
512 QEMUPutMouseEntry
*cursor
;
514 int index
= qdict_get_int(qdict
, "index");
516 if (!qemu_put_mouse_event_head
) {
517 monitor_printf(mon
, "No mouse devices connected\n");
521 cursor
= qemu_put_mouse_event_head
;
522 while (cursor
!= NULL
&& index
!= i
) {
524 cursor
= cursor
->next
;
528 qemu_put_mouse_event_current
= cursor
;
530 monitor_printf(mon
, "Mouse at given index not found\n");
533 /* compute with 96 bit intermediate result: (a*b)/c */
534 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
539 #ifdef HOST_WORDS_BIGENDIAN
549 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
550 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
553 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
557 /***********************************************************/
558 /* real time host monotonic timer */
560 static int64_t get_clock_realtime(void)
564 gettimeofday(&tv
, NULL
);
565 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
570 static int64_t clock_freq
;
572 static void init_get_clock(void)
576 ret
= QueryPerformanceFrequency(&freq
);
578 fprintf(stderr
, "Could not calibrate ticks\n");
581 clock_freq
= freq
.QuadPart
;
584 static int64_t get_clock(void)
587 QueryPerformanceCounter(&ti
);
588 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
593 static int use_rt_clock
;
595 static void init_get_clock(void)
598 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
599 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
602 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
609 static int64_t get_clock(void)
611 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
612 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
615 clock_gettime(CLOCK_MONOTONIC
, &ts
);
616 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
620 /* XXX: using gettimeofday leads to problems if the date
621 changes, so it should be avoided. */
622 return get_clock_realtime();
627 /* Return the virtual CPU time, based on the instruction counter. */
628 static int64_t cpu_get_icount(void)
631 CPUState
*env
= cpu_single_env
;;
632 icount
= qemu_icount
;
635 fprintf(stderr
, "Bad clock read\n");
636 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
638 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
641 /***********************************************************/
642 /* guest cycle counter */
644 typedef struct TimersState
{
645 int64_t cpu_ticks_prev
;
646 int64_t cpu_ticks_offset
;
647 int64_t cpu_clock_offset
;
648 int32_t cpu_ticks_enabled
;
652 TimersState timers_state
;
654 /* return the host CPU cycle counter and handle stop/restart */
655 int64_t cpu_get_ticks(void)
658 return cpu_get_icount();
660 if (!timers_state
.cpu_ticks_enabled
) {
661 return timers_state
.cpu_ticks_offset
;
664 ticks
= cpu_get_real_ticks();
665 if (timers_state
.cpu_ticks_prev
> ticks
) {
666 /* Note: non increasing ticks may happen if the host uses
668 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
670 timers_state
.cpu_ticks_prev
= ticks
;
671 return ticks
+ timers_state
.cpu_ticks_offset
;
675 /* return the host CPU monotonic timer and handle stop/restart */
676 static int64_t cpu_get_clock(void)
679 if (!timers_state
.cpu_ticks_enabled
) {
680 return timers_state
.cpu_clock_offset
;
683 return ti
+ timers_state
.cpu_clock_offset
;
687 /* enable cpu_get_ticks() */
688 void cpu_enable_ticks(void)
690 if (!timers_state
.cpu_ticks_enabled
) {
691 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
692 timers_state
.cpu_clock_offset
-= get_clock();
693 timers_state
.cpu_ticks_enabled
= 1;
697 /* disable cpu_get_ticks() : the clock is stopped. You must not call
698 cpu_get_ticks() after that. */
699 void cpu_disable_ticks(void)
701 if (timers_state
.cpu_ticks_enabled
) {
702 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
703 timers_state
.cpu_clock_offset
= cpu_get_clock();
704 timers_state
.cpu_ticks_enabled
= 0;
708 /***********************************************************/
711 #define QEMU_CLOCK_REALTIME 0
712 #define QEMU_CLOCK_VIRTUAL 1
713 #define QEMU_CLOCK_HOST 2
717 /* XXX: add frequency */
725 struct QEMUTimer
*next
;
728 struct qemu_alarm_timer
{
732 int (*start
)(struct qemu_alarm_timer
*t
);
733 void (*stop
)(struct qemu_alarm_timer
*t
);
734 void (*rearm
)(struct qemu_alarm_timer
*t
);
738 #define ALARM_FLAG_DYNTICKS 0x1
739 #define ALARM_FLAG_EXPIRED 0x2
741 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
743 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
746 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
748 if (!alarm_has_dynticks(t
))
754 /* TODO: MIN_TIMER_REARM_US should be optimized */
755 #define MIN_TIMER_REARM_US 250
757 static struct qemu_alarm_timer
*alarm_timer
;
761 struct qemu_alarm_win32
{
764 } alarm_win32_data
= {0, -1};
766 static int win32_start_timer(struct qemu_alarm_timer
*t
);
767 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
768 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
772 static int unix_start_timer(struct qemu_alarm_timer
*t
);
773 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
777 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
778 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
779 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
781 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
782 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
784 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
785 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
787 #endif /* __linux__ */
791 /* Correlation between real and virtual time is always going to be
792 fairly approximate, so ignore small variation.
793 When the guest is idle real and virtual time will be aligned in
795 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
797 static void icount_adjust(void)
802 static int64_t last_delta
;
803 /* If the VM is not running, then do nothing. */
807 cur_time
= cpu_get_clock();
808 cur_icount
= qemu_get_clock(vm_clock
);
809 delta
= cur_icount
- cur_time
;
810 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
812 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
813 && icount_time_shift
> 0) {
814 /* The guest is getting too far ahead. Slow time down. */
818 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
819 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
820 /* The guest is getting too far behind. Speed time up. */
824 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
827 static void icount_adjust_rt(void * opaque
)
829 qemu_mod_timer(icount_rt_timer
,
830 qemu_get_clock(rt_clock
) + 1000);
834 static void icount_adjust_vm(void * opaque
)
836 qemu_mod_timer(icount_vm_timer
,
837 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
841 static void init_icount_adjust(void)
843 /* Have both realtime and virtual time triggers for speed adjustment.
844 The realtime trigger catches emulated time passing too slowly,
845 the virtual time trigger catches emulated time passing too fast.
846 Realtime triggers occur even when idle, so use them less frequently
848 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
849 qemu_mod_timer(icount_rt_timer
,
850 qemu_get_clock(rt_clock
) + 1000);
851 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
852 qemu_mod_timer(icount_vm_timer
,
853 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
856 static struct qemu_alarm_timer alarm_timers
[] = {
859 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
860 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
861 /* HPET - if available - is preferred */
862 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
863 /* ...otherwise try RTC */
864 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
866 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
868 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
869 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
870 {"win32", 0, win32_start_timer
,
871 win32_stop_timer
, NULL
, &alarm_win32_data
},
876 static void show_available_alarms(void)
880 printf("Available alarm timers, in order of precedence:\n");
881 for (i
= 0; alarm_timers
[i
].name
; i
++)
882 printf("%s\n", alarm_timers
[i
].name
);
885 static void configure_alarms(char const *opt
)
889 int count
= ARRAY_SIZE(alarm_timers
) - 1;
892 struct qemu_alarm_timer tmp
;
894 if (!strcmp(opt
, "?")) {
895 show_available_alarms();
899 arg
= qemu_strdup(opt
);
901 /* Reorder the array */
902 name
= strtok(arg
, ",");
904 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
905 if (!strcmp(alarm_timers
[i
].name
, name
))
910 fprintf(stderr
, "Unknown clock %s\n", name
);
919 tmp
= alarm_timers
[i
];
920 alarm_timers
[i
] = alarm_timers
[cur
];
921 alarm_timers
[cur
] = tmp
;
925 name
= strtok(NULL
, ",");
931 /* Disable remaining timers */
932 for (i
= cur
; i
< count
; i
++)
933 alarm_timers
[i
].name
= NULL
;
935 show_available_alarms();
940 #define QEMU_NUM_CLOCKS 3
944 QEMUClock
*host_clock
;
946 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
948 static QEMUClock
*qemu_new_clock(int type
)
951 clock
= qemu_mallocz(sizeof(QEMUClock
));
956 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
960 ts
= qemu_mallocz(sizeof(QEMUTimer
));
967 void qemu_free_timer(QEMUTimer
*ts
)
972 /* stop a timer, but do not dealloc it */
973 void qemu_del_timer(QEMUTimer
*ts
)
977 /* NOTE: this code must be signal safe because
978 qemu_timer_expired() can be called from a signal. */
979 pt
= &active_timers
[ts
->clock
->type
];
992 /* modify the current timer so that it will be fired when current_time
993 >= expire_time. The corresponding callback will be called. */
994 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1000 /* add the timer in the sorted list */
1001 /* NOTE: this code must be signal safe because
1002 qemu_timer_expired() can be called from a signal. */
1003 pt
= &active_timers
[ts
->clock
->type
];
1008 if (t
->expire_time
> expire_time
)
1012 ts
->expire_time
= expire_time
;
1016 /* Rearm if necessary */
1017 if (pt
== &active_timers
[ts
->clock
->type
]) {
1018 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1019 qemu_rearm_alarm_timer(alarm_timer
);
1021 /* Interrupt execution to force deadline recalculation. */
1023 qemu_notify_event();
1027 int qemu_timer_pending(QEMUTimer
*ts
)
1030 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1037 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1041 return (timer_head
->expire_time
<= current_time
);
1044 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1050 if (!ts
|| ts
->expire_time
> current_time
)
1052 /* remove timer from the list before calling the callback */
1053 *ptimer_head
= ts
->next
;
1056 /* run the callback (the timer list can be modified) */
1061 int64_t qemu_get_clock(QEMUClock
*clock
)
1063 switch(clock
->type
) {
1064 case QEMU_CLOCK_REALTIME
:
1065 return get_clock() / 1000000;
1067 case QEMU_CLOCK_VIRTUAL
:
1069 return cpu_get_icount();
1071 return cpu_get_clock();
1073 case QEMU_CLOCK_HOST
:
1074 return get_clock_realtime();
1078 static void init_clocks(void)
1081 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1082 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1083 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1085 rtc_clock
= host_clock
;
1089 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1091 uint64_t expire_time
;
1093 if (qemu_timer_pending(ts
)) {
1094 expire_time
= ts
->expire_time
;
1098 qemu_put_be64(f
, expire_time
);
1101 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1103 uint64_t expire_time
;
1105 expire_time
= qemu_get_be64(f
);
1106 if (expire_time
!= -1) {
1107 qemu_mod_timer(ts
, expire_time
);
1113 static const VMStateDescription vmstate_timers
= {
1116 .minimum_version_id
= 1,
1117 .minimum_version_id_old
= 1,
1118 .fields
= (VMStateField
[]) {
1119 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1120 VMSTATE_INT64(dummy
, TimersState
),
1121 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1122 VMSTATE_END_OF_LIST()
1126 static void qemu_event_increment(void);
1129 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1130 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1133 static void host_alarm_handler(int host_signum
)
1137 #define DISP_FREQ 1000
1139 static int64_t delta_min
= INT64_MAX
;
1140 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1142 ti
= qemu_get_clock(vm_clock
);
1143 if (last_clock
!= 0) {
1144 delta
= ti
- last_clock
;
1145 if (delta
< delta_min
)
1147 if (delta
> delta_max
)
1150 if (++count
== DISP_FREQ
) {
1151 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1152 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1153 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1154 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1155 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1157 delta_min
= INT64_MAX
;
1165 if (alarm_has_dynticks(alarm_timer
) ||
1167 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1168 qemu_get_clock(vm_clock
))) ||
1169 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1170 qemu_get_clock(rt_clock
)) ||
1171 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1172 qemu_get_clock(host_clock
))) {
1173 qemu_event_increment();
1174 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1176 #ifndef CONFIG_IOTHREAD
1178 /* stop the currently executing cpu because a timer occured */
1182 timer_alarm_pending
= 1;
1183 qemu_notify_event();
1187 static int64_t qemu_next_deadline(void)
1189 /* To avoid problems with overflow limit this to 2^32. */
1190 int64_t delta
= INT32_MAX
;
1192 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1193 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1194 qemu_get_clock(vm_clock
);
1196 if (active_timers
[QEMU_CLOCK_HOST
]) {
1197 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1198 qemu_get_clock(host_clock
);
1209 #if defined(__linux__)
1210 static uint64_t qemu_next_deadline_dyntick(void)
1218 delta
= (qemu_next_deadline() + 999) / 1000;
1220 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1221 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1222 qemu_get_clock(rt_clock
))*1000;
1223 if (rtdelta
< delta
)
1227 if (delta
< MIN_TIMER_REARM_US
)
1228 delta
= MIN_TIMER_REARM_US
;
1236 /* Sets a specific flag */
1237 static int fcntl_setfl(int fd
, int flag
)
1241 flags
= fcntl(fd
, F_GETFL
);
1245 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1251 #if defined(__linux__)
1253 #define RTC_FREQ 1024
1255 static void enable_sigio_timer(int fd
)
1257 struct sigaction act
;
1260 sigfillset(&act
.sa_mask
);
1262 act
.sa_handler
= host_alarm_handler
;
1264 sigaction(SIGIO
, &act
, NULL
);
1265 fcntl_setfl(fd
, O_ASYNC
);
1266 fcntl(fd
, F_SETOWN
, getpid());
1269 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1271 struct hpet_info info
;
1274 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1279 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1281 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1282 "error, but for better emulation accuracy type:\n"
1283 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1287 /* Check capabilities */
1288 r
= ioctl(fd
, HPET_INFO
, &info
);
1292 /* Enable periodic mode */
1293 r
= ioctl(fd
, HPET_EPI
, 0);
1294 if (info
.hi_flags
&& (r
< 0))
1297 /* Enable interrupt */
1298 r
= ioctl(fd
, HPET_IE_ON
, 0);
1302 enable_sigio_timer(fd
);
1303 t
->priv
= (void *)(long)fd
;
1311 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1313 int fd
= (long)t
->priv
;
1318 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1321 unsigned long current_rtc_freq
= 0;
1323 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1326 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1327 if (current_rtc_freq
!= RTC_FREQ
&&
1328 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1329 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1330 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1331 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1334 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1340 enable_sigio_timer(rtc_fd
);
1342 t
->priv
= (void *)(long)rtc_fd
;
1347 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1349 int rtc_fd
= (long)t
->priv
;
1354 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1358 struct sigaction act
;
1360 sigfillset(&act
.sa_mask
);
1362 act
.sa_handler
= host_alarm_handler
;
1364 sigaction(SIGALRM
, &act
, NULL
);
1367 * Initialize ev struct to 0 to avoid valgrind complaining
1368 * about uninitialized data in timer_create call
1370 memset(&ev
, 0, sizeof(ev
));
1371 ev
.sigev_value
.sival_int
= 0;
1372 ev
.sigev_notify
= SIGEV_SIGNAL
;
1373 ev
.sigev_signo
= SIGALRM
;
1375 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1376 perror("timer_create");
1378 /* disable dynticks */
1379 fprintf(stderr
, "Dynamic Ticks disabled\n");
1384 t
->priv
= (void *)(long)host_timer
;
1389 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1391 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1393 timer_delete(host_timer
);
1396 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1398 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1399 struct itimerspec timeout
;
1400 int64_t nearest_delta_us
= INT64_MAX
;
1403 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1404 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1405 !active_timers
[QEMU_CLOCK_HOST
])
1408 nearest_delta_us
= qemu_next_deadline_dyntick();
1410 /* check whether a timer is already running */
1411 if (timer_gettime(host_timer
, &timeout
)) {
1413 fprintf(stderr
, "Internal timer error: aborting\n");
1416 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1417 if (current_us
&& current_us
<= nearest_delta_us
)
1420 timeout
.it_interval
.tv_sec
= 0;
1421 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1422 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1423 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1424 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1426 fprintf(stderr
, "Internal timer error: aborting\n");
1431 #endif /* defined(__linux__) */
1433 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1435 struct sigaction act
;
1436 struct itimerval itv
;
1440 sigfillset(&act
.sa_mask
);
1442 act
.sa_handler
= host_alarm_handler
;
1444 sigaction(SIGALRM
, &act
, NULL
);
1446 itv
.it_interval
.tv_sec
= 0;
1447 /* for i386 kernel 2.6 to get 1 ms */
1448 itv
.it_interval
.tv_usec
= 999;
1449 itv
.it_value
.tv_sec
= 0;
1450 itv
.it_value
.tv_usec
= 10 * 1000;
1452 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1459 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1461 struct itimerval itv
;
1463 memset(&itv
, 0, sizeof(itv
));
1464 setitimer(ITIMER_REAL
, &itv
, NULL
);
1467 #endif /* !defined(_WIN32) */
1472 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1475 struct qemu_alarm_win32
*data
= t
->priv
;
1478 memset(&tc
, 0, sizeof(tc
));
1479 timeGetDevCaps(&tc
, sizeof(tc
));
1481 if (data
->period
< tc
.wPeriodMin
)
1482 data
->period
= tc
.wPeriodMin
;
1484 timeBeginPeriod(data
->period
);
1486 flags
= TIME_CALLBACK_FUNCTION
;
1487 if (alarm_has_dynticks(t
))
1488 flags
|= TIME_ONESHOT
;
1490 flags
|= TIME_PERIODIC
;
1492 data
->timerId
= timeSetEvent(1, // interval (ms)
1493 data
->period
, // resolution
1494 host_alarm_handler
, // function
1495 (DWORD
)t
, // parameter
1498 if (!data
->timerId
) {
1499 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1501 timeEndPeriod(data
->period
);
1508 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1510 struct qemu_alarm_win32
*data
= t
->priv
;
1512 timeKillEvent(data
->timerId
);
1513 timeEndPeriod(data
->period
);
1516 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1518 struct qemu_alarm_win32
*data
= t
->priv
;
1520 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1521 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1522 !active_timers
[QEMU_CLOCK_HOST
])
1525 timeKillEvent(data
->timerId
);
1527 data
->timerId
= timeSetEvent(1,
1531 TIME_ONESHOT
| TIME_PERIODIC
);
1533 if (!data
->timerId
) {
1534 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1537 timeEndPeriod(data
->period
);
1544 static int init_timer_alarm(void)
1546 struct qemu_alarm_timer
*t
= NULL
;
1549 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1550 t
= &alarm_timers
[i
];
1570 static void quit_timers(void)
1572 alarm_timer
->stop(alarm_timer
);
1576 /***********************************************************/
1577 /* host time/date access */
1578 void qemu_get_timedate(struct tm
*tm
, int offset
)
1585 if (rtc_date_offset
== -1) {
1589 ret
= localtime(&ti
);
1591 ti
-= rtc_date_offset
;
1595 memcpy(tm
, ret
, sizeof(struct tm
));
1598 int qemu_timedate_diff(struct tm
*tm
)
1602 if (rtc_date_offset
== -1)
1604 seconds
= mktimegm(tm
);
1606 seconds
= mktime(tm
);
1608 seconds
= mktimegm(tm
) + rtc_date_offset
;
1610 return seconds
- time(NULL
);
1613 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1615 time_t rtc_start_date
;
1618 if (!strcmp(startdate
, "now") && legacy
) {
1619 rtc_date_offset
= -1;
1621 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1629 } else if (sscanf(startdate
, "%d-%d-%d",
1632 &tm
.tm_mday
) == 3) {
1641 rtc_start_date
= mktimegm(&tm
);
1642 if (rtc_start_date
== -1) {
1644 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1645 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1648 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1652 static void configure_rtc(QemuOpts
*opts
)
1656 value
= qemu_opt_get(opts
, "base");
1658 if (!strcmp(value
, "utc")) {
1660 } else if (!strcmp(value
, "localtime")) {
1663 configure_rtc_date_offset(value
, 0);
1666 value
= qemu_opt_get(opts
, "clock");
1668 if (!strcmp(value
, "host")) {
1669 rtc_clock
= host_clock
;
1670 } else if (!strcmp(value
, "vm")) {
1671 rtc_clock
= vm_clock
;
1673 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1677 #ifdef CONFIG_TARGET_I386
1678 value
= qemu_opt_get(opts
, "driftfix");
1680 if (!strcmp(buf
, "slew")) {
1682 } else if (!strcmp(buf
, "none")) {
1685 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1693 static void socket_cleanup(void)
1698 static int socket_init(void)
1703 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1705 err
= WSAGetLastError();
1706 fprintf(stderr
, "WSAStartup: %d\n", err
);
1709 atexit(socket_cleanup
);
1714 /***********************************************************/
1715 /* Bluetooth support */
1718 static struct HCIInfo
*hci_table
[MAX_NICS
];
1720 static struct bt_vlan_s
{
1721 struct bt_scatternet_s net
;
1723 struct bt_vlan_s
*next
;
1726 /* find or alloc a new bluetooth "VLAN" */
1727 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1729 struct bt_vlan_s
**pvlan
, *vlan
;
1730 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1734 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1736 pvlan
= &first_bt_vlan
;
1737 while (*pvlan
!= NULL
)
1738 pvlan
= &(*pvlan
)->next
;
1743 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1747 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1752 static struct HCIInfo null_hci
= {
1753 .cmd_send
= null_hci_send
,
1754 .sco_send
= null_hci_send
,
1755 .acl_send
= null_hci_send
,
1756 .bdaddr_set
= null_hci_addr_set
,
1759 struct HCIInfo
*qemu_next_hci(void)
1761 if (cur_hci
== nb_hcis
)
1764 return hci_table
[cur_hci
++];
1767 static struct HCIInfo
*hci_init(const char *str
)
1770 struct bt_scatternet_s
*vlan
= 0;
1772 if (!strcmp(str
, "null"))
1775 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1777 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1778 else if (!strncmp(str
, "hci", 3)) {
1781 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1782 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1787 vlan
= qemu_find_bt_vlan(0);
1789 return bt_new_hci(vlan
);
1792 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1797 static int bt_hci_parse(const char *str
)
1799 struct HCIInfo
*hci
;
1802 if (nb_hcis
>= MAX_NICS
) {
1803 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1807 hci
= hci_init(str
);
1816 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1817 hci
->bdaddr_set(hci
, bdaddr
.b
);
1819 hci_table
[nb_hcis
++] = hci
;
1824 static void bt_vhci_add(int vlan_id
)
1826 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1829 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1830 "an empty scatternet %i\n", vlan_id
);
1832 bt_vhci_init(bt_new_hci(vlan
));
1835 static struct bt_device_s
*bt_device_add(const char *opt
)
1837 struct bt_scatternet_s
*vlan
;
1839 char *endp
= strstr(opt
, ",vlan=");
1840 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1843 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1846 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1848 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1853 vlan
= qemu_find_bt_vlan(vlan_id
);
1856 fprintf(stderr
, "qemu: warning: adding a slave device to "
1857 "an empty scatternet %i\n", vlan_id
);
1859 if (!strcmp(devname
, "keyboard"))
1860 return bt_keyboard_init(vlan
);
1862 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1866 static int bt_parse(const char *opt
)
1868 const char *endp
, *p
;
1871 if (strstart(opt
, "hci", &endp
)) {
1872 if (!*endp
|| *endp
== ',') {
1874 if (!strstart(endp
, ",vlan=", 0))
1877 return bt_hci_parse(opt
);
1879 } else if (strstart(opt
, "vhci", &endp
)) {
1880 if (!*endp
|| *endp
== ',') {
1882 if (strstart(endp
, ",vlan=", &p
)) {
1883 vlan
= strtol(p
, (char **) &endp
, 0);
1885 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1889 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1898 } else if (strstart(opt
, "device:", &endp
))
1899 return !bt_device_add(endp
);
1901 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1905 /***********************************************************/
1906 /* QEMU Block devices */
1908 #define HD_ALIAS "index=%d,media=disk"
1909 #define CDROM_ALIAS "index=2,media=cdrom"
1910 #define FD_ALIAS "index=%d,if=floppy"
1911 #define PFLASH_ALIAS "if=pflash"
1912 #define MTD_ALIAS "if=mtd"
1913 #define SD_ALIAS "index=0,if=sd"
1915 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1922 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1925 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1927 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1928 __FUNCTION__
, optstr
);
1932 qemu_opt_set(opts
, "file", file
);
1936 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1940 /* seek interface, bus and unit */
1942 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1943 if (dinfo
->type
== type
&&
1944 dinfo
->bus
== bus
&&
1945 dinfo
->unit
== unit
)
1952 DriveInfo
*drive_get_by_id(const char *id
)
1956 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1957 if (strcmp(id
, dinfo
->id
))
1964 int drive_get_max_bus(BlockInterfaceType type
)
1970 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1971 if(dinfo
->type
== type
&&
1972 dinfo
->bus
> max_bus
)
1973 max_bus
= dinfo
->bus
;
1978 const char *drive_get_serial(BlockDriverState
*bdrv
)
1982 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1983 if (dinfo
->bdrv
== bdrv
)
1984 return dinfo
->serial
;
1990 BlockInterfaceErrorAction
drive_get_on_error(
1991 BlockDriverState
*bdrv
, int is_read
)
1996 return BLOCK_ERR_REPORT
;
1999 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
2000 if (dinfo
->bdrv
== bdrv
)
2001 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
2004 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
2007 static void bdrv_format_print(void *opaque
, const char *name
)
2009 fprintf(stderr
, " %s", name
);
2012 void drive_uninit(DriveInfo
*dinfo
)
2014 qemu_opts_del(dinfo
->opts
);
2015 bdrv_delete(dinfo
->bdrv
);
2016 QTAILQ_REMOVE(&drives
, dinfo
, next
);
2020 static int parse_block_error_action(const char *buf
, int is_read
)
2022 if (!strcmp(buf
, "ignore")) {
2023 return BLOCK_ERR_IGNORE
;
2024 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
2025 return BLOCK_ERR_STOP_ENOSPC
;
2026 } else if (!strcmp(buf
, "stop")) {
2027 return BLOCK_ERR_STOP_ANY
;
2028 } else if (!strcmp(buf
, "report")) {
2029 return BLOCK_ERR_REPORT
;
2031 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
2032 buf
, is_read
? "read" : "write");
2037 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
2041 const char *file
= NULL
;
2044 const char *mediastr
= "";
2045 BlockInterfaceType type
;
2046 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2047 int bus_id
, unit_id
;
2048 int cyls
, heads
, secs
, translation
;
2049 BlockDriver
*drv
= NULL
;
2050 QEMUMachine
*machine
= opaque
;
2057 int on_read_error
, on_write_error
;
2058 const char *devaddr
;
2064 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2067 if (machine
&& machine
->use_scsi
) {
2069 max_devs
= MAX_SCSI_DEVS
;
2070 pstrcpy(devname
, sizeof(devname
), "scsi");
2073 max_devs
= MAX_IDE_DEVS
;
2074 pstrcpy(devname
, sizeof(devname
), "ide");
2078 /* extract parameters */
2079 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2080 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2081 index
= qemu_opt_get_number(opts
, "index", -1);
2083 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2084 heads
= qemu_opt_get_number(opts
, "heads", 0);
2085 secs
= qemu_opt_get_number(opts
, "secs", 0);
2087 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2088 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2090 file
= qemu_opt_get(opts
, "file");
2091 serial
= qemu_opt_get(opts
, "serial");
2093 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2094 pstrcpy(devname
, sizeof(devname
), buf
);
2095 if (!strcmp(buf
, "ide")) {
2097 max_devs
= MAX_IDE_DEVS
;
2098 } else if (!strcmp(buf
, "scsi")) {
2100 max_devs
= MAX_SCSI_DEVS
;
2101 } else if (!strcmp(buf
, "floppy")) {
2104 } else if (!strcmp(buf
, "pflash")) {
2107 } else if (!strcmp(buf
, "mtd")) {
2110 } else if (!strcmp(buf
, "sd")) {
2113 } else if (!strcmp(buf
, "virtio")) {
2116 } else if (!strcmp(buf
, "xen")) {
2119 } else if (!strcmp(buf
, "none")) {
2123 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2128 if (cyls
|| heads
|| secs
) {
2129 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2130 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2133 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2134 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2137 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2138 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2143 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2146 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2150 if (!strcmp(buf
, "none"))
2151 translation
= BIOS_ATA_TRANSLATION_NONE
;
2152 else if (!strcmp(buf
, "lba"))
2153 translation
= BIOS_ATA_TRANSLATION_LBA
;
2154 else if (!strcmp(buf
, "auto"))
2155 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2157 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2162 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2163 if (!strcmp(buf
, "disk")) {
2165 } else if (!strcmp(buf
, "cdrom")) {
2166 if (cyls
|| secs
|| heads
) {
2168 "qemu: '%s' invalid physical CHS format\n", buf
);
2171 media
= MEDIA_CDROM
;
2173 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2178 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2179 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2181 else if (!strcmp(buf
, "writethrough"))
2183 else if (!strcmp(buf
, "writeback"))
2186 fprintf(stderr
, "qemu: invalid cache option\n");
2191 #ifdef CONFIG_LINUX_AIO
2192 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2193 if (!strcmp(buf
, "threads"))
2195 else if (!strcmp(buf
, "native"))
2198 fprintf(stderr
, "qemu: invalid aio option\n");
2204 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2205 if (strcmp(buf
, "?") == 0) {
2206 fprintf(stderr
, "qemu: Supported formats:");
2207 bdrv_iterate_format(bdrv_format_print
, NULL
);
2208 fprintf(stderr
, "\n");
2211 drv
= bdrv_find_whitelisted_format(buf
);
2213 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2218 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2219 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2220 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2221 fprintf(stderr
, "werror is no supported by this format\n");
2225 on_write_error
= parse_block_error_action(buf
, 0);
2226 if (on_write_error
< 0) {
2231 on_read_error
= BLOCK_ERR_REPORT
;
2232 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2233 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2234 fprintf(stderr
, "rerror is no supported by this format\n");
2238 on_read_error
= parse_block_error_action(buf
, 1);
2239 if (on_read_error
< 0) {
2244 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2245 if (type
!= IF_VIRTIO
) {
2246 fprintf(stderr
, "addr is not supported\n");
2251 /* compute bus and unit according index */
2254 if (bus_id
!= 0 || unit_id
!= -1) {
2256 "qemu: index cannot be used with bus and unit\n");
2264 unit_id
= index
% max_devs
;
2265 bus_id
= index
/ max_devs
;
2269 /* if user doesn't specify a unit_id,
2270 * try to find the first free
2273 if (unit_id
== -1) {
2275 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2277 if (max_devs
&& unit_id
>= max_devs
) {
2278 unit_id
-= max_devs
;
2286 if (max_devs
&& unit_id
>= max_devs
) {
2287 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2288 unit_id
, max_devs
- 1);
2293 * ignore multiple definitions
2296 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2303 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2304 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2305 dinfo
->id
= qemu_strdup(buf
);
2307 /* no id supplied -> create one */
2308 dinfo
->id
= qemu_mallocz(32);
2309 if (type
== IF_IDE
|| type
== IF_SCSI
)
2310 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2312 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2313 devname
, bus_id
, mediastr
, unit_id
);
2315 snprintf(dinfo
->id
, 32, "%s%s%i",
2316 devname
, mediastr
, unit_id
);
2318 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2319 dinfo
->devaddr
= devaddr
;
2321 dinfo
->bus
= bus_id
;
2322 dinfo
->unit
= unit_id
;
2323 dinfo
->on_read_error
= on_read_error
;
2324 dinfo
->on_write_error
= on_write_error
;
2327 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2328 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2338 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2339 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2343 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2348 /* FIXME: This isn't really a floppy, but it's a reasonable
2351 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2357 /* add virtio block device */
2358 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2359 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2360 qemu_opt_set(opts
, "drive", dinfo
->id
);
2362 qemu_opt_set(opts
, "addr", devaddr
);
2373 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2374 cache
= 2; /* always use write-back with snapshot */
2376 if (cache
== 0) /* no caching */
2377 bdrv_flags
|= BDRV_O_NOCACHE
;
2378 else if (cache
== 2) /* write-back */
2379 bdrv_flags
|= BDRV_O_CACHE_WB
;
2382 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2384 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2388 if (type
== IF_IDE
) {
2389 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2392 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2395 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2396 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2397 file
, strerror(errno
));
2401 if (bdrv_key_required(dinfo
->bdrv
))
2407 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2409 QEMUMachine
*machine
= opaque
;
2410 int fatal_error
= 0;
2412 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2419 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2421 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2422 qemu_opt_set(opts
, "snapshot", "on");
2427 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2429 boot_set_handler
= func
;
2430 boot_set_opaque
= opaque
;
2433 int qemu_boot_set(const char *boot_devices
)
2435 if (!boot_set_handler
) {
2438 return boot_set_handler(boot_set_opaque
, boot_devices
);
2441 static int parse_bootdevices(char *devices
)
2443 /* We just do some generic consistency checks */
2447 for (p
= devices
; *p
!= '\0'; p
++) {
2448 /* Allowed boot devices are:
2449 * a-b: floppy disk drives
2450 * c-f: IDE disk drives
2451 * g-m: machine implementation dependant drives
2452 * n-p: network devices
2453 * It's up to each machine implementation to check if the given boot
2454 * devices match the actual hardware implementation and firmware
2457 if (*p
< 'a' || *p
> 'p') {
2458 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2461 if (bitmap
& (1 << (*p
- 'a'))) {
2462 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2465 bitmap
|= 1 << (*p
- 'a');
2470 static void restore_boot_devices(void *opaque
)
2472 char *standard_boot_devices
= opaque
;
2474 qemu_boot_set(standard_boot_devices
);
2476 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2477 qemu_free(standard_boot_devices
);
2480 static void numa_add(const char *optarg
)
2484 unsigned long long value
, endvalue
;
2487 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2488 if (!strcmp(option
, "node")) {
2489 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2490 nodenr
= nb_numa_nodes
;
2492 nodenr
= strtoull(option
, NULL
, 10);
2495 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2496 node_mem
[nodenr
] = 0;
2498 value
= strtoull(option
, &endptr
, 0);
2500 case 0: case 'M': case 'm':
2507 node_mem
[nodenr
] = value
;
2509 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2510 node_cpumask
[nodenr
] = 0;
2512 value
= strtoull(option
, &endptr
, 10);
2515 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2517 if (*endptr
== '-') {
2518 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2519 if (endvalue
>= 63) {
2522 "only 63 CPUs in NUMA mode supported.\n");
2524 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2529 node_cpumask
[nodenr
] = value
;
2536 static void smp_parse(const char *optarg
)
2538 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2542 smp
= strtoul(optarg
, &endptr
, 10);
2543 if (endptr
!= optarg
) {
2544 if (*endptr
== ',') {
2548 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2549 sockets
= strtoull(option
, NULL
, 10);
2550 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2551 cores
= strtoull(option
, NULL
, 10);
2552 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2553 threads
= strtoull(option
, NULL
, 10);
2554 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2555 max_cpus
= strtoull(option
, NULL
, 10);
2557 /* compute missing values, prefer sockets over cores over threads */
2558 if (smp
== 0 || sockets
== 0) {
2559 sockets
= sockets
> 0 ? sockets
: 1;
2560 cores
= cores
> 0 ? cores
: 1;
2561 threads
= threads
> 0 ? threads
: 1;
2563 smp
= cores
* threads
* sockets
;
2565 sockets
= smp
/ (cores
* threads
);
2569 threads
= threads
> 0 ? threads
: 1;
2570 cores
= smp
/ (sockets
* threads
);
2573 sockets
= smp
/ (cores
* threads
);
2575 threads
= smp
/ (cores
* sockets
);
2580 smp_cores
= cores
> 0 ? cores
: 1;
2581 smp_threads
= threads
> 0 ? threads
: 1;
2583 max_cpus
= smp_cpus
;
2586 /***********************************************************/
2589 static int usb_device_add(const char *devname
, int is_hotplug
)
2592 USBDevice
*dev
= NULL
;
2597 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2598 dev
= usbdevice_create(devname
);
2602 /* the other ones */
2603 if (strstart(devname
, "host:", &p
)) {
2604 dev
= usb_host_device_open(p
);
2605 } else if (strstart(devname
, "net:", &p
)) {
2609 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2614 qemu_opt_set(opts
, "type", "nic");
2615 qemu_opt_set(opts
, "model", "usb");
2617 idx
= net_client_init(NULL
, opts
, 0);
2622 dev
= usb_net_init(&nd_table
[idx
]);
2623 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2624 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2625 bt_new_hci(qemu_find_bt_vlan(0)));
2636 static int usb_device_del(const char *devname
)
2641 if (strstart(devname
, "host:", &p
))
2642 return usb_host_device_close(p
);
2647 p
= strchr(devname
, '.');
2650 bus_num
= strtoul(devname
, NULL
, 0);
2651 addr
= strtoul(p
+ 1, NULL
, 0);
2653 return usb_device_delete_addr(bus_num
, addr
);
2656 static int usb_parse(const char *cmdline
)
2658 return usb_device_add(cmdline
, 0);
2661 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2663 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2666 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2668 usb_device_del(qdict_get_str(qdict
, "devname"));
2671 /***********************************************************/
2672 /* PCMCIA/Cardbus */
2674 static struct pcmcia_socket_entry_s
{
2675 PCMCIASocket
*socket
;
2676 struct pcmcia_socket_entry_s
*next
;
2677 } *pcmcia_sockets
= 0;
2679 void pcmcia_socket_register(PCMCIASocket
*socket
)
2681 struct pcmcia_socket_entry_s
*entry
;
2683 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2684 entry
->socket
= socket
;
2685 entry
->next
= pcmcia_sockets
;
2686 pcmcia_sockets
= entry
;
2689 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2691 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2693 ptr
= &pcmcia_sockets
;
2694 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2695 if (entry
->socket
== socket
) {
2701 void pcmcia_info(Monitor
*mon
)
2703 struct pcmcia_socket_entry_s
*iter
;
2705 if (!pcmcia_sockets
)
2706 monitor_printf(mon
, "No PCMCIA sockets\n");
2708 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2709 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2710 iter
->socket
->attached
? iter
->socket
->card_string
:
2714 /***********************************************************/
2715 /* register display */
2717 struct DisplayAllocator default_allocator
= {
2718 defaultallocator_create_displaysurface
,
2719 defaultallocator_resize_displaysurface
,
2720 defaultallocator_free_displaysurface
2723 void register_displaystate(DisplayState
*ds
)
2733 DisplayState
*get_displaystate(void)
2735 return display_state
;
2738 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2740 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2741 return ds
->allocator
;
2746 static void dumb_display_init(void)
2748 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2749 ds
->allocator
= &default_allocator
;
2750 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2751 register_displaystate(ds
);
2754 /***********************************************************/
2757 typedef struct IOHandlerRecord
{
2759 IOCanRWHandler
*fd_read_poll
;
2761 IOHandler
*fd_write
;
2764 /* temporary data */
2766 struct IOHandlerRecord
*next
;
2769 static IOHandlerRecord
*first_io_handler
;
2771 /* XXX: fd_read_poll should be suppressed, but an API change is
2772 necessary in the character devices to suppress fd_can_read(). */
2773 int qemu_set_fd_handler2(int fd
,
2774 IOCanRWHandler
*fd_read_poll
,
2776 IOHandler
*fd_write
,
2779 IOHandlerRecord
**pioh
, *ioh
;
2781 if (!fd_read
&& !fd_write
) {
2782 pioh
= &first_io_handler
;
2787 if (ioh
->fd
== fd
) {
2794 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2798 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2799 ioh
->next
= first_io_handler
;
2800 first_io_handler
= ioh
;
2803 ioh
->fd_read_poll
= fd_read_poll
;
2804 ioh
->fd_read
= fd_read
;
2805 ioh
->fd_write
= fd_write
;
2806 ioh
->opaque
= opaque
;
2812 int qemu_set_fd_handler(int fd
,
2814 IOHandler
*fd_write
,
2817 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2821 /***********************************************************/
2822 /* Polling handling */
2824 typedef struct PollingEntry
{
2827 struct PollingEntry
*next
;
2830 static PollingEntry
*first_polling_entry
;
2832 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2834 PollingEntry
**ppe
, *pe
;
2835 pe
= qemu_mallocz(sizeof(PollingEntry
));
2837 pe
->opaque
= opaque
;
2838 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2843 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2845 PollingEntry
**ppe
, *pe
;
2846 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2848 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2856 /***********************************************************/
2857 /* Wait objects support */
2858 typedef struct WaitObjects
{
2860 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2861 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2862 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2865 static WaitObjects wait_objects
= {0};
2867 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2869 WaitObjects
*w
= &wait_objects
;
2871 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2873 w
->events
[w
->num
] = handle
;
2874 w
->func
[w
->num
] = func
;
2875 w
->opaque
[w
->num
] = opaque
;
2880 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2883 WaitObjects
*w
= &wait_objects
;
2886 for (i
= 0; i
< w
->num
; i
++) {
2887 if (w
->events
[i
] == handle
)
2890 w
->events
[i
] = w
->events
[i
+ 1];
2891 w
->func
[i
] = w
->func
[i
+ 1];
2892 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2900 /***********************************************************/
2901 /* ram save/restore */
2903 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2904 #define RAM_SAVE_FLAG_COMPRESS 0x02
2905 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2906 #define RAM_SAVE_FLAG_PAGE 0x08
2907 #define RAM_SAVE_FLAG_EOS 0x10
2909 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2911 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2912 uint32_t *array
= (uint32_t *)page
;
2915 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2916 if (array
[i
] != val
)
2923 static int ram_save_block(QEMUFile
*f
)
2925 static ram_addr_t current_addr
= 0;
2926 ram_addr_t saved_addr
= current_addr
;
2927 ram_addr_t addr
= 0;
2930 while (addr
< last_ram_offset
) {
2931 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2934 cpu_physical_memory_reset_dirty(current_addr
,
2935 current_addr
+ TARGET_PAGE_SIZE
,
2936 MIGRATION_DIRTY_FLAG
);
2938 p
= qemu_get_ram_ptr(current_addr
);
2940 if (is_dup_page(p
, *p
)) {
2941 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2942 qemu_put_byte(f
, *p
);
2944 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2945 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2951 addr
+= TARGET_PAGE_SIZE
;
2952 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2958 static uint64_t bytes_transferred
;
2960 static ram_addr_t
ram_save_remaining(void)
2963 ram_addr_t count
= 0;
2965 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2966 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2973 uint64_t ram_bytes_remaining(void)
2975 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2978 uint64_t ram_bytes_transferred(void)
2980 return bytes_transferred
;
2983 uint64_t ram_bytes_total(void)
2985 return last_ram_offset
;
2988 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2991 uint64_t bytes_transferred_last
;
2993 uint64_t expected_time
= 0;
2996 cpu_physical_memory_set_dirty_tracking(0);
3000 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3001 qemu_file_set_error(f
);
3006 bytes_transferred
= 0;
3008 /* Make sure all dirty bits are set */
3009 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3010 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3011 cpu_physical_memory_set_dirty(addr
);
3014 /* Enable dirty memory tracking */
3015 cpu_physical_memory_set_dirty_tracking(1);
3017 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3020 bytes_transferred_last
= bytes_transferred
;
3021 bwidth
= get_clock();
3023 while (!qemu_file_rate_limit(f
)) {
3026 ret
= ram_save_block(f
);
3027 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3028 if (ret
== 0) /* no more blocks */
3032 bwidth
= get_clock() - bwidth
;
3033 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3035 /* if we haven't transferred anything this round, force expected_time to a
3036 * a very high value, but without crashing */
3040 /* try transferring iterative blocks of memory */
3042 /* flush all remaining blocks regardless of rate limiting */
3043 while (ram_save_block(f
) != 0) {
3044 bytes_transferred
+= TARGET_PAGE_SIZE
;
3046 cpu_physical_memory_set_dirty_tracking(0);
3049 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3051 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3053 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3056 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3061 if (version_id
!= 3)
3065 addr
= qemu_get_be64(f
);
3067 flags
= addr
& ~TARGET_PAGE_MASK
;
3068 addr
&= TARGET_PAGE_MASK
;
3070 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3071 if (addr
!= last_ram_offset
)
3075 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3076 uint8_t ch
= qemu_get_byte(f
);
3077 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3080 (!kvm_enabled() || kvm_has_sync_mmu())) {
3081 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3084 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
3085 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3087 if (qemu_file_has_error(f
)) {
3090 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3095 void qemu_service_io(void)
3097 qemu_notify_event();
3100 /***********************************************************/
3101 /* machine registration */
3103 static QEMUMachine
*first_machine
= NULL
;
3104 QEMUMachine
*current_machine
= NULL
;
3106 int qemu_register_machine(QEMUMachine
*m
)
3109 pm
= &first_machine
;
3117 static QEMUMachine
*find_machine(const char *name
)
3121 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3122 if (!strcmp(m
->name
, name
))
3124 if (m
->alias
&& !strcmp(m
->alias
, name
))
3130 static QEMUMachine
*find_default_machine(void)
3134 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3135 if (m
->is_default
) {
3142 /***********************************************************/
3143 /* main execution loop */
3145 static void gui_update(void *opaque
)
3147 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3148 DisplayState
*ds
= opaque
;
3149 DisplayChangeListener
*dcl
= ds
->listeners
;
3153 while (dcl
!= NULL
) {
3154 if (dcl
->gui_timer_interval
&&
3155 dcl
->gui_timer_interval
< interval
)
3156 interval
= dcl
->gui_timer_interval
;
3159 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3162 static void nographic_update(void *opaque
)
3164 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3166 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3169 struct vm_change_state_entry
{
3170 VMChangeStateHandler
*cb
;
3172 QLIST_ENTRY (vm_change_state_entry
) entries
;
3175 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3177 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3180 VMChangeStateEntry
*e
;
3182 e
= qemu_mallocz(sizeof (*e
));
3186 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3190 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3192 QLIST_REMOVE (e
, entries
);
3196 static void vm_state_notify(int running
, int reason
)
3198 VMChangeStateEntry
*e
;
3200 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3201 e
->cb(e
->opaque
, running
, reason
);
3205 static void resume_all_vcpus(void);
3206 static void pause_all_vcpus(void);
3213 vm_state_notify(1, 0);
3214 qemu_rearm_alarm_timer(alarm_timer
);
3219 /* reset/shutdown handler */
3221 typedef struct QEMUResetEntry
{
3222 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3223 QEMUResetHandler
*func
;
3227 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3228 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3229 static int reset_requested
;
3230 static int shutdown_requested
;
3231 static int powerdown_requested
;
3232 static int debug_requested
;
3233 static int vmstop_requested
;
3235 int qemu_shutdown_requested(void)
3237 int r
= shutdown_requested
;
3238 shutdown_requested
= 0;
3242 int qemu_reset_requested(void)
3244 int r
= reset_requested
;
3245 reset_requested
= 0;
3249 int qemu_powerdown_requested(void)
3251 int r
= powerdown_requested
;
3252 powerdown_requested
= 0;
3256 static int qemu_debug_requested(void)
3258 int r
= debug_requested
;
3259 debug_requested
= 0;
3263 static int qemu_vmstop_requested(void)
3265 int r
= vmstop_requested
;
3266 vmstop_requested
= 0;
3270 static void do_vm_stop(int reason
)
3273 cpu_disable_ticks();
3276 vm_state_notify(0, reason
);
3280 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3282 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3285 re
->opaque
= opaque
;
3286 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3289 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3293 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3294 if (re
->func
== func
&& re
->opaque
== opaque
) {
3295 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3302 void qemu_system_reset(void)
3304 QEMUResetEntry
*re
, *nre
;
3306 /* reset all devices */
3307 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3308 re
->func(re
->opaque
);
3312 void qemu_system_reset_request(void)
3315 shutdown_requested
= 1;
3317 reset_requested
= 1;
3319 qemu_notify_event();
3322 void qemu_system_shutdown_request(void)
3324 shutdown_requested
= 1;
3325 qemu_notify_event();
3328 void qemu_system_powerdown_request(void)
3330 powerdown_requested
= 1;
3331 qemu_notify_event();
3334 #ifdef CONFIG_IOTHREAD
3335 static void qemu_system_vmstop_request(int reason
)
3337 vmstop_requested
= reason
;
3338 qemu_notify_event();
3343 static int io_thread_fd
= -1;
3345 static void qemu_event_increment(void)
3347 static const char byte
= 0;
3349 if (io_thread_fd
== -1)
3352 write(io_thread_fd
, &byte
, sizeof(byte
));
3355 static void qemu_event_read(void *opaque
)
3357 int fd
= (unsigned long)opaque
;
3360 /* Drain the notify pipe */
3363 len
= read(fd
, buffer
, sizeof(buffer
));
3364 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3367 static int qemu_event_init(void)
3372 err
= qemu_pipe(fds
);
3376 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3380 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3384 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3385 (void *)(unsigned long)fds
[0]);
3387 io_thread_fd
= fds
[1];
3396 HANDLE qemu_event_handle
;
3398 static void dummy_event_handler(void *opaque
)
3402 static int qemu_event_init(void)
3404 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3405 if (!qemu_event_handle
) {
3406 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3409 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3413 static void qemu_event_increment(void)
3415 if (!SetEvent(qemu_event_handle
)) {
3416 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3423 static int cpu_can_run(CPUState
*env
)
3432 #ifndef CONFIG_IOTHREAD
3433 static int qemu_init_main_loop(void)
3435 return qemu_event_init();
3438 void qemu_init_vcpu(void *_env
)
3440 CPUState
*env
= _env
;
3444 env
->nr_cores
= smp_cores
;
3445 env
->nr_threads
= smp_threads
;
3449 int qemu_cpu_self(void *env
)
3454 static void resume_all_vcpus(void)
3458 static void pause_all_vcpus(void)
3462 void qemu_cpu_kick(void *env
)
3467 void qemu_notify_event(void)
3469 CPUState
*env
= cpu_single_env
;
3476 void qemu_mutex_lock_iothread(void) {}
3477 void qemu_mutex_unlock_iothread(void) {}
3479 void vm_stop(int reason
)
3484 #else /* CONFIG_IOTHREAD */
3486 #include "qemu-thread.h"
3488 QemuMutex qemu_global_mutex
;
3489 static QemuMutex qemu_fair_mutex
;
3491 static QemuThread io_thread
;
3493 static QemuThread
*tcg_cpu_thread
;
3494 static QemuCond
*tcg_halt_cond
;
3496 static int qemu_system_ready
;
3498 static QemuCond qemu_cpu_cond
;
3500 static QemuCond qemu_system_cond
;
3501 static QemuCond qemu_pause_cond
;
3503 static void block_io_signals(void);
3504 static void unblock_io_signals(void);
3505 static int tcg_has_work(void);
3507 static int qemu_init_main_loop(void)
3511 ret
= qemu_event_init();
3515 qemu_cond_init(&qemu_pause_cond
);
3516 qemu_mutex_init(&qemu_fair_mutex
);
3517 qemu_mutex_init(&qemu_global_mutex
);
3518 qemu_mutex_lock(&qemu_global_mutex
);
3520 unblock_io_signals();
3521 qemu_thread_self(&io_thread
);
3526 static void qemu_wait_io_event(CPUState
*env
)
3528 while (!tcg_has_work())
3529 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3531 qemu_mutex_unlock(&qemu_global_mutex
);
3534 * Users of qemu_global_mutex can be starved, having no chance
3535 * to acquire it since this path will get to it first.
3536 * So use another lock to provide fairness.
3538 qemu_mutex_lock(&qemu_fair_mutex
);
3539 qemu_mutex_unlock(&qemu_fair_mutex
);
3541 qemu_mutex_lock(&qemu_global_mutex
);
3545 qemu_cond_signal(&qemu_pause_cond
);
3549 static int qemu_cpu_exec(CPUState
*env
);
3551 static void *kvm_cpu_thread_fn(void *arg
)
3553 CPUState
*env
= arg
;
3556 qemu_thread_self(env
->thread
);
3560 /* signal CPU creation */
3561 qemu_mutex_lock(&qemu_global_mutex
);
3563 qemu_cond_signal(&qemu_cpu_cond
);
3565 /* and wait for machine initialization */
3566 while (!qemu_system_ready
)
3567 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3570 if (cpu_can_run(env
))
3572 qemu_wait_io_event(env
);
3578 static void tcg_cpu_exec(void);
3580 static void *tcg_cpu_thread_fn(void *arg
)
3582 CPUState
*env
= arg
;
3585 qemu_thread_self(env
->thread
);
3587 /* signal CPU creation */
3588 qemu_mutex_lock(&qemu_global_mutex
);
3589 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3591 qemu_cond_signal(&qemu_cpu_cond
);
3593 /* and wait for machine initialization */
3594 while (!qemu_system_ready
)
3595 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3599 qemu_wait_io_event(cur_cpu
);
3605 void qemu_cpu_kick(void *_env
)
3607 CPUState
*env
= _env
;
3608 qemu_cond_broadcast(env
->halt_cond
);
3610 qemu_thread_signal(env
->thread
, SIGUSR1
);
3613 int qemu_cpu_self(void *_env
)
3615 CPUState
*env
= _env
;
3618 qemu_thread_self(&this);
3620 return qemu_thread_equal(&this, env
->thread
);
3623 static void cpu_signal(int sig
)
3626 cpu_exit(cpu_single_env
);
3629 static void block_io_signals(void)
3632 struct sigaction sigact
;
3635 sigaddset(&set
, SIGUSR2
);
3636 sigaddset(&set
, SIGIO
);
3637 sigaddset(&set
, SIGALRM
);
3638 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3641 sigaddset(&set
, SIGUSR1
);
3642 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3644 memset(&sigact
, 0, sizeof(sigact
));
3645 sigact
.sa_handler
= cpu_signal
;
3646 sigaction(SIGUSR1
, &sigact
, NULL
);
3649 static void unblock_io_signals(void)
3654 sigaddset(&set
, SIGUSR2
);
3655 sigaddset(&set
, SIGIO
);
3656 sigaddset(&set
, SIGALRM
);
3657 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3660 sigaddset(&set
, SIGUSR1
);
3661 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3664 static void qemu_signal_lock(unsigned int msecs
)
3666 qemu_mutex_lock(&qemu_fair_mutex
);
3668 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3669 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3670 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3673 qemu_mutex_unlock(&qemu_fair_mutex
);
3676 void qemu_mutex_lock_iothread(void)
3678 if (kvm_enabled()) {
3679 qemu_mutex_lock(&qemu_fair_mutex
);
3680 qemu_mutex_lock(&qemu_global_mutex
);
3681 qemu_mutex_unlock(&qemu_fair_mutex
);
3683 qemu_signal_lock(100);
3686 void qemu_mutex_unlock_iothread(void)
3688 qemu_mutex_unlock(&qemu_global_mutex
);
3691 static int all_vcpus_paused(void)
3693 CPUState
*penv
= first_cpu
;
3698 penv
= (CPUState
*)penv
->next_cpu
;
3704 static void pause_all_vcpus(void)
3706 CPUState
*penv
= first_cpu
;
3710 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3711 qemu_cpu_kick(penv
);
3712 penv
= (CPUState
*)penv
->next_cpu
;
3715 while (!all_vcpus_paused()) {
3716 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3719 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3720 penv
= (CPUState
*)penv
->next_cpu
;
3725 static void resume_all_vcpus(void)
3727 CPUState
*penv
= first_cpu
;
3732 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3733 qemu_cpu_kick(penv
);
3734 penv
= (CPUState
*)penv
->next_cpu
;
3738 static void tcg_init_vcpu(void *_env
)
3740 CPUState
*env
= _env
;
3741 /* share a single thread for all cpus with TCG */
3742 if (!tcg_cpu_thread
) {
3743 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3744 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3745 qemu_cond_init(env
->halt_cond
);
3746 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3747 while (env
->created
== 0)
3748 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3749 tcg_cpu_thread
= env
->thread
;
3750 tcg_halt_cond
= env
->halt_cond
;
3752 env
->thread
= tcg_cpu_thread
;
3753 env
->halt_cond
= tcg_halt_cond
;
3757 static void kvm_start_vcpu(CPUState
*env
)
3759 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3760 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3761 qemu_cond_init(env
->halt_cond
);
3762 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3763 while (env
->created
== 0)
3764 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3767 void qemu_init_vcpu(void *_env
)
3769 CPUState
*env
= _env
;
3772 kvm_start_vcpu(env
);
3775 env
->nr_cores
= smp_cores
;
3776 env
->nr_threads
= smp_threads
;
3779 void qemu_notify_event(void)
3781 qemu_event_increment();
3784 void vm_stop(int reason
)
3787 qemu_thread_self(&me
);
3789 if (!qemu_thread_equal(&me
, &io_thread
)) {
3790 qemu_system_vmstop_request(reason
);
3792 * FIXME: should not return to device code in case
3793 * vm_stop() has been requested.
3795 if (cpu_single_env
) {
3796 cpu_exit(cpu_single_env
);
3797 cpu_single_env
->stop
= 1;
3808 static void host_main_loop_wait(int *timeout
)
3814 /* XXX: need to suppress polling by better using win32 events */
3816 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3817 ret
|= pe
->func(pe
->opaque
);
3821 WaitObjects
*w
= &wait_objects
;
3823 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3824 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3825 if (w
->func
[ret
- WAIT_OBJECT_0
])
3826 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3828 /* Check for additional signaled events */
3829 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3831 /* Check if event is signaled */
3832 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3833 if(ret2
== WAIT_OBJECT_0
) {
3835 w
->func
[i
](w
->opaque
[i
]);
3836 } else if (ret2
== WAIT_TIMEOUT
) {
3838 err
= GetLastError();
3839 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3842 } else if (ret
== WAIT_TIMEOUT
) {
3844 err
= GetLastError();
3845 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3852 static void host_main_loop_wait(int *timeout
)
3857 void main_loop_wait(int timeout
)
3859 IOHandlerRecord
*ioh
;
3860 fd_set rfds
, wfds
, xfds
;
3864 qemu_bh_update_timeout(&timeout
);
3866 host_main_loop_wait(&timeout
);
3868 /* poll any events */
3869 /* XXX: separate device handlers from system ones */
3874 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3878 (!ioh
->fd_read_poll
||
3879 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3880 FD_SET(ioh
->fd
, &rfds
);
3884 if (ioh
->fd_write
) {
3885 FD_SET(ioh
->fd
, &wfds
);
3891 tv
.tv_sec
= timeout
/ 1000;
3892 tv
.tv_usec
= (timeout
% 1000) * 1000;
3894 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3896 qemu_mutex_unlock_iothread();
3897 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3898 qemu_mutex_lock_iothread();
3900 IOHandlerRecord
**pioh
;
3902 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3903 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3904 ioh
->fd_read(ioh
->opaque
);
3906 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3907 ioh
->fd_write(ioh
->opaque
);
3911 /* remove deleted IO handlers */
3912 pioh
= &first_io_handler
;
3923 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3925 /* rearm timer, if not periodic */
3926 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3927 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3928 qemu_rearm_alarm_timer(alarm_timer
);
3931 /* vm time timers */
3933 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3934 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3935 qemu_get_clock(vm_clock
));
3938 /* real time timers */
3939 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3940 qemu_get_clock(rt_clock
));
3942 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3943 qemu_get_clock(host_clock
));
3945 /* Check bottom-halves last in case any of the earlier events triggered
3951 static int qemu_cpu_exec(CPUState
*env
)
3954 #ifdef CONFIG_PROFILER
3958 #ifdef CONFIG_PROFILER
3959 ti
= profile_getclock();
3964 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3965 env
->icount_decr
.u16
.low
= 0;
3966 env
->icount_extra
= 0;
3967 count
= qemu_next_deadline();
3968 count
= (count
+ (1 << icount_time_shift
) - 1)
3969 >> icount_time_shift
;
3970 qemu_icount
+= count
;
3971 decr
= (count
> 0xffff) ? 0xffff : count
;
3973 env
->icount_decr
.u16
.low
= decr
;
3974 env
->icount_extra
= count
;
3976 ret
= cpu_exec(env
);
3977 #ifdef CONFIG_PROFILER
3978 qemu_time
+= profile_getclock() - ti
;
3981 /* Fold pending instructions back into the
3982 instruction counter, and clear the interrupt flag. */
3983 qemu_icount
-= (env
->icount_decr
.u16
.low
3984 + env
->icount_extra
);
3985 env
->icount_decr
.u32
= 0;
3986 env
->icount_extra
= 0;
3991 static void tcg_cpu_exec(void)
3995 if (next_cpu
== NULL
)
3996 next_cpu
= first_cpu
;
3997 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3998 CPUState
*env
= cur_cpu
= next_cpu
;
4002 if (timer_alarm_pending
) {
4003 timer_alarm_pending
= 0;
4006 if (cpu_can_run(env
))
4007 ret
= qemu_cpu_exec(env
);
4008 if (ret
== EXCP_DEBUG
) {
4009 gdb_set_stop_cpu(env
);
4010 debug_requested
= 1;
4016 static int cpu_has_work(CPUState
*env
)
4024 if (qemu_cpu_has_work(env
))
4029 static int tcg_has_work(void)
4033 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4034 if (cpu_has_work(env
))
4039 static int qemu_calculate_timeout(void)
4041 #ifndef CONFIG_IOTHREAD
4046 else if (tcg_has_work())
4048 else if (!use_icount
)
4051 /* XXX: use timeout computed from timers */
4054 /* Advance virtual time to the next event. */
4055 if (use_icount
== 1) {
4056 /* When not using an adaptive execution frequency
4057 we tend to get badly out of sync with real time,
4058 so just delay for a reasonable amount of time. */
4061 delta
= cpu_get_icount() - cpu_get_clock();
4064 /* If virtual time is ahead of real time then just
4066 timeout
= (delta
/ 1000000) + 1;
4068 /* Wait for either IO to occur or the next
4070 add
= qemu_next_deadline();
4071 /* We advance the timer before checking for IO.
4072 Limit the amount we advance so that early IO
4073 activity won't get the guest too far ahead. */
4077 add
= (add
+ (1 << icount_time_shift
) - 1)
4078 >> icount_time_shift
;
4080 timeout
= delta
/ 1000000;
4087 #else /* CONFIG_IOTHREAD */
4092 static int vm_can_run(void)
4094 if (powerdown_requested
)
4096 if (reset_requested
)
4098 if (shutdown_requested
)
4100 if (debug_requested
)
4105 qemu_irq qemu_system_powerdown
;
4107 static void main_loop(void)
4111 #ifdef CONFIG_IOTHREAD
4112 qemu_system_ready
= 1;
4113 qemu_cond_broadcast(&qemu_system_cond
);
4118 #ifdef CONFIG_PROFILER
4121 #ifndef CONFIG_IOTHREAD
4124 #ifdef CONFIG_PROFILER
4125 ti
= profile_getclock();
4127 main_loop_wait(qemu_calculate_timeout());
4128 #ifdef CONFIG_PROFILER
4129 dev_time
+= profile_getclock() - ti
;
4131 } while (vm_can_run());
4133 if (qemu_debug_requested()) {
4134 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
4135 vm_stop(EXCP_DEBUG
);
4137 if (qemu_shutdown_requested()) {
4138 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4145 if (qemu_reset_requested()) {
4146 monitor_protocol_event(QEVENT_RESET
, NULL
);
4148 qemu_system_reset();
4151 if (qemu_powerdown_requested()) {
4152 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4153 qemu_irq_raise(qemu_system_powerdown
);
4155 if ((r
= qemu_vmstop_requested())) {
4156 monitor_protocol_event(QEVENT_STOP
, NULL
);
4163 static void version(void)
4165 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4168 static void help(int exitcode
)
4171 printf("usage: %s [options] [disk_image]\n"
4173 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4175 #define DEF(option, opt_arg, opt_enum, opt_help) \
4177 #define DEFHEADING(text) stringify(text) "\n"
4178 #include "qemu-options.h"
4183 "During emulation, the following keys are useful:\n"
4184 "ctrl-alt-f toggle full screen\n"
4185 "ctrl-alt-n switch to virtual console 'n'\n"
4186 "ctrl-alt toggle mouse and keyboard grab\n"
4188 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4193 DEFAULT_NETWORK_SCRIPT
,
4194 DEFAULT_NETWORK_DOWN_SCRIPT
,
4196 DEFAULT_GDBSTUB_PORT
,
4201 #define HAS_ARG 0x0001
4204 #define DEF(option, opt_arg, opt_enum, opt_help) \
4206 #define DEFHEADING(text)
4207 #include "qemu-options.h"
4213 typedef struct QEMUOption
{
4219 static const QEMUOption qemu_options
[] = {
4220 { "h", 0, QEMU_OPTION_h
},
4221 #define DEF(option, opt_arg, opt_enum, opt_help) \
4222 { option, opt_arg, opt_enum },
4223 #define DEFHEADING(text)
4224 #include "qemu-options.h"
4232 struct soundhw soundhw
[] = {
4233 #ifdef HAS_AUDIO_CHOICE
4234 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4240 { .init_isa
= pcspk_audio_init
}
4247 "Creative Sound Blaster 16",
4250 { .init_isa
= SB16_init
}
4254 #ifdef CONFIG_CS4231A
4260 { .init_isa
= cs4231a_init
}
4268 "Yamaha YMF262 (OPL3)",
4270 "Yamaha YM3812 (OPL2)",
4274 { .init_isa
= Adlib_init
}
4281 "Gravis Ultrasound GF1",
4284 { .init_isa
= GUS_init
}
4291 "Intel 82801AA AC97 Audio",
4294 { .init_pci
= ac97_init
}
4298 #ifdef CONFIG_ES1370
4301 "ENSONIQ AudioPCI ES1370",
4304 { .init_pci
= es1370_init
}
4308 #endif /* HAS_AUDIO_CHOICE */
4310 { NULL
, NULL
, 0, 0, { NULL
} }
4313 static void select_soundhw (const char *optarg
)
4317 if (*optarg
== '?') {
4320 printf ("Valid sound card names (comma separated):\n");
4321 for (c
= soundhw
; c
->name
; ++c
) {
4322 printf ("%-11s %s\n", c
->name
, c
->descr
);
4324 printf ("\n-soundhw all will enable all of the above\n");
4325 exit (*optarg
!= '?');
4333 if (!strcmp (optarg
, "all")) {
4334 for (c
= soundhw
; c
->name
; ++c
) {
4342 e
= strchr (p
, ',');
4343 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4345 for (c
= soundhw
; c
->name
; ++c
) {
4346 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4355 "Unknown sound card name (too big to show)\n");
4358 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4363 p
+= l
+ (e
!= NULL
);
4367 goto show_valid_cards
;
4372 static void select_vgahw (const char *p
)
4376 vga_interface_type
= VGA_NONE
;
4377 if (strstart(p
, "std", &opts
)) {
4378 vga_interface_type
= VGA_STD
;
4379 } else if (strstart(p
, "cirrus", &opts
)) {
4380 vga_interface_type
= VGA_CIRRUS
;
4381 } else if (strstart(p
, "vmware", &opts
)) {
4382 vga_interface_type
= VGA_VMWARE
;
4383 } else if (strstart(p
, "xenfb", &opts
)) {
4384 vga_interface_type
= VGA_XENFB
;
4385 } else if (!strstart(p
, "none", &opts
)) {
4387 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4391 const char *nextopt
;
4393 if (strstart(opts
, ",retrace=", &nextopt
)) {
4395 if (strstart(opts
, "dumb", &nextopt
))
4396 vga_retrace_method
= VGA_RETRACE_DUMB
;
4397 else if (strstart(opts
, "precise", &nextopt
))
4398 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4399 else goto invalid_vga
;
4400 } else goto invalid_vga
;
4406 static int balloon_parse(const char *arg
)
4410 if (strcmp(arg
, "none") == 0) {
4414 if (!strncmp(arg
, "virtio", 6)) {
4415 if (arg
[6] == ',') {
4416 /* have params -> parse them */
4417 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4421 /* create empty opts */
4422 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4424 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4433 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4435 exit(STATUS_CONTROL_C_EXIT
);
4440 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4444 if(strlen(str
) != 36)
4447 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4448 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4449 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4455 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4463 static void termsig_handler(int signal
)
4465 qemu_system_shutdown_request();
4468 static void sigchld_handler(int signal
)
4470 waitpid(-1, NULL
, WNOHANG
);
4473 static void sighandler_setup(void)
4475 struct sigaction act
;
4477 memset(&act
, 0, sizeof(act
));
4478 act
.sa_handler
= termsig_handler
;
4479 sigaction(SIGINT
, &act
, NULL
);
4480 sigaction(SIGHUP
, &act
, NULL
);
4481 sigaction(SIGTERM
, &act
, NULL
);
4483 act
.sa_handler
= sigchld_handler
;
4484 act
.sa_flags
= SA_NOCLDSTOP
;
4485 sigaction(SIGCHLD
, &act
, NULL
);
4491 /* Look for support files in the same directory as the executable. */
4492 static char *find_datadir(const char *argv0
)
4498 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4505 while (p
!= buf
&& *p
!= '\\')
4508 if (access(buf
, R_OK
) == 0) {
4509 return qemu_strdup(buf
);
4515 /* Find a likely location for support files using the location of the binary.
4516 For installed binaries this will be "$bindir/../share/qemu". When
4517 running from the build tree this will be "$bindir/../pc-bios". */
4518 #define SHARE_SUFFIX "/share/qemu"
4519 #define BUILD_SUFFIX "/pc-bios"
4520 static char *find_datadir(const char *argv0
)
4528 #if defined(__linux__)
4531 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4537 #elif defined(__FreeBSD__)
4540 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4547 /* If we don't have any way of figuring out the actual executable
4548 location then try argv[0]. */
4550 p
= realpath(argv0
, buf
);
4558 max_len
= strlen(dir
) +
4559 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4560 res
= qemu_mallocz(max_len
);
4561 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4562 if (access(res
, R_OK
)) {
4563 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4564 if (access(res
, R_OK
)) {
4576 char *qemu_find_file(int type
, const char *name
)
4582 /* If name contains path separators then try it as a straight path. */
4583 if ((strchr(name
, '/') || strchr(name
, '\\'))
4584 && access(name
, R_OK
) == 0) {
4585 return qemu_strdup(name
);
4588 case QEMU_FILE_TYPE_BIOS
:
4591 case QEMU_FILE_TYPE_KEYMAP
:
4592 subdir
= "keymaps/";
4597 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4598 buf
= qemu_mallocz(len
);
4599 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4600 if (access(buf
, R_OK
)) {
4607 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4611 dev
= qdev_device_add(opts
);
4617 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4619 CharDriverState
*chr
;
4621 chr
= qemu_chr_open_opts(opts
, NULL
);
4627 struct device_config
{
4629 DEV_USB
, /* -usbdevice */
4631 DEV_SERIAL
, /* -serial */
4632 DEV_PARALLEL
, /* -parallel */
4633 DEV_MONITOR
, /* -monitor */
4635 const char *cmdline
;
4636 QTAILQ_ENTRY(device_config
) next
;
4638 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4640 static void add_device_config(int type
, const char *cmdline
)
4642 struct device_config
*conf
;
4644 conf
= qemu_mallocz(sizeof(*conf
));
4646 conf
->cmdline
= cmdline
;
4647 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4650 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4652 struct device_config
*conf
;
4655 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4656 if (conf
->type
!= type
)
4658 rc
= func(conf
->cmdline
);
4665 static void serial_monitor_mux(void)
4667 struct device_config
*mon0
, *serial
;
4668 const char *devname
;
4670 QTAILQ_FOREACH(mon0
, &device_configs
, next
) {
4671 if (mon0
->type
!= DEV_MONITOR
)
4673 if (strcmp(mon0
->cmdline
,"stdio") != 0)
4677 QTAILQ_FOREACH(serial
, &device_configs
, next
) {
4678 if (serial
->type
!= DEV_SERIAL
)
4680 devname
= serial
->cmdline
;
4681 if (devname
&& !strcmp(devname
,"mon:stdio")) {
4682 QTAILQ_REMOVE(&device_configs
, mon0
, next
);
4684 } else if (devname
&& !strcmp(devname
,"stdio")) {
4685 QTAILQ_REMOVE(&device_configs
, mon0
, next
);
4686 serial
->cmdline
= "mon:stdio";
4692 static int serial_parse(const char *devname
)
4694 static int index
= 0;
4697 if (strcmp(devname
, "none") == 0)
4699 if (index
== MAX_SERIAL_PORTS
) {
4700 fprintf(stderr
, "qemu: too many serial ports\n");
4703 snprintf(label
, sizeof(label
), "serial%d", index
);
4704 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4705 if (!serial_hds
[index
]) {
4706 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4707 devname
, strerror(errno
));
4714 static int parallel_parse(const char *devname
)
4716 static int index
= 0;
4719 if (strcmp(devname
, "none") == 0)
4721 if (index
== MAX_PARALLEL_PORTS
) {
4722 fprintf(stderr
, "qemu: too many parallel ports\n");
4725 snprintf(label
, sizeof(label
), "parallel%d", index
);
4726 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4727 if (!parallel_hds
[index
]) {
4728 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4729 devname
, strerror(errno
));
4736 static int monitor_parse(const char *devname
)
4738 static int index
= 0;
4741 if (strcmp(devname
, "none") == 0)
4743 if (index
== MAX_MONITOR_DEVICES
) {
4744 fprintf(stderr
, "qemu: too many monitor devices\n");
4748 snprintf(label
, sizeof(label
), "monitor");
4750 snprintf(label
, sizeof(label
), "monitor%d", index
);
4752 monitor_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4753 if (!monitor_hds
[index
]) {
4754 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
4762 int main(int argc
, char **argv
, char **envp
)
4764 const char *gdbstub_dev
= NULL
;
4765 uint32_t boot_devices_bitmap
= 0;
4767 int snapshot
, linux_boot
, net_boot
;
4768 const char *initrd_filename
;
4769 const char *kernel_filename
, *kernel_cmdline
;
4770 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4772 DisplayChangeListener
*dcl
;
4773 int cyls
, heads
, secs
, translation
;
4774 QemuOpts
*hda_opts
= NULL
, *opts
;
4776 const char *r
, *optarg
;
4777 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4778 int virtio_console_index
;
4779 const char *loadvm
= NULL
;
4780 QEMUMachine
*machine
;
4781 const char *cpu_model
;
4786 const char *pid_file
= NULL
;
4787 const char *incoming
= NULL
;
4790 struct passwd
*pwd
= NULL
;
4791 const char *chroot_dir
= NULL
;
4792 const char *run_as
= NULL
;
4795 int show_vnc_port
= 0;
4799 qemu_errors_to_file(stderr
);
4800 qemu_cache_utils_init(envp
);
4802 QLIST_INIT (&vm_change_state_head
);
4805 struct sigaction act
;
4806 sigfillset(&act
.sa_mask
);
4808 act
.sa_handler
= SIG_IGN
;
4809 sigaction(SIGPIPE
, &act
, NULL
);
4812 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4813 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4814 QEMU to run on a single CPU */
4819 h
= GetCurrentProcess();
4820 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4821 for(i
= 0; i
< 32; i
++) {
4822 if (mask
& (1 << i
))
4827 SetProcessAffinityMask(h
, mask
);
4833 module_call_init(MODULE_INIT_MACHINE
);
4834 machine
= find_default_machine();
4836 initrd_filename
= NULL
;
4839 kernel_filename
= NULL
;
4840 kernel_cmdline
= "";
4841 cyls
= heads
= secs
= 0;
4842 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4844 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4845 virtio_consoles
[i
] = NULL
;
4846 virtio_console_index
= 0;
4848 for (i
= 0; i
< MAX_NODES
; i
++) {
4850 node_cpumask
[i
] = 0;
4865 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4867 const QEMUOption
*popt
;
4870 /* Treat --foo the same as -foo. */
4873 popt
= qemu_options
;
4876 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4880 if (!strcmp(popt
->name
, r
+ 1))
4884 if (popt
->flags
& HAS_ARG
) {
4885 if (optind
>= argc
) {
4886 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4890 optarg
= argv
[optind
++];
4895 switch(popt
->index
) {
4897 machine
= find_machine(optarg
);
4900 printf("Supported machines are:\n");
4901 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4903 printf("%-10s %s (alias of %s)\n",
4904 m
->alias
, m
->desc
, m
->name
);
4905 printf("%-10s %s%s\n",
4907 m
->is_default
? " (default)" : "");
4909 exit(*optarg
!= '?');
4912 case QEMU_OPTION_cpu
:
4913 /* hw initialization will check this */
4914 if (*optarg
== '?') {
4915 /* XXX: implement xxx_cpu_list for targets that still miss it */
4916 #if defined(cpu_list)
4917 cpu_list(stdout
, &fprintf
);
4924 case QEMU_OPTION_initrd
:
4925 initrd_filename
= optarg
;
4927 case QEMU_OPTION_hda
:
4929 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4931 hda_opts
= drive_add(optarg
, HD_ALIAS
4932 ",cyls=%d,heads=%d,secs=%d%s",
4933 0, cyls
, heads
, secs
,
4934 translation
== BIOS_ATA_TRANSLATION_LBA
?
4936 translation
== BIOS_ATA_TRANSLATION_NONE
?
4937 ",trans=none" : "");
4939 case QEMU_OPTION_hdb
:
4940 case QEMU_OPTION_hdc
:
4941 case QEMU_OPTION_hdd
:
4942 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4944 case QEMU_OPTION_drive
:
4945 drive_add(NULL
, "%s", optarg
);
4947 case QEMU_OPTION_set
:
4948 if (qemu_set_option(optarg
) != 0)
4951 case QEMU_OPTION_global
:
4952 if (qemu_global_option(optarg
) != 0)
4955 case QEMU_OPTION_mtdblock
:
4956 drive_add(optarg
, MTD_ALIAS
);
4958 case QEMU_OPTION_sd
:
4959 drive_add(optarg
, SD_ALIAS
);
4961 case QEMU_OPTION_pflash
:
4962 drive_add(optarg
, PFLASH_ALIAS
);
4964 case QEMU_OPTION_snapshot
:
4967 case QEMU_OPTION_hdachs
:
4971 cyls
= strtol(p
, (char **)&p
, 0);
4972 if (cyls
< 1 || cyls
> 16383)
4977 heads
= strtol(p
, (char **)&p
, 0);
4978 if (heads
< 1 || heads
> 16)
4983 secs
= strtol(p
, (char **)&p
, 0);
4984 if (secs
< 1 || secs
> 63)
4988 if (!strcmp(p
, "none"))
4989 translation
= BIOS_ATA_TRANSLATION_NONE
;
4990 else if (!strcmp(p
, "lba"))
4991 translation
= BIOS_ATA_TRANSLATION_LBA
;
4992 else if (!strcmp(p
, "auto"))
4993 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4996 } else if (*p
!= '\0') {
4998 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5001 if (hda_opts
!= NULL
) {
5003 snprintf(num
, sizeof(num
), "%d", cyls
);
5004 qemu_opt_set(hda_opts
, "cyls", num
);
5005 snprintf(num
, sizeof(num
), "%d", heads
);
5006 qemu_opt_set(hda_opts
, "heads", num
);
5007 snprintf(num
, sizeof(num
), "%d", secs
);
5008 qemu_opt_set(hda_opts
, "secs", num
);
5009 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5010 qemu_opt_set(hda_opts
, "trans", "lba");
5011 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5012 qemu_opt_set(hda_opts
, "trans", "none");
5016 case QEMU_OPTION_numa
:
5017 if (nb_numa_nodes
>= MAX_NODES
) {
5018 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5023 case QEMU_OPTION_nographic
:
5024 display_type
= DT_NOGRAPHIC
;
5026 #ifdef CONFIG_CURSES
5027 case QEMU_OPTION_curses
:
5028 display_type
= DT_CURSES
;
5031 case QEMU_OPTION_portrait
:
5034 case QEMU_OPTION_kernel
:
5035 kernel_filename
= optarg
;
5037 case QEMU_OPTION_append
:
5038 kernel_cmdline
= optarg
;
5040 case QEMU_OPTION_cdrom
:
5041 drive_add(optarg
, CDROM_ALIAS
);
5043 case QEMU_OPTION_boot
:
5045 static const char * const params
[] = {
5046 "order", "once", "menu", NULL
5048 char buf
[sizeof(boot_devices
)];
5049 char *standard_boot_devices
;
5052 if (!strchr(optarg
, '=')) {
5054 pstrcpy(buf
, sizeof(buf
), optarg
);
5055 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5057 "qemu: unknown boot parameter '%s' in '%s'\n",
5063 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5064 boot_devices_bitmap
= parse_bootdevices(buf
);
5065 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5068 if (get_param_value(buf
, sizeof(buf
),
5070 boot_devices_bitmap
|= parse_bootdevices(buf
);
5071 standard_boot_devices
= qemu_strdup(boot_devices
);
5072 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5073 qemu_register_reset(restore_boot_devices
,
5074 standard_boot_devices
);
5076 if (get_param_value(buf
, sizeof(buf
),
5078 if (!strcmp(buf
, "on")) {
5080 } else if (!strcmp(buf
, "off")) {
5084 "qemu: invalid option value '%s'\n",
5092 case QEMU_OPTION_fda
:
5093 case QEMU_OPTION_fdb
:
5094 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5097 case QEMU_OPTION_no_fd_bootchk
:
5101 case QEMU_OPTION_netdev
:
5102 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5106 case QEMU_OPTION_net
:
5107 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5112 case QEMU_OPTION_tftp
:
5113 legacy_tftp_prefix
= optarg
;
5115 case QEMU_OPTION_bootp
:
5116 legacy_bootp_filename
= optarg
;
5119 case QEMU_OPTION_smb
:
5120 if (net_slirp_smb(optarg
) < 0)
5124 case QEMU_OPTION_redir
:
5125 if (net_slirp_redir(optarg
) < 0)
5129 case QEMU_OPTION_bt
:
5130 add_device_config(DEV_BT
, optarg
);
5133 case QEMU_OPTION_audio_help
:
5137 case QEMU_OPTION_soundhw
:
5138 select_soundhw (optarg
);
5144 case QEMU_OPTION_version
:
5148 case QEMU_OPTION_m
: {
5152 value
= strtoul(optarg
, &ptr
, 10);
5154 case 0: case 'M': case 'm':
5161 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5165 /* On 32-bit hosts, QEMU is limited by virtual address space */
5166 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5167 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5170 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5171 fprintf(stderr
, "qemu: ram size too large\n");
5180 const CPULogItem
*item
;
5182 mask
= cpu_str_to_log_mask(optarg
);
5184 printf("Log items (comma separated):\n");
5185 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5186 printf("%-10s %s\n", item
->name
, item
->help
);
5194 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5196 case QEMU_OPTION_gdb
:
5197 gdbstub_dev
= optarg
;
5202 case QEMU_OPTION_bios
:
5205 case QEMU_OPTION_singlestep
:
5212 keyboard_layout
= optarg
;
5214 case QEMU_OPTION_localtime
:
5217 case QEMU_OPTION_vga
:
5218 select_vgahw (optarg
);
5220 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5226 w
= strtol(p
, (char **)&p
, 10);
5229 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5235 h
= strtol(p
, (char **)&p
, 10);
5240 depth
= strtol(p
, (char **)&p
, 10);
5241 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5242 depth
!= 24 && depth
!= 32)
5244 } else if (*p
== '\0') {
5245 depth
= graphic_depth
;
5252 graphic_depth
= depth
;
5256 case QEMU_OPTION_echr
:
5259 term_escape_char
= strtol(optarg
, &r
, 0);
5261 printf("Bad argument to echr\n");
5264 case QEMU_OPTION_monitor
:
5265 add_device_config(DEV_MONITOR
, optarg
);
5266 default_monitor
= 0;
5268 case QEMU_OPTION_chardev
:
5269 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5271 fprintf(stderr
, "parse error: %s\n", optarg
);
5275 case QEMU_OPTION_serial
:
5276 add_device_config(DEV_SERIAL
, optarg
);
5279 case QEMU_OPTION_watchdog
:
5282 "qemu: only one watchdog option may be given\n");
5287 case QEMU_OPTION_watchdog_action
:
5288 if (select_watchdog_action(optarg
) == -1) {
5289 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5293 case QEMU_OPTION_virtiocon
:
5294 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5295 fprintf(stderr
, "qemu: too many virtio consoles\n");
5298 virtio_consoles
[virtio_console_index
] = optarg
;
5299 virtio_console_index
++;
5301 case QEMU_OPTION_parallel
:
5302 add_device_config(DEV_PARALLEL
, optarg
);
5303 default_parallel
= 0;
5305 case QEMU_OPTION_loadvm
:
5308 case QEMU_OPTION_full_screen
:
5312 case QEMU_OPTION_no_frame
:
5315 case QEMU_OPTION_alt_grab
:
5318 case QEMU_OPTION_ctrl_grab
:
5321 case QEMU_OPTION_no_quit
:
5324 case QEMU_OPTION_sdl
:
5325 display_type
= DT_SDL
;
5328 case QEMU_OPTION_pidfile
:
5332 case QEMU_OPTION_win2k_hack
:
5333 win2k_install_hack
= 1;
5335 case QEMU_OPTION_rtc_td_hack
:
5338 case QEMU_OPTION_acpitable
:
5339 if(acpi_table_add(optarg
) < 0) {
5340 fprintf(stderr
, "Wrong acpi table provided\n");
5344 case QEMU_OPTION_smbios
:
5345 if(smbios_entry_add(optarg
) < 0) {
5346 fprintf(stderr
, "Wrong smbios provided\n");
5352 case QEMU_OPTION_enable_kvm
:
5356 case QEMU_OPTION_usb
:
5359 case QEMU_OPTION_usbdevice
:
5361 add_device_config(DEV_USB
, optarg
);
5363 case QEMU_OPTION_device
:
5364 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5368 case QEMU_OPTION_smp
:
5371 fprintf(stderr
, "Invalid number of CPUs\n");
5374 if (max_cpus
< smp_cpus
) {
5375 fprintf(stderr
, "maxcpus must be equal to or greater than "
5379 if (max_cpus
> 255) {
5380 fprintf(stderr
, "Unsupported number of maxcpus\n");
5384 case QEMU_OPTION_vnc
:
5385 display_type
= DT_VNC
;
5386 vnc_display
= optarg
;
5389 case QEMU_OPTION_no_acpi
:
5392 case QEMU_OPTION_no_hpet
:
5395 case QEMU_OPTION_balloon
:
5396 if (balloon_parse(optarg
) < 0) {
5397 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5402 case QEMU_OPTION_no_reboot
:
5405 case QEMU_OPTION_no_shutdown
:
5408 case QEMU_OPTION_show_cursor
:
5411 case QEMU_OPTION_uuid
:
5412 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5413 fprintf(stderr
, "Fail to parse UUID string."
5414 " Wrong format.\n");
5419 case QEMU_OPTION_daemonize
:
5423 case QEMU_OPTION_option_rom
:
5424 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5425 fprintf(stderr
, "Too many option ROMs\n");
5428 option_rom
[nb_option_roms
] = optarg
;
5431 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5432 case QEMU_OPTION_semihosting
:
5433 semihosting_enabled
= 1;
5436 case QEMU_OPTION_name
:
5437 qemu_name
= qemu_strdup(optarg
);
5439 char *p
= strchr(qemu_name
, ',');
5442 if (strncmp(p
, "process=", 8)) {
5443 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5451 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5452 case QEMU_OPTION_prom_env
:
5453 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5454 fprintf(stderr
, "Too many prom variables\n");
5457 prom_envs
[nb_prom_envs
] = optarg
;
5462 case QEMU_OPTION_old_param
:
5466 case QEMU_OPTION_clock
:
5467 configure_alarms(optarg
);
5469 case QEMU_OPTION_startdate
:
5470 configure_rtc_date_offset(optarg
, 1);
5472 case QEMU_OPTION_rtc
:
5473 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5475 fprintf(stderr
, "parse error: %s\n", optarg
);
5478 configure_rtc(opts
);
5480 case QEMU_OPTION_tb_size
:
5481 tb_size
= strtol(optarg
, NULL
, 0);
5485 case QEMU_OPTION_icount
:
5487 if (strcmp(optarg
, "auto") == 0) {
5488 icount_time_shift
= -1;
5490 icount_time_shift
= strtol(optarg
, NULL
, 0);
5493 case QEMU_OPTION_incoming
:
5497 case QEMU_OPTION_chroot
:
5498 chroot_dir
= optarg
;
5500 case QEMU_OPTION_runas
:
5505 case QEMU_OPTION_xen_domid
:
5506 xen_domid
= atoi(optarg
);
5508 case QEMU_OPTION_xen_create
:
5509 xen_mode
= XEN_CREATE
;
5511 case QEMU_OPTION_xen_attach
:
5512 xen_mode
= XEN_ATTACH
;
5515 case QEMU_OPTION_readconfig
:
5518 fp
= fopen(optarg
, "r");
5520 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5523 if (qemu_config_parse(fp
) != 0) {
5529 case QEMU_OPTION_writeconfig
:
5532 if (strcmp(optarg
, "-") == 0) {
5535 fp
= fopen(optarg
, "w");
5537 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5541 qemu_config_write(fp
);
5549 /* If no data_dir is specified then try to find it relative to the
5552 data_dir
= find_datadir(argv
[0]);
5554 /* If all else fails use the install patch specified when building. */
5556 data_dir
= CONFIG_QEMU_SHAREDIR
;
5560 * Default to max_cpus = smp_cpus, in case the user doesn't
5561 * specify a max_cpus value.
5564 max_cpus
= smp_cpus
;
5566 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5567 if (smp_cpus
> machine
->max_cpus
) {
5568 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5569 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5574 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5576 if (display_type
== DT_NOGRAPHIC
) {
5578 add_device_config(DEV_SERIAL
, "stdio");
5579 if (default_parallel
)
5580 add_device_config(DEV_PARALLEL
, "null");
5581 if (default_monitor
)
5582 add_device_config(DEV_MONITOR
, "stdio");
5585 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5586 if (default_parallel
)
5587 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5588 if (default_monitor
)
5589 add_device_config(DEV_MONITOR
, "vc:80Cx24C");
5592 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5599 if (pipe(fds
) == -1)
5610 len
= read(fds
[0], &status
, 1);
5611 if (len
== -1 && (errno
== EINTR
))
5616 else if (status
== 1) {
5617 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5625 qemu_set_cloexec(fds
[1]);
5637 signal(SIGTSTP
, SIG_IGN
);
5638 signal(SIGTTOU
, SIG_IGN
);
5639 signal(SIGTTIN
, SIG_IGN
);
5642 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5645 write(fds
[1], &status
, 1);
5647 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5652 if (kvm_enabled()) {
5655 ret
= kvm_init(smp_cpus
);
5657 fprintf(stderr
, "failed to initialize KVM\n");
5662 if (qemu_init_main_loop()) {
5663 fprintf(stderr
, "qemu_init_main_loop failed\n");
5666 linux_boot
= (kernel_filename
!= NULL
);
5668 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5669 fprintf(stderr
, "-append only allowed with -kernel option\n");
5673 if (!linux_boot
&& initrd_filename
!= NULL
) {
5674 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5679 /* Win32 doesn't support line-buffering and requires size >= 2 */
5680 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5683 if (init_timer_alarm() < 0) {
5684 fprintf(stderr
, "could not initialize alarm timer\n");
5687 if (use_icount
&& icount_time_shift
< 0) {
5689 /* 125MIPS seems a reasonable initial guess at the guest speed.
5690 It will be corrected fairly quickly anyway. */
5691 icount_time_shift
= 3;
5692 init_icount_adjust();
5699 if (net_init_clients() < 0) {
5703 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5704 net_set_boot_mask(net_boot
);
5706 /* init the bluetooth world */
5707 if (foreach_device_config(DEV_BT
, bt_parse
))
5710 /* init the memory */
5712 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5714 /* init the dynamic translator */
5715 cpu_exec_init_all(tb_size
* 1024 * 1024);
5717 bdrv_init_with_whitelist();
5721 /* we always create the cdrom drive, even if no disk is there */
5722 drive_add(NULL
, CDROM_ALIAS
);
5724 /* we always create at least one floppy */
5725 drive_add(NULL
, FD_ALIAS
, 0);
5727 /* we always create one sd slot, even if no card is in it */
5728 drive_add(NULL
, SD_ALIAS
);
5730 /* open the virtual block devices */
5732 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5733 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5736 vmstate_register(0, &vmstate_timers
,&timers_state
);
5737 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5740 /* Maintain compatibility with multiple stdio monitors */
5741 serial_monitor_mux();
5743 if (nb_numa_nodes
> 0) {
5746 if (nb_numa_nodes
> smp_cpus
) {
5747 nb_numa_nodes
= smp_cpus
;
5750 /* If no memory size if given for any node, assume the default case
5751 * and distribute the available memory equally across all nodes
5753 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5754 if (node_mem
[i
] != 0)
5757 if (i
== nb_numa_nodes
) {
5758 uint64_t usedmem
= 0;
5760 /* On Linux, the each node's border has to be 8MB aligned,
5761 * the final node gets the rest.
5763 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5764 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5765 usedmem
+= node_mem
[i
];
5767 node_mem
[i
] = ram_size
- usedmem
;
5770 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5771 if (node_cpumask
[i
] != 0)
5774 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5775 * must cope with this anyway, because there are BIOSes out there in
5776 * real machines which also use this scheme.
5778 if (i
== nb_numa_nodes
) {
5779 for (i
= 0; i
< smp_cpus
; i
++) {
5780 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5785 if (foreach_device_config(DEV_MONITOR
, monitor_parse
) < 0)
5787 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5789 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5792 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5793 const char *devname
= virtio_consoles
[i
];
5794 if (devname
&& strcmp(devname
, "none")) {
5796 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5797 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5798 if (!virtcon_hds
[i
]) {
5799 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5800 devname
, strerror(errno
));
5806 module_call_init(MODULE_INIT_DEVICE
);
5809 i
= select_watchdog(watchdog
);
5811 exit (i
== 1 ? 1 : 0);
5814 if (machine
->compat_props
) {
5815 qdev_prop_register_global_list(machine
->compat_props
);
5819 machine
->init(ram_size
, boot_devices
,
5820 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5824 /* must be after terminal init, SDL library changes signal handlers */
5828 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5829 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5830 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5836 current_machine
= machine
;
5838 /* init USB devices */
5840 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5844 /* init generic devices */
5845 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5849 dumb_display_init();
5850 /* just use the first displaystate for the moment */
5853 if (display_type
== DT_DEFAULT
) {
5854 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5855 display_type
= DT_SDL
;
5857 display_type
= DT_VNC
;
5858 vnc_display
= "localhost:0,to=99";
5864 switch (display_type
) {
5867 #if defined(CONFIG_CURSES)
5869 curses_display_init(ds
, full_screen
);
5872 #if defined(CONFIG_SDL)
5874 sdl_display_init(ds
, full_screen
, no_frame
);
5876 #elif defined(CONFIG_COCOA)
5878 cocoa_display_init(ds
, full_screen
);
5882 vnc_display_init(ds
);
5883 if (vnc_display_open(ds
, vnc_display
) < 0)
5886 if (show_vnc_port
) {
5887 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5895 dcl
= ds
->listeners
;
5896 while (dcl
!= NULL
) {
5897 if (dcl
->dpy_refresh
!= NULL
) {
5898 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5899 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5904 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5905 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5906 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5909 text_consoles_set_display(display_state
);
5911 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5912 if (monitor_hds
[i
]) {
5913 monitor_init(monitor_hds
[i
],
5914 MONITOR_USE_READLINE
|
5915 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5919 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5920 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5925 qdev_machine_creation_done();
5929 qemu_system_reset();
5931 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5937 qemu_start_incoming_migration(incoming
);
5938 } else if (autostart
) {
5948 len
= write(fds
[1], &status
, 1);
5949 if (len
== -1 && (errno
== EINTR
))
5956 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5962 pwd
= getpwnam(run_as
);
5964 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5970 if (chroot(chroot_dir
) < 0) {
5971 fprintf(stderr
, "chroot failed\n");
5978 if (setgid(pwd
->pw_gid
) < 0) {
5979 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5982 if (setuid(pwd
->pw_uid
) < 0) {
5983 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5986 if (setuid(0) != -1) {
5987 fprintf(stderr
, "Dropping privileges failed\n");