4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
111 #include <mmsystem.h>
115 #if defined(__APPLE__) || defined(main)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 return qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/watchdog.h"
142 #include "hw/smbios.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
156 #include "audio/audio.h"
157 #include "migration.h"
160 #include "qemu-option.h"
161 #include "qemu-config.h"
165 #include "exec-all.h"
167 #include "qemu_socket.h"
169 #include "slirp/libslirp.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
179 to store the VM snapshots */
180 struct drivelist drives
= TAILQ_HEAD_INITIALIZER(drives
);
181 struct driveoptlist driveopts
= TAILQ_HEAD_INITIALIZER(driveopts
);
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 static DisplayState
*display_state
;
184 DisplayType display_type
= DT_DEFAULT
;
185 const char* keyboard_layout
= NULL
;
186 int64_t ticks_per_sec
;
189 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 int cirrus_vga_enabled
= 1;
195 int std_vga_enabled
= 0;
196 int vmsvga_enabled
= 0;
197 int xenfb_enabled
= 0;
199 int graphic_width
= 1024;
200 int graphic_height
= 768;
201 int graphic_depth
= 8;
203 int graphic_width
= 800;
204 int graphic_height
= 600;
205 int graphic_depth
= 15;
207 static int full_screen
= 0;
209 static int no_frame
= 0;
212 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
213 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
214 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
216 int win2k_install_hack
= 0;
223 const char *vnc_display
;
224 int acpi_enabled
= 1;
226 int virtio_balloon
= 1;
227 const char *virtio_balloon_devaddr
;
232 int graphic_rotate
= 0;
236 WatchdogTimerModel
*watchdog
= NULL
;
237 int watchdog_action
= WDT_RESET
;
238 const char *option_rom
[MAX_OPTION_ROMS
];
240 int semihosting_enabled
= 0;
244 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static int timer_alarm_pending
= 1;
259 /* Conversion factor from emulated instructions to virtual clock ticks. */
260 static int icount_time_shift
;
261 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
262 #define MAX_ICOUNT_SHIFT 10
263 /* Compensate for varying guest execution speed. */
264 static int64_t qemu_icount_bias
;
265 static QEMUTimer
*icount_rt_timer
;
266 static QEMUTimer
*icount_vm_timer
;
267 static QEMUTimer
*nographic_timer
;
269 uint8_t qemu_uuid
[16];
271 static QEMUBootSetHandler
*boot_set_handler
;
272 static void *boot_set_opaque
;
274 /***********************************************************/
275 /* x86 ISA bus support */
277 target_phys_addr_t isa_mem_base
= 0;
280 /***********************************************************/
281 void hw_error(const char *fmt
, ...)
287 fprintf(stderr
, "qemu: hardware error: ");
288 vfprintf(stderr
, fmt
, ap
);
289 fprintf(stderr
, "\n");
290 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
291 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
293 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
295 cpu_dump_state(env
, stderr
, fprintf
, 0);
302 static void set_proc_name(const char *s
)
304 #if defined(__linux__) && defined(PR_SET_NAME)
308 name
[sizeof(name
) - 1] = 0;
309 strncpy(name
, s
, sizeof(name
));
310 /* Could rewrite argv[0] too, but that's a bit more complicated.
311 This simple way is enough for `top'. */
312 prctl(PR_SET_NAME
, name
);
319 static QEMUBalloonEvent
*qemu_balloon_event
;
320 void *qemu_balloon_event_opaque
;
322 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
324 qemu_balloon_event
= func
;
325 qemu_balloon_event_opaque
= opaque
;
328 void qemu_balloon(ram_addr_t target
)
330 if (qemu_balloon_event
)
331 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
334 ram_addr_t
qemu_balloon_status(void)
336 if (qemu_balloon_event
)
337 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
341 /***********************************************************/
344 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
345 static void *qemu_put_kbd_event_opaque
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
347 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
349 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
351 qemu_put_kbd_event_opaque
= opaque
;
352 qemu_put_kbd_event
= func
;
355 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
356 void *opaque
, int absolute
,
359 QEMUPutMouseEntry
*s
, *cursor
;
361 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
363 s
->qemu_put_mouse_event
= func
;
364 s
->qemu_put_mouse_event_opaque
= opaque
;
365 s
->qemu_put_mouse_event_absolute
= absolute
;
366 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
369 if (!qemu_put_mouse_event_head
) {
370 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
374 cursor
= qemu_put_mouse_event_head
;
375 while (cursor
->next
!= NULL
)
376 cursor
= cursor
->next
;
379 qemu_put_mouse_event_current
= s
;
384 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
386 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
388 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
391 cursor
= qemu_put_mouse_event_head
;
392 while (cursor
!= NULL
&& cursor
!= entry
) {
394 cursor
= cursor
->next
;
397 if (cursor
== NULL
) // does not exist or list empty
399 else if (prev
== NULL
) { // entry is head
400 qemu_put_mouse_event_head
= cursor
->next
;
401 if (qemu_put_mouse_event_current
== entry
)
402 qemu_put_mouse_event_current
= cursor
->next
;
403 qemu_free(entry
->qemu_put_mouse_event_name
);
408 prev
->next
= entry
->next
;
410 if (qemu_put_mouse_event_current
== entry
)
411 qemu_put_mouse_event_current
= prev
;
413 qemu_free(entry
->qemu_put_mouse_event_name
);
417 void kbd_put_keycode(int keycode
)
419 if (qemu_put_kbd_event
) {
420 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
424 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
426 QEMUPutMouseEvent
*mouse_event
;
427 void *mouse_event_opaque
;
430 if (!qemu_put_mouse_event_current
) {
435 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
437 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
440 if (graphic_rotate
) {
441 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
444 width
= graphic_width
- 1;
445 mouse_event(mouse_event_opaque
,
446 width
- dy
, dx
, dz
, buttons_state
);
448 mouse_event(mouse_event_opaque
,
449 dx
, dy
, dz
, buttons_state
);
453 int kbd_mouse_is_absolute(void)
455 if (!qemu_put_mouse_event_current
)
458 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
461 void do_info_mice(Monitor
*mon
)
463 QEMUPutMouseEntry
*cursor
;
466 if (!qemu_put_mouse_event_head
) {
467 monitor_printf(mon
, "No mouse devices connected\n");
471 monitor_printf(mon
, "Mouse devices available:\n");
472 cursor
= qemu_put_mouse_event_head
;
473 while (cursor
!= NULL
) {
474 monitor_printf(mon
, "%c Mouse #%d: %s\n",
475 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
476 index
, cursor
->qemu_put_mouse_event_name
);
478 cursor
= cursor
->next
;
482 void do_mouse_set(Monitor
*mon
, int index
)
484 QEMUPutMouseEntry
*cursor
;
487 if (!qemu_put_mouse_event_head
) {
488 monitor_printf(mon
, "No mouse devices connected\n");
492 cursor
= qemu_put_mouse_event_head
;
493 while (cursor
!= NULL
&& index
!= i
) {
495 cursor
= cursor
->next
;
499 qemu_put_mouse_event_current
= cursor
;
501 monitor_printf(mon
, "Mouse at given index not found\n");
504 /* compute with 96 bit intermediate result: (a*b)/c */
505 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
510 #ifdef HOST_WORDS_BIGENDIAN
520 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
521 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
524 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
528 /***********************************************************/
529 /* real time host monotonic timer */
531 #define QEMU_TIMER_BASE 1000000000LL
535 static int64_t clock_freq
;
537 static void init_get_clock(void)
541 ret
= QueryPerformanceFrequency(&freq
);
543 fprintf(stderr
, "Could not calibrate ticks\n");
546 clock_freq
= freq
.QuadPart
;
549 static int64_t get_clock(void)
552 QueryPerformanceCounter(&ti
);
553 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
558 static int use_rt_clock
;
560 static void init_get_clock(void)
563 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
564 || defined(__DragonFly__)
567 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
574 static int64_t get_clock(void)
576 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
577 || defined(__DragonFly__)
580 clock_gettime(CLOCK_MONOTONIC
, &ts
);
581 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
585 /* XXX: using gettimeofday leads to problems if the date
586 changes, so it should be avoided. */
588 gettimeofday(&tv
, NULL
);
589 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
594 /* Return the virtual CPU time, based on the instruction counter. */
595 static int64_t cpu_get_icount(void)
598 CPUState
*env
= cpu_single_env
;;
599 icount
= qemu_icount
;
602 fprintf(stderr
, "Bad clock read\n");
603 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
605 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
608 /***********************************************************/
609 /* guest cycle counter */
611 static int64_t cpu_ticks_prev
;
612 static int64_t cpu_ticks_offset
;
613 static int64_t cpu_clock_offset
;
614 static int cpu_ticks_enabled
;
616 /* return the host CPU cycle counter and handle stop/restart */
617 int64_t cpu_get_ticks(void)
620 return cpu_get_icount();
622 if (!cpu_ticks_enabled
) {
623 return cpu_ticks_offset
;
626 ticks
= cpu_get_real_ticks();
627 if (cpu_ticks_prev
> ticks
) {
628 /* Note: non increasing ticks may happen if the host uses
630 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
632 cpu_ticks_prev
= ticks
;
633 return ticks
+ cpu_ticks_offset
;
637 /* return the host CPU monotonic timer and handle stop/restart */
638 static int64_t cpu_get_clock(void)
641 if (!cpu_ticks_enabled
) {
642 return cpu_clock_offset
;
645 return ti
+ cpu_clock_offset
;
649 /* enable cpu_get_ticks() */
650 void cpu_enable_ticks(void)
652 if (!cpu_ticks_enabled
) {
653 cpu_ticks_offset
-= cpu_get_real_ticks();
654 cpu_clock_offset
-= get_clock();
655 cpu_ticks_enabled
= 1;
659 /* disable cpu_get_ticks() : the clock is stopped. You must not call
660 cpu_get_ticks() after that. */
661 void cpu_disable_ticks(void)
663 if (cpu_ticks_enabled
) {
664 cpu_ticks_offset
= cpu_get_ticks();
665 cpu_clock_offset
= cpu_get_clock();
666 cpu_ticks_enabled
= 0;
670 /***********************************************************/
673 #define QEMU_TIMER_REALTIME 0
674 #define QEMU_TIMER_VIRTUAL 1
678 /* XXX: add frequency */
686 struct QEMUTimer
*next
;
689 struct qemu_alarm_timer
{
693 int (*start
)(struct qemu_alarm_timer
*t
);
694 void (*stop
)(struct qemu_alarm_timer
*t
);
695 void (*rearm
)(struct qemu_alarm_timer
*t
);
699 #define ALARM_FLAG_DYNTICKS 0x1
700 #define ALARM_FLAG_EXPIRED 0x2
702 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
704 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
707 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
709 if (!alarm_has_dynticks(t
))
715 /* TODO: MIN_TIMER_REARM_US should be optimized */
716 #define MIN_TIMER_REARM_US 250
718 static struct qemu_alarm_timer
*alarm_timer
;
722 struct qemu_alarm_win32
{
725 } alarm_win32_data
= {0, -1};
727 static int win32_start_timer(struct qemu_alarm_timer
*t
);
728 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
729 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
733 static int unix_start_timer(struct qemu_alarm_timer
*t
);
734 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
738 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
739 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
740 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
742 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
743 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
745 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
746 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
748 #endif /* __linux__ */
752 /* Correlation between real and virtual time is always going to be
753 fairly approximate, so ignore small variation.
754 When the guest is idle real and virtual time will be aligned in
756 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
758 static void icount_adjust(void)
763 static int64_t last_delta
;
764 /* If the VM is not running, then do nothing. */
768 cur_time
= cpu_get_clock();
769 cur_icount
= qemu_get_clock(vm_clock
);
770 delta
= cur_icount
- cur_time
;
771 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
773 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
774 && icount_time_shift
> 0) {
775 /* The guest is getting too far ahead. Slow time down. */
779 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
780 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
781 /* The guest is getting too far behind. Speed time up. */
785 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
788 static void icount_adjust_rt(void * opaque
)
790 qemu_mod_timer(icount_rt_timer
,
791 qemu_get_clock(rt_clock
) + 1000);
795 static void icount_adjust_vm(void * opaque
)
797 qemu_mod_timer(icount_vm_timer
,
798 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
802 static void init_icount_adjust(void)
804 /* Have both realtime and virtual time triggers for speed adjustment.
805 The realtime trigger catches emulated time passing too slowly,
806 the virtual time trigger catches emulated time passing too fast.
807 Realtime triggers occur even when idle, so use them less frequently
809 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
810 qemu_mod_timer(icount_rt_timer
,
811 qemu_get_clock(rt_clock
) + 1000);
812 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
813 qemu_mod_timer(icount_vm_timer
,
814 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
817 static struct qemu_alarm_timer alarm_timers
[] = {
820 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
821 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
822 /* HPET - if available - is preferred */
823 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
824 /* ...otherwise try RTC */
825 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
827 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
829 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
830 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
831 {"win32", 0, win32_start_timer
,
832 win32_stop_timer
, NULL
, &alarm_win32_data
},
837 static void show_available_alarms(void)
841 printf("Available alarm timers, in order of precedence:\n");
842 for (i
= 0; alarm_timers
[i
].name
; i
++)
843 printf("%s\n", alarm_timers
[i
].name
);
846 static void configure_alarms(char const *opt
)
850 int count
= ARRAY_SIZE(alarm_timers
) - 1;
853 struct qemu_alarm_timer tmp
;
855 if (!strcmp(opt
, "?")) {
856 show_available_alarms();
862 /* Reorder the array */
863 name
= strtok(arg
, ",");
865 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
866 if (!strcmp(alarm_timers
[i
].name
, name
))
871 fprintf(stderr
, "Unknown clock %s\n", name
);
880 tmp
= alarm_timers
[i
];
881 alarm_timers
[i
] = alarm_timers
[cur
];
882 alarm_timers
[cur
] = tmp
;
886 name
= strtok(NULL
, ",");
892 /* Disable remaining timers */
893 for (i
= cur
; i
< count
; i
++)
894 alarm_timers
[i
].name
= NULL
;
896 show_available_alarms();
904 static QEMUTimer
*active_timers
[2];
906 static QEMUClock
*qemu_new_clock(int type
)
909 clock
= qemu_mallocz(sizeof(QEMUClock
));
914 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
918 ts
= qemu_mallocz(sizeof(QEMUTimer
));
925 void qemu_free_timer(QEMUTimer
*ts
)
930 /* stop a timer, but do not dealloc it */
931 void qemu_del_timer(QEMUTimer
*ts
)
935 /* NOTE: this code must be signal safe because
936 qemu_timer_expired() can be called from a signal. */
937 pt
= &active_timers
[ts
->clock
->type
];
950 /* modify the current timer so that it will be fired when current_time
951 >= expire_time. The corresponding callback will be called. */
952 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
958 /* add the timer in the sorted list */
959 /* NOTE: this code must be signal safe because
960 qemu_timer_expired() can be called from a signal. */
961 pt
= &active_timers
[ts
->clock
->type
];
966 if (t
->expire_time
> expire_time
)
970 ts
->expire_time
= expire_time
;
974 /* Rearm if necessary */
975 if (pt
== &active_timers
[ts
->clock
->type
]) {
976 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
977 qemu_rearm_alarm_timer(alarm_timer
);
979 /* Interrupt execution to force deadline recalculation. */
985 int qemu_timer_pending(QEMUTimer
*ts
)
988 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
995 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
999 return (timer_head
->expire_time
<= current_time
);
1002 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1008 if (!ts
|| ts
->expire_time
> current_time
)
1010 /* remove timer from the list before calling the callback */
1011 *ptimer_head
= ts
->next
;
1014 /* run the callback (the timer list can be modified) */
1019 int64_t qemu_get_clock(QEMUClock
*clock
)
1021 switch(clock
->type
) {
1022 case QEMU_TIMER_REALTIME
:
1023 return get_clock() / 1000000;
1025 case QEMU_TIMER_VIRTUAL
:
1027 return cpu_get_icount();
1029 return cpu_get_clock();
1034 static void init_timers(void)
1037 ticks_per_sec
= QEMU_TIMER_BASE
;
1038 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1039 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1043 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1045 uint64_t expire_time
;
1047 if (qemu_timer_pending(ts
)) {
1048 expire_time
= ts
->expire_time
;
1052 qemu_put_be64(f
, expire_time
);
1055 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1057 uint64_t expire_time
;
1059 expire_time
= qemu_get_be64(f
);
1060 if (expire_time
!= -1) {
1061 qemu_mod_timer(ts
, expire_time
);
1067 static void timer_save(QEMUFile
*f
, void *opaque
)
1069 if (cpu_ticks_enabled
) {
1070 hw_error("cannot save state if virtual timers are running");
1072 qemu_put_be64(f
, cpu_ticks_offset
);
1073 qemu_put_be64(f
, ticks_per_sec
);
1074 qemu_put_be64(f
, cpu_clock_offset
);
1077 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1079 if (version_id
!= 1 && version_id
!= 2)
1081 if (cpu_ticks_enabled
) {
1084 cpu_ticks_offset
=qemu_get_be64(f
);
1085 ticks_per_sec
=qemu_get_be64(f
);
1086 if (version_id
== 2) {
1087 cpu_clock_offset
=qemu_get_be64(f
);
1092 static void qemu_event_increment(void);
1095 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1096 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1099 static void host_alarm_handler(int host_signum
)
1103 #define DISP_FREQ 1000
1105 static int64_t delta_min
= INT64_MAX
;
1106 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1108 ti
= qemu_get_clock(vm_clock
);
1109 if (last_clock
!= 0) {
1110 delta
= ti
- last_clock
;
1111 if (delta
< delta_min
)
1113 if (delta
> delta_max
)
1116 if (++count
== DISP_FREQ
) {
1117 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1118 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1119 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1120 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1121 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1123 delta_min
= INT64_MAX
;
1131 if (alarm_has_dynticks(alarm_timer
) ||
1133 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1134 qemu_get_clock(vm_clock
))) ||
1135 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1136 qemu_get_clock(rt_clock
))) {
1137 qemu_event_increment();
1138 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1140 #ifndef CONFIG_IOTHREAD
1142 /* stop the currently executing cpu because a timer occured */
1145 if (next_cpu
->kqemu_enabled
) {
1146 kqemu_cpu_interrupt(next_cpu
);
1151 timer_alarm_pending
= 1;
1152 qemu_notify_event();
1156 static int64_t qemu_next_deadline(void)
1160 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1161 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1162 qemu_get_clock(vm_clock
);
1164 /* To avoid problems with overflow limit this to 2^32. */
1174 #if defined(__linux__) || defined(_WIN32)
1175 static uint64_t qemu_next_deadline_dyntick(void)
1183 delta
= (qemu_next_deadline() + 999) / 1000;
1185 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1186 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1187 qemu_get_clock(rt_clock
))*1000;
1188 if (rtdelta
< delta
)
1192 if (delta
< MIN_TIMER_REARM_US
)
1193 delta
= MIN_TIMER_REARM_US
;
1201 /* Sets a specific flag */
1202 static int fcntl_setfl(int fd
, int flag
)
1206 flags
= fcntl(fd
, F_GETFL
);
1210 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1216 #if defined(__linux__)
1218 #define RTC_FREQ 1024
1220 static void enable_sigio_timer(int fd
)
1222 struct sigaction act
;
1225 sigfillset(&act
.sa_mask
);
1227 act
.sa_handler
= host_alarm_handler
;
1229 sigaction(SIGIO
, &act
, NULL
);
1230 fcntl_setfl(fd
, O_ASYNC
);
1231 fcntl(fd
, F_SETOWN
, getpid());
1234 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1236 struct hpet_info info
;
1239 fd
= open("/dev/hpet", O_RDONLY
);
1244 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1246 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1247 "error, but for better emulation accuracy type:\n"
1248 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1252 /* Check capabilities */
1253 r
= ioctl(fd
, HPET_INFO
, &info
);
1257 /* Enable periodic mode */
1258 r
= ioctl(fd
, HPET_EPI
, 0);
1259 if (info
.hi_flags
&& (r
< 0))
1262 /* Enable interrupt */
1263 r
= ioctl(fd
, HPET_IE_ON
, 0);
1267 enable_sigio_timer(fd
);
1268 t
->priv
= (void *)(long)fd
;
1276 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1278 int fd
= (long)t
->priv
;
1283 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1286 unsigned long current_rtc_freq
= 0;
1288 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1291 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1292 if (current_rtc_freq
!= RTC_FREQ
&&
1293 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1294 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1295 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1296 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1299 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1305 enable_sigio_timer(rtc_fd
);
1307 t
->priv
= (void *)(long)rtc_fd
;
1312 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1314 int rtc_fd
= (long)t
->priv
;
1319 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1323 struct sigaction act
;
1325 sigfillset(&act
.sa_mask
);
1327 act
.sa_handler
= host_alarm_handler
;
1329 sigaction(SIGALRM
, &act
, NULL
);
1332 * Initialize ev struct to 0 to avoid valgrind complaining
1333 * about uninitialized data in timer_create call
1335 memset(&ev
, 0, sizeof(ev
));
1336 ev
.sigev_value
.sival_int
= 0;
1337 ev
.sigev_notify
= SIGEV_SIGNAL
;
1338 ev
.sigev_signo
= SIGALRM
;
1340 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1341 perror("timer_create");
1343 /* disable dynticks */
1344 fprintf(stderr
, "Dynamic Ticks disabled\n");
1349 t
->priv
= (void *)(long)host_timer
;
1354 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1356 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1358 timer_delete(host_timer
);
1361 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1363 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1364 struct itimerspec timeout
;
1365 int64_t nearest_delta_us
= INT64_MAX
;
1368 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1369 !active_timers
[QEMU_TIMER_VIRTUAL
])
1372 nearest_delta_us
= qemu_next_deadline_dyntick();
1374 /* check whether a timer is already running */
1375 if (timer_gettime(host_timer
, &timeout
)) {
1377 fprintf(stderr
, "Internal timer error: aborting\n");
1380 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1381 if (current_us
&& current_us
<= nearest_delta_us
)
1384 timeout
.it_interval
.tv_sec
= 0;
1385 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1386 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1387 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1388 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1390 fprintf(stderr
, "Internal timer error: aborting\n");
1395 #endif /* defined(__linux__) */
1397 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1399 struct sigaction act
;
1400 struct itimerval itv
;
1404 sigfillset(&act
.sa_mask
);
1406 act
.sa_handler
= host_alarm_handler
;
1408 sigaction(SIGALRM
, &act
, NULL
);
1410 itv
.it_interval
.tv_sec
= 0;
1411 /* for i386 kernel 2.6 to get 1 ms */
1412 itv
.it_interval
.tv_usec
= 999;
1413 itv
.it_value
.tv_sec
= 0;
1414 itv
.it_value
.tv_usec
= 10 * 1000;
1416 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1423 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1425 struct itimerval itv
;
1427 memset(&itv
, 0, sizeof(itv
));
1428 setitimer(ITIMER_REAL
, &itv
, NULL
);
1431 #endif /* !defined(_WIN32) */
1436 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1439 struct qemu_alarm_win32
*data
= t
->priv
;
1442 memset(&tc
, 0, sizeof(tc
));
1443 timeGetDevCaps(&tc
, sizeof(tc
));
1445 if (data
->period
< tc
.wPeriodMin
)
1446 data
->period
= tc
.wPeriodMin
;
1448 timeBeginPeriod(data
->period
);
1450 flags
= TIME_CALLBACK_FUNCTION
;
1451 if (alarm_has_dynticks(t
))
1452 flags
|= TIME_ONESHOT
;
1454 flags
|= TIME_PERIODIC
;
1456 data
->timerId
= timeSetEvent(1, // interval (ms)
1457 data
->period
, // resolution
1458 host_alarm_handler
, // function
1459 (DWORD
)t
, // parameter
1462 if (!data
->timerId
) {
1463 perror("Failed to initialize win32 alarm timer");
1464 timeEndPeriod(data
->period
);
1471 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1473 struct qemu_alarm_win32
*data
= t
->priv
;
1475 timeKillEvent(data
->timerId
);
1476 timeEndPeriod(data
->period
);
1479 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1481 struct qemu_alarm_win32
*data
= t
->priv
;
1482 uint64_t nearest_delta_us
;
1484 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1485 !active_timers
[QEMU_TIMER_VIRTUAL
])
1488 nearest_delta_us
= qemu_next_deadline_dyntick();
1489 nearest_delta_us
/= 1000;
1491 timeKillEvent(data
->timerId
);
1493 data
->timerId
= timeSetEvent(1,
1497 TIME_ONESHOT
| TIME_PERIODIC
);
1499 if (!data
->timerId
) {
1500 perror("Failed to re-arm win32 alarm timer");
1502 timeEndPeriod(data
->period
);
1509 static int init_timer_alarm(void)
1511 struct qemu_alarm_timer
*t
= NULL
;
1514 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1515 t
= &alarm_timers
[i
];
1535 static void quit_timers(void)
1537 alarm_timer
->stop(alarm_timer
);
1541 /***********************************************************/
1542 /* host time/date access */
1543 void qemu_get_timedate(struct tm
*tm
, int offset
)
1550 if (rtc_date_offset
== -1) {
1554 ret
= localtime(&ti
);
1556 ti
-= rtc_date_offset
;
1560 memcpy(tm
, ret
, sizeof(struct tm
));
1563 int qemu_timedate_diff(struct tm
*tm
)
1567 if (rtc_date_offset
== -1)
1569 seconds
= mktimegm(tm
);
1571 seconds
= mktime(tm
);
1573 seconds
= mktimegm(tm
) + rtc_date_offset
;
1575 return seconds
- time(NULL
);
1579 static void socket_cleanup(void)
1584 static int socket_init(void)
1589 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1591 err
= WSAGetLastError();
1592 fprintf(stderr
, "WSAStartup: %d\n", err
);
1595 atexit(socket_cleanup
);
1600 /***********************************************************/
1601 /* Bluetooth support */
1604 static struct HCIInfo
*hci_table
[MAX_NICS
];
1606 static struct bt_vlan_s
{
1607 struct bt_scatternet_s net
;
1609 struct bt_vlan_s
*next
;
1612 /* find or alloc a new bluetooth "VLAN" */
1613 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1615 struct bt_vlan_s
**pvlan
, *vlan
;
1616 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1620 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1622 pvlan
= &first_bt_vlan
;
1623 while (*pvlan
!= NULL
)
1624 pvlan
= &(*pvlan
)->next
;
1629 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1633 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1638 static struct HCIInfo null_hci
= {
1639 .cmd_send
= null_hci_send
,
1640 .sco_send
= null_hci_send
,
1641 .acl_send
= null_hci_send
,
1642 .bdaddr_set
= null_hci_addr_set
,
1645 struct HCIInfo
*qemu_next_hci(void)
1647 if (cur_hci
== nb_hcis
)
1650 return hci_table
[cur_hci
++];
1653 static struct HCIInfo
*hci_init(const char *str
)
1656 struct bt_scatternet_s
*vlan
= 0;
1658 if (!strcmp(str
, "null"))
1661 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1663 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1664 else if (!strncmp(str
, "hci", 3)) {
1667 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1668 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1673 vlan
= qemu_find_bt_vlan(0);
1675 return bt_new_hci(vlan
);
1678 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1683 static int bt_hci_parse(const char *str
)
1685 struct HCIInfo
*hci
;
1688 if (nb_hcis
>= MAX_NICS
) {
1689 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1693 hci
= hci_init(str
);
1702 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1703 hci
->bdaddr_set(hci
, bdaddr
.b
);
1705 hci_table
[nb_hcis
++] = hci
;
1710 static void bt_vhci_add(int vlan_id
)
1712 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1715 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1716 "an empty scatternet %i\n", vlan_id
);
1718 bt_vhci_init(bt_new_hci(vlan
));
1721 static struct bt_device_s
*bt_device_add(const char *opt
)
1723 struct bt_scatternet_s
*vlan
;
1725 char *endp
= strstr(opt
, ",vlan=");
1726 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1729 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1732 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1734 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1739 vlan
= qemu_find_bt_vlan(vlan_id
);
1742 fprintf(stderr
, "qemu: warning: adding a slave device to "
1743 "an empty scatternet %i\n", vlan_id
);
1745 if (!strcmp(devname
, "keyboard"))
1746 return bt_keyboard_init(vlan
);
1748 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1752 static int bt_parse(const char *opt
)
1754 const char *endp
, *p
;
1757 if (strstart(opt
, "hci", &endp
)) {
1758 if (!*endp
|| *endp
== ',') {
1760 if (!strstart(endp
, ",vlan=", 0))
1763 return bt_hci_parse(opt
);
1765 } else if (strstart(opt
, "vhci", &endp
)) {
1766 if (!*endp
|| *endp
== ',') {
1768 if (strstart(endp
, ",vlan=", &p
)) {
1769 vlan
= strtol(p
, (char **) &endp
, 0);
1771 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1775 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1784 } else if (strstart(opt
, "device:", &endp
))
1785 return !bt_device_add(endp
);
1787 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1791 /***********************************************************/
1792 /* QEMU Block devices */
1794 #define HD_ALIAS "index=%d,media=disk"
1795 #define CDROM_ALIAS "index=2,media=cdrom"
1796 #define FD_ALIAS "index=%d,if=floppy"
1797 #define PFLASH_ALIAS "if=pflash"
1798 #define MTD_ALIAS "if=mtd"
1799 #define SD_ALIAS "index=0,if=sd"
1801 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1808 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1811 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1813 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1814 __FUNCTION__
, optstr
);
1818 qemu_opt_set(opts
, "file", file
);
1822 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1826 /* seek interface, bus and unit */
1828 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1829 if (dinfo
->type
== type
&&
1830 dinfo
->bus
== bus
&&
1831 dinfo
->unit
== unit
)
1838 DriveInfo
*drive_get_by_id(const char *id
)
1842 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1843 if (strcmp(id
, dinfo
->id
))
1850 int drive_get_max_bus(BlockInterfaceType type
)
1856 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1857 if(dinfo
->type
== type
&&
1858 dinfo
->bus
> max_bus
)
1859 max_bus
= dinfo
->bus
;
1864 const char *drive_get_serial(BlockDriverState
*bdrv
)
1868 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1869 if (dinfo
->bdrv
== bdrv
)
1870 return dinfo
->serial
;
1876 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1880 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1881 if (dinfo
->bdrv
== bdrv
)
1882 return dinfo
->onerror
;
1885 return BLOCK_ERR_STOP_ENOSPC
;
1888 static void bdrv_format_print(void *opaque
, const char *name
)
1890 fprintf(stderr
, " %s", name
);
1893 void drive_uninit(BlockDriverState
*bdrv
)
1897 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1898 if (dinfo
->bdrv
!= bdrv
)
1900 qemu_opts_del(dinfo
->opts
);
1901 TAILQ_REMOVE(&drives
, dinfo
, next
);
1907 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1911 const char *file
= NULL
;
1914 const char *mediastr
= "";
1915 BlockInterfaceType type
;
1916 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1917 int bus_id
, unit_id
;
1918 int cyls
, heads
, secs
, translation
;
1919 BlockDriver
*drv
= NULL
;
1920 QEMUMachine
*machine
= opaque
;
1924 int bdrv_flags
, onerror
;
1925 const char *devaddr
;
1931 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1934 if (machine
->use_scsi
) {
1936 max_devs
= MAX_SCSI_DEVS
;
1937 pstrcpy(devname
, sizeof(devname
), "scsi");
1940 max_devs
= MAX_IDE_DEVS
;
1941 pstrcpy(devname
, sizeof(devname
), "ide");
1945 /* extract parameters */
1946 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1947 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1948 index
= qemu_opt_get_number(opts
, "index", -1);
1950 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1951 heads
= qemu_opt_get_number(opts
, "heads", 0);
1952 secs
= qemu_opt_get_number(opts
, "secs", 0);
1954 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1956 file
= qemu_opt_get(opts
, "file");
1957 serial
= qemu_opt_get(opts
, "serial");
1959 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1960 pstrcpy(devname
, sizeof(devname
), buf
);
1961 if (!strcmp(buf
, "ide")) {
1963 max_devs
= MAX_IDE_DEVS
;
1964 } else if (!strcmp(buf
, "scsi")) {
1966 max_devs
= MAX_SCSI_DEVS
;
1967 } else if (!strcmp(buf
, "floppy")) {
1970 } else if (!strcmp(buf
, "pflash")) {
1973 } else if (!strcmp(buf
, "mtd")) {
1976 } else if (!strcmp(buf
, "sd")) {
1979 } else if (!strcmp(buf
, "virtio")) {
1982 } else if (!strcmp(buf
, "xen")) {
1985 } else if (!strcmp(buf
, "none")) {
1989 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1994 if (cyls
|| heads
|| secs
) {
1995 if (cyls
< 1 || cyls
> 16383) {
1996 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1999 if (heads
< 1 || heads
> 16) {
2000 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2003 if (secs
< 1 || secs
> 63) {
2004 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2009 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2012 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2016 if (!strcmp(buf
, "none"))
2017 translation
= BIOS_ATA_TRANSLATION_NONE
;
2018 else if (!strcmp(buf
, "lba"))
2019 translation
= BIOS_ATA_TRANSLATION_LBA
;
2020 else if (!strcmp(buf
, "auto"))
2021 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2023 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2028 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2029 if (!strcmp(buf
, "disk")) {
2031 } else if (!strcmp(buf
, "cdrom")) {
2032 if (cyls
|| secs
|| heads
) {
2034 "qemu: '%s' invalid physical CHS format\n", buf
);
2037 media
= MEDIA_CDROM
;
2039 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2044 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2045 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2047 else if (!strcmp(buf
, "writethrough"))
2049 else if (!strcmp(buf
, "writeback"))
2052 fprintf(stderr
, "qemu: invalid cache option\n");
2057 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2058 if (strcmp(buf
, "?") == 0) {
2059 fprintf(stderr
, "qemu: Supported formats:");
2060 bdrv_iterate_format(bdrv_format_print
, NULL
);
2061 fprintf(stderr
, "\n");
2064 drv
= bdrv_find_format(buf
);
2066 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2071 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2072 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2073 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2074 fprintf(stderr
, "werror is no supported by this format\n");
2077 if (!strcmp(buf
, "ignore"))
2078 onerror
= BLOCK_ERR_IGNORE
;
2079 else if (!strcmp(buf
, "enospc"))
2080 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2081 else if (!strcmp(buf
, "stop"))
2082 onerror
= BLOCK_ERR_STOP_ANY
;
2083 else if (!strcmp(buf
, "report"))
2084 onerror
= BLOCK_ERR_REPORT
;
2086 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2091 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2092 if (type
!= IF_VIRTIO
) {
2093 fprintf(stderr
, "addr is not supported\n");
2098 /* compute bus and unit according index */
2101 if (bus_id
!= 0 || unit_id
!= -1) {
2103 "qemu: index cannot be used with bus and unit\n");
2111 unit_id
= index
% max_devs
;
2112 bus_id
= index
/ max_devs
;
2116 /* if user doesn't specify a unit_id,
2117 * try to find the first free
2120 if (unit_id
== -1) {
2122 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2124 if (max_devs
&& unit_id
>= max_devs
) {
2125 unit_id
-= max_devs
;
2133 if (max_devs
&& unit_id
>= max_devs
) {
2134 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2135 unit_id
, max_devs
- 1);
2140 * ignore multiple definitions
2143 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2150 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2151 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2152 dinfo
->id
= qemu_strdup(buf
);
2154 /* no id supplied -> create one */
2155 dinfo
->id
= qemu_mallocz(32);
2156 if (type
== IF_IDE
|| type
== IF_SCSI
)
2157 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2159 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2160 devname
, bus_id
, mediastr
, unit_id
);
2162 snprintf(dinfo
->id
, 32, "%s%s%i",
2163 devname
, mediastr
, unit_id
);
2165 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2166 dinfo
->devaddr
= devaddr
;
2168 dinfo
->bus
= bus_id
;
2169 dinfo
->unit
= unit_id
;
2170 dinfo
->onerror
= onerror
;
2173 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2174 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2183 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2184 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2188 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2193 /* FIXME: This isn't really a floppy, but it's a reasonable
2196 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2203 /* add virtio block device */
2204 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2205 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2206 qemu_opt_set(opts
, "drive", dinfo
->id
);
2208 qemu_opt_set(opts
, "addr", devaddr
);
2219 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2220 cache
= 2; /* always use write-back with snapshot */
2222 if (cache
== 0) /* no caching */
2223 bdrv_flags
|= BDRV_O_NOCACHE
;
2224 else if (cache
== 2) /* write-back */
2225 bdrv_flags
|= BDRV_O_CACHE_WB
;
2226 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2227 fprintf(stderr
, "qemu: could not open disk image %s\n",
2231 if (bdrv_key_required(dinfo
->bdrv
))
2237 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2239 QEMUMachine
*machine
= opaque
;
2240 int fatal_error
= 0;
2242 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2249 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2251 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2252 qemu_opt_set(opts
, "snapshot", "on");
2257 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2259 boot_set_handler
= func
;
2260 boot_set_opaque
= opaque
;
2263 int qemu_boot_set(const char *boot_devices
)
2265 if (!boot_set_handler
) {
2268 return boot_set_handler(boot_set_opaque
, boot_devices
);
2271 static int parse_bootdevices(char *devices
)
2273 /* We just do some generic consistency checks */
2277 for (p
= devices
; *p
!= '\0'; p
++) {
2278 /* Allowed boot devices are:
2279 * a-b: floppy disk drives
2280 * c-f: IDE disk drives
2281 * g-m: machine implementation dependant drives
2282 * n-p: network devices
2283 * It's up to each machine implementation to check if the given boot
2284 * devices match the actual hardware implementation and firmware
2287 if (*p
< 'a' || *p
> 'p') {
2288 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2291 if (bitmap
& (1 << (*p
- 'a'))) {
2292 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2295 bitmap
|= 1 << (*p
- 'a');
2300 static void restore_boot_devices(void *opaque
)
2302 char *standard_boot_devices
= opaque
;
2304 qemu_boot_set(standard_boot_devices
);
2306 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2307 qemu_free(standard_boot_devices
);
2310 static void numa_add(const char *optarg
)
2314 unsigned long long value
, endvalue
;
2317 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2318 if (!strcmp(option
, "node")) {
2319 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2320 nodenr
= nb_numa_nodes
;
2322 nodenr
= strtoull(option
, NULL
, 10);
2325 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2326 node_mem
[nodenr
] = 0;
2328 value
= strtoull(option
, &endptr
, 0);
2330 case 0: case 'M': case 'm':
2337 node_mem
[nodenr
] = value
;
2339 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2340 node_cpumask
[nodenr
] = 0;
2342 value
= strtoull(option
, &endptr
, 10);
2345 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2347 if (*endptr
== '-') {
2348 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2349 if (endvalue
>= 63) {
2352 "only 63 CPUs in NUMA mode supported.\n");
2354 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2359 node_cpumask
[nodenr
] = value
;
2366 /***********************************************************/
2369 static USBPort
*used_usb_ports
;
2370 static USBPort
*free_usb_ports
;
2372 /* ??? Maybe change this to register a hub to keep track of the topology. */
2373 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2374 usb_attachfn attach
)
2376 port
->opaque
= opaque
;
2377 port
->index
= index
;
2378 port
->attach
= attach
;
2379 port
->next
= free_usb_ports
;
2380 free_usb_ports
= port
;
2383 int usb_device_add_dev(USBDevice
*dev
)
2387 /* Find a USB port to add the device to. */
2388 port
= free_usb_ports
;
2392 /* Create a new hub and chain it on. */
2393 free_usb_ports
= NULL
;
2394 port
->next
= used_usb_ports
;
2395 used_usb_ports
= port
;
2397 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2398 usb_attach(port
, hub
);
2399 port
= free_usb_ports
;
2402 free_usb_ports
= port
->next
;
2403 port
->next
= used_usb_ports
;
2404 used_usb_ports
= port
;
2405 usb_attach(port
, dev
);
2409 static void usb_msd_password_cb(void *opaque
, int err
)
2411 USBDevice
*dev
= opaque
;
2414 usb_device_add_dev(dev
);
2416 dev
->handle_destroy(dev
);
2419 static int usb_device_add(const char *devname
, int is_hotplug
)
2424 if (!free_usb_ports
)
2427 if (strstart(devname
, "host:", &p
)) {
2428 dev
= usb_host_device_open(p
);
2429 } else if (!strcmp(devname
, "mouse")) {
2430 dev
= usb_mouse_init();
2431 } else if (!strcmp(devname
, "tablet")) {
2432 dev
= usb_tablet_init();
2433 } else if (!strcmp(devname
, "keyboard")) {
2434 dev
= usb_keyboard_init();
2435 } else if (strstart(devname
, "disk:", &p
)) {
2436 BlockDriverState
*bs
;
2438 dev
= usb_msd_init(p
);
2441 bs
= usb_msd_get_bdrv(dev
);
2442 if (bdrv_key_required(bs
)) {
2445 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2450 } else if (!strcmp(devname
, "wacom-tablet")) {
2451 dev
= usb_wacom_init();
2452 } else if (strstart(devname
, "serial:", &p
)) {
2453 dev
= usb_serial_init(p
);
2454 #ifdef CONFIG_BRLAPI
2455 } else if (!strcmp(devname
, "braille")) {
2456 dev
= usb_baum_init();
2458 } else if (strstart(devname
, "net:", &p
)) {
2461 if (net_client_init(NULL
, "nic", p
) < 0)
2463 nd_table
[nic
].model
= "usb";
2464 dev
= usb_net_init(&nd_table
[nic
]);
2465 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2466 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2467 bt_new_hci(qemu_find_bt_vlan(0)));
2474 return usb_device_add_dev(dev
);
2477 int usb_device_del_addr(int bus_num
, int addr
)
2483 if (!used_usb_ports
)
2489 lastp
= &used_usb_ports
;
2490 port
= used_usb_ports
;
2491 while (port
&& port
->dev
->addr
!= addr
) {
2492 lastp
= &port
->next
;
2500 *lastp
= port
->next
;
2501 usb_attach(port
, NULL
);
2502 dev
->handle_destroy(dev
);
2503 port
->next
= free_usb_ports
;
2504 free_usb_ports
= port
;
2508 static int usb_device_del(const char *devname
)
2513 if (strstart(devname
, "host:", &p
))
2514 return usb_host_device_close(p
);
2516 if (!used_usb_ports
)
2519 p
= strchr(devname
, '.');
2522 bus_num
= strtoul(devname
, NULL
, 0);
2523 addr
= strtoul(p
+ 1, NULL
, 0);
2525 return usb_device_del_addr(bus_num
, addr
);
2528 static int usb_parse(const char *cmdline
)
2530 return usb_device_add(cmdline
, 0);
2533 void do_usb_add(Monitor
*mon
, const char *devname
)
2535 usb_device_add(devname
, 1);
2538 void do_usb_del(Monitor
*mon
, const char *devname
)
2540 usb_device_del(devname
);
2543 void usb_info(Monitor
*mon
)
2547 const char *speed_str
;
2550 monitor_printf(mon
, "USB support not enabled\n");
2554 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2558 switch(dev
->speed
) {
2562 case USB_SPEED_FULL
:
2565 case USB_SPEED_HIGH
:
2572 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2573 0, dev
->addr
, speed_str
, dev
->devname
);
2577 /***********************************************************/
2578 /* PCMCIA/Cardbus */
2580 static struct pcmcia_socket_entry_s
{
2581 PCMCIASocket
*socket
;
2582 struct pcmcia_socket_entry_s
*next
;
2583 } *pcmcia_sockets
= 0;
2585 void pcmcia_socket_register(PCMCIASocket
*socket
)
2587 struct pcmcia_socket_entry_s
*entry
;
2589 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2590 entry
->socket
= socket
;
2591 entry
->next
= pcmcia_sockets
;
2592 pcmcia_sockets
= entry
;
2595 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2597 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2599 ptr
= &pcmcia_sockets
;
2600 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2601 if (entry
->socket
== socket
) {
2607 void pcmcia_info(Monitor
*mon
)
2609 struct pcmcia_socket_entry_s
*iter
;
2611 if (!pcmcia_sockets
)
2612 monitor_printf(mon
, "No PCMCIA sockets\n");
2614 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2615 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2616 iter
->socket
->attached
? iter
->socket
->card_string
:
2620 /***********************************************************/
2621 /* register display */
2623 struct DisplayAllocator default_allocator
= {
2624 defaultallocator_create_displaysurface
,
2625 defaultallocator_resize_displaysurface
,
2626 defaultallocator_free_displaysurface
2629 void register_displaystate(DisplayState
*ds
)
2639 DisplayState
*get_displaystate(void)
2641 return display_state
;
2644 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2646 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2647 return ds
->allocator
;
2652 static void dumb_display_init(void)
2654 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2655 ds
->allocator
= &default_allocator
;
2656 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2657 register_displaystate(ds
);
2660 /***********************************************************/
2663 typedef struct IOHandlerRecord
{
2665 IOCanRWHandler
*fd_read_poll
;
2667 IOHandler
*fd_write
;
2670 /* temporary data */
2672 struct IOHandlerRecord
*next
;
2675 static IOHandlerRecord
*first_io_handler
;
2677 /* XXX: fd_read_poll should be suppressed, but an API change is
2678 necessary in the character devices to suppress fd_can_read(). */
2679 int qemu_set_fd_handler2(int fd
,
2680 IOCanRWHandler
*fd_read_poll
,
2682 IOHandler
*fd_write
,
2685 IOHandlerRecord
**pioh
, *ioh
;
2687 if (!fd_read
&& !fd_write
) {
2688 pioh
= &first_io_handler
;
2693 if (ioh
->fd
== fd
) {
2700 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2704 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2705 ioh
->next
= first_io_handler
;
2706 first_io_handler
= ioh
;
2709 ioh
->fd_read_poll
= fd_read_poll
;
2710 ioh
->fd_read
= fd_read
;
2711 ioh
->fd_write
= fd_write
;
2712 ioh
->opaque
= opaque
;
2718 int qemu_set_fd_handler(int fd
,
2720 IOHandler
*fd_write
,
2723 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2727 /***********************************************************/
2728 /* Polling handling */
2730 typedef struct PollingEntry
{
2733 struct PollingEntry
*next
;
2736 static PollingEntry
*first_polling_entry
;
2738 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2740 PollingEntry
**ppe
, *pe
;
2741 pe
= qemu_mallocz(sizeof(PollingEntry
));
2743 pe
->opaque
= opaque
;
2744 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2749 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2751 PollingEntry
**ppe
, *pe
;
2752 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2754 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2762 /***********************************************************/
2763 /* Wait objects support */
2764 typedef struct WaitObjects
{
2766 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2767 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2768 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2771 static WaitObjects wait_objects
= {0};
2773 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2775 WaitObjects
*w
= &wait_objects
;
2777 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2779 w
->events
[w
->num
] = handle
;
2780 w
->func
[w
->num
] = func
;
2781 w
->opaque
[w
->num
] = opaque
;
2786 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2789 WaitObjects
*w
= &wait_objects
;
2792 for (i
= 0; i
< w
->num
; i
++) {
2793 if (w
->events
[i
] == handle
)
2796 w
->events
[i
] = w
->events
[i
+ 1];
2797 w
->func
[i
] = w
->func
[i
+ 1];
2798 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2806 /***********************************************************/
2807 /* ram save/restore */
2809 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2813 v
= qemu_get_byte(f
);
2816 if (qemu_get_buffer(f
, buf
, len
) != len
)
2820 v
= qemu_get_byte(f
);
2821 memset(buf
, v
, len
);
2827 if (qemu_file_has_error(f
))
2833 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2838 if (qemu_get_be32(f
) != last_ram_offset
)
2840 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2841 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2848 #define BDRV_HASH_BLOCK_SIZE 1024
2849 #define IOBUF_SIZE 4096
2850 #define RAM_CBLOCK_MAGIC 0xfabe
2852 typedef struct RamDecompressState
{
2855 uint8_t buf
[IOBUF_SIZE
];
2856 } RamDecompressState
;
2858 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2861 memset(s
, 0, sizeof(*s
));
2863 ret
= inflateInit(&s
->zstream
);
2869 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2873 s
->zstream
.avail_out
= len
;
2874 s
->zstream
.next_out
= buf
;
2875 while (s
->zstream
.avail_out
> 0) {
2876 if (s
->zstream
.avail_in
== 0) {
2877 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2879 clen
= qemu_get_be16(s
->f
);
2880 if (clen
> IOBUF_SIZE
)
2882 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2883 s
->zstream
.avail_in
= clen
;
2884 s
->zstream
.next_in
= s
->buf
;
2886 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2887 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2894 static void ram_decompress_close(RamDecompressState
*s
)
2896 inflateEnd(&s
->zstream
);
2899 #define RAM_SAVE_FLAG_FULL 0x01
2900 #define RAM_SAVE_FLAG_COMPRESS 0x02
2901 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2902 #define RAM_SAVE_FLAG_PAGE 0x08
2903 #define RAM_SAVE_FLAG_EOS 0x10
2905 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2907 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2908 uint32_t *array
= (uint32_t *)page
;
2911 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2912 if (array
[i
] != val
)
2919 static int ram_save_block(QEMUFile
*f
)
2921 static ram_addr_t current_addr
= 0;
2922 ram_addr_t saved_addr
= current_addr
;
2923 ram_addr_t addr
= 0;
2926 while (addr
< last_ram_offset
) {
2927 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2930 cpu_physical_memory_reset_dirty(current_addr
,
2931 current_addr
+ TARGET_PAGE_SIZE
,
2932 MIGRATION_DIRTY_FLAG
);
2934 p
= qemu_get_ram_ptr(current_addr
);
2936 if (is_dup_page(p
, *p
)) {
2937 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2938 qemu_put_byte(f
, *p
);
2940 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2941 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2947 addr
+= TARGET_PAGE_SIZE
;
2948 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2954 static uint64_t bytes_transferred
= 0;
2956 static ram_addr_t
ram_save_remaining(void)
2959 ram_addr_t count
= 0;
2961 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2962 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2969 uint64_t ram_bytes_remaining(void)
2971 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2974 uint64_t ram_bytes_transferred(void)
2976 return bytes_transferred
;
2979 uint64_t ram_bytes_total(void)
2981 return last_ram_offset
;
2984 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2987 uint64_t bytes_transferred_last
;
2989 uint64_t expected_time
= 0;
2991 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2992 qemu_file_set_error(f
);
2997 /* Make sure all dirty bits are set */
2998 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2999 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3000 cpu_physical_memory_set_dirty(addr
);
3003 /* Enable dirty memory tracking */
3004 cpu_physical_memory_set_dirty_tracking(1);
3006 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3009 bytes_transferred_last
= bytes_transferred
;
3010 bwidth
= get_clock();
3012 while (!qemu_file_rate_limit(f
)) {
3015 ret
= ram_save_block(f
);
3016 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3017 if (ret
== 0) /* no more blocks */
3021 bwidth
= get_clock() - bwidth
;
3022 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3024 /* if we haven't transferred anything this round, force expected_time to a
3025 * a very high value, but without crashing */
3029 /* try transferring iterative blocks of memory */
3033 /* flush all remaining blocks regardless of rate limiting */
3034 while (ram_save_block(f
) != 0) {
3035 bytes_transferred
+= TARGET_PAGE_SIZE
;
3037 cpu_physical_memory_set_dirty_tracking(0);
3040 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3042 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3044 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3047 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3049 RamDecompressState s1
, *s
= &s1
;
3053 if (ram_decompress_open(s
, f
) < 0)
3055 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3056 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3057 fprintf(stderr
, "Error while reading ram block header\n");
3061 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3062 BDRV_HASH_BLOCK_SIZE
) < 0) {
3063 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3068 printf("Error block header\n");
3072 ram_decompress_close(s
);
3077 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3082 if (version_id
== 1)
3083 return ram_load_v1(f
, opaque
);
3085 if (version_id
== 2) {
3086 if (qemu_get_be32(f
) != last_ram_offset
)
3088 return ram_load_dead(f
, opaque
);
3091 if (version_id
!= 3)
3095 addr
= qemu_get_be64(f
);
3097 flags
= addr
& ~TARGET_PAGE_MASK
;
3098 addr
&= TARGET_PAGE_MASK
;
3100 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3101 if (addr
!= last_ram_offset
)
3105 if (flags
& RAM_SAVE_FLAG_FULL
) {
3106 if (ram_load_dead(f
, opaque
) < 0)
3110 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3111 uint8_t ch
= qemu_get_byte(f
);
3112 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3115 (!kvm_enabled() || kvm_has_sync_mmu())) {
3116 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3119 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3120 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3121 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3126 void qemu_service_io(void)
3128 qemu_notify_event();
3131 /***********************************************************/
3132 /* bottom halves (can be seen as timers which expire ASAP) */
3143 static QEMUBH
*first_bh
= NULL
;
3145 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3148 bh
= qemu_mallocz(sizeof(QEMUBH
));
3150 bh
->opaque
= opaque
;
3151 bh
->next
= first_bh
;
3156 int qemu_bh_poll(void)
3162 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3163 if (!bh
->deleted
&& bh
->scheduled
) {
3172 /* remove deleted bhs */
3186 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3194 void qemu_bh_schedule(QEMUBH
*bh
)
3200 /* stop the currently executing CPU to execute the BH ASAP */
3201 qemu_notify_event();
3204 void qemu_bh_cancel(QEMUBH
*bh
)
3209 void qemu_bh_delete(QEMUBH
*bh
)
3215 static void qemu_bh_update_timeout(int *timeout
)
3219 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3220 if (!bh
->deleted
&& bh
->scheduled
) {
3222 /* idle bottom halves will be polled at least
3224 *timeout
= MIN(10, *timeout
);
3226 /* non-idle bottom halves will be executed
3235 /***********************************************************/
3236 /* machine registration */
3238 static QEMUMachine
*first_machine
= NULL
;
3239 QEMUMachine
*current_machine
= NULL
;
3241 int qemu_register_machine(QEMUMachine
*m
)
3244 pm
= &first_machine
;
3252 static QEMUMachine
*find_machine(const char *name
)
3256 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3257 if (!strcmp(m
->name
, name
))
3259 if (m
->alias
&& !strcmp(m
->alias
, name
))
3265 static QEMUMachine
*find_default_machine(void)
3269 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3270 if (m
->is_default
) {
3277 /***********************************************************/
3278 /* main execution loop */
3280 static void gui_update(void *opaque
)
3282 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3283 DisplayState
*ds
= opaque
;
3284 DisplayChangeListener
*dcl
= ds
->listeners
;
3288 while (dcl
!= NULL
) {
3289 if (dcl
->gui_timer_interval
&&
3290 dcl
->gui_timer_interval
< interval
)
3291 interval
= dcl
->gui_timer_interval
;
3294 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3297 static void nographic_update(void *opaque
)
3299 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3301 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3304 struct vm_change_state_entry
{
3305 VMChangeStateHandler
*cb
;
3307 LIST_ENTRY (vm_change_state_entry
) entries
;
3310 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3312 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3315 VMChangeStateEntry
*e
;
3317 e
= qemu_mallocz(sizeof (*e
));
3321 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3325 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3327 LIST_REMOVE (e
, entries
);
3331 static void vm_state_notify(int running
, int reason
)
3333 VMChangeStateEntry
*e
;
3335 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3336 e
->cb(e
->opaque
, running
, reason
);
3340 static void resume_all_vcpus(void);
3341 static void pause_all_vcpus(void);
3348 vm_state_notify(1, 0);
3349 qemu_rearm_alarm_timer(alarm_timer
);
3354 /* reset/shutdown handler */
3356 typedef struct QEMUResetEntry
{
3357 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3358 QEMUResetHandler
*func
;
3362 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3363 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3364 static int reset_requested
;
3365 static int shutdown_requested
;
3366 static int powerdown_requested
;
3367 static int debug_requested
;
3368 static int vmstop_requested
;
3370 int qemu_shutdown_requested(void)
3372 int r
= shutdown_requested
;
3373 shutdown_requested
= 0;
3377 int qemu_reset_requested(void)
3379 int r
= reset_requested
;
3380 reset_requested
= 0;
3384 int qemu_powerdown_requested(void)
3386 int r
= powerdown_requested
;
3387 powerdown_requested
= 0;
3391 static int qemu_debug_requested(void)
3393 int r
= debug_requested
;
3394 debug_requested
= 0;
3398 static int qemu_vmstop_requested(void)
3400 int r
= vmstop_requested
;
3401 vmstop_requested
= 0;
3405 static void do_vm_stop(int reason
)
3408 cpu_disable_ticks();
3411 vm_state_notify(0, reason
);
3415 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3417 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3420 re
->opaque
= opaque
;
3421 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3424 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3428 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3429 if (re
->func
== func
&& re
->opaque
== opaque
) {
3430 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3437 void qemu_system_reset(void)
3439 QEMUResetEntry
*re
, *nre
;
3441 /* reset all devices */
3442 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3443 re
->func(re
->opaque
);
3447 void qemu_system_reset_request(void)
3450 shutdown_requested
= 1;
3452 reset_requested
= 1;
3454 qemu_notify_event();
3457 void qemu_system_shutdown_request(void)
3459 shutdown_requested
= 1;
3460 qemu_notify_event();
3463 void qemu_system_powerdown_request(void)
3465 powerdown_requested
= 1;
3466 qemu_notify_event();
3469 #ifdef CONFIG_IOTHREAD
3470 static void qemu_system_vmstop_request(int reason
)
3472 vmstop_requested
= reason
;
3473 qemu_notify_event();
3478 static int io_thread_fd
= -1;
3480 static void qemu_event_increment(void)
3482 static const char byte
= 0;
3484 if (io_thread_fd
== -1)
3487 write(io_thread_fd
, &byte
, sizeof(byte
));
3490 static void qemu_event_read(void *opaque
)
3492 int fd
= (unsigned long)opaque
;
3495 /* Drain the notify pipe */
3498 len
= read(fd
, buffer
, sizeof(buffer
));
3499 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3502 static int qemu_event_init(void)
3511 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3515 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3519 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3520 (void *)(unsigned long)fds
[0]);
3522 io_thread_fd
= fds
[1];
3531 HANDLE qemu_event_handle
;
3533 static void dummy_event_handler(void *opaque
)
3537 static int qemu_event_init(void)
3539 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3540 if (!qemu_event_handle
) {
3541 perror("Failed CreateEvent");
3544 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3548 static void qemu_event_increment(void)
3550 SetEvent(qemu_event_handle
);
3554 static int cpu_can_run(CPUState
*env
)
3563 #ifndef CONFIG_IOTHREAD
3564 static int qemu_init_main_loop(void)
3566 return qemu_event_init();
3569 void qemu_init_vcpu(void *_env
)
3571 CPUState
*env
= _env
;
3578 int qemu_cpu_self(void *env
)
3583 static void resume_all_vcpus(void)
3587 static void pause_all_vcpus(void)
3591 void qemu_cpu_kick(void *env
)
3596 void qemu_notify_event(void)
3598 CPUState
*env
= cpu_single_env
;
3603 if (env
->kqemu_enabled
)
3604 kqemu_cpu_interrupt(env
);
3609 #define qemu_mutex_lock_iothread() do { } while (0)
3610 #define qemu_mutex_unlock_iothread() do { } while (0)
3612 void vm_stop(int reason
)
3617 #else /* CONFIG_IOTHREAD */
3619 #include "qemu-thread.h"
3621 QemuMutex qemu_global_mutex
;
3622 static QemuMutex qemu_fair_mutex
;
3624 static QemuThread io_thread
;
3626 static QemuThread
*tcg_cpu_thread
;
3627 static QemuCond
*tcg_halt_cond
;
3629 static int qemu_system_ready
;
3631 static QemuCond qemu_cpu_cond
;
3633 static QemuCond qemu_system_cond
;
3634 static QemuCond qemu_pause_cond
;
3636 static void block_io_signals(void);
3637 static void unblock_io_signals(void);
3638 static int tcg_has_work(void);
3640 static int qemu_init_main_loop(void)
3644 ret
= qemu_event_init();
3648 qemu_cond_init(&qemu_pause_cond
);
3649 qemu_mutex_init(&qemu_fair_mutex
);
3650 qemu_mutex_init(&qemu_global_mutex
);
3651 qemu_mutex_lock(&qemu_global_mutex
);
3653 unblock_io_signals();
3654 qemu_thread_self(&io_thread
);
3659 static void qemu_wait_io_event(CPUState
*env
)
3661 while (!tcg_has_work())
3662 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3664 qemu_mutex_unlock(&qemu_global_mutex
);
3667 * Users of qemu_global_mutex can be starved, having no chance
3668 * to acquire it since this path will get to it first.
3669 * So use another lock to provide fairness.
3671 qemu_mutex_lock(&qemu_fair_mutex
);
3672 qemu_mutex_unlock(&qemu_fair_mutex
);
3674 qemu_mutex_lock(&qemu_global_mutex
);
3678 qemu_cond_signal(&qemu_pause_cond
);
3682 static int qemu_cpu_exec(CPUState
*env
);
3684 static void *kvm_cpu_thread_fn(void *arg
)
3686 CPUState
*env
= arg
;
3689 qemu_thread_self(env
->thread
);
3691 /* signal CPU creation */
3692 qemu_mutex_lock(&qemu_global_mutex
);
3694 qemu_cond_signal(&qemu_cpu_cond
);
3696 /* and wait for machine initialization */
3697 while (!qemu_system_ready
)
3698 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3701 if (cpu_can_run(env
))
3703 qemu_wait_io_event(env
);
3709 static void tcg_cpu_exec(void);
3711 static void *tcg_cpu_thread_fn(void *arg
)
3713 CPUState
*env
= arg
;
3716 qemu_thread_self(env
->thread
);
3718 /* signal CPU creation */
3719 qemu_mutex_lock(&qemu_global_mutex
);
3720 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3722 qemu_cond_signal(&qemu_cpu_cond
);
3724 /* and wait for machine initialization */
3725 while (!qemu_system_ready
)
3726 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3730 qemu_wait_io_event(cur_cpu
);
3736 void qemu_cpu_kick(void *_env
)
3738 CPUState
*env
= _env
;
3739 qemu_cond_broadcast(env
->halt_cond
);
3741 qemu_thread_signal(env
->thread
, SIGUSR1
);
3744 int qemu_cpu_self(void *env
)
3746 return (cpu_single_env
!= NULL
);
3749 static void cpu_signal(int sig
)
3752 cpu_exit(cpu_single_env
);
3755 static void block_io_signals(void)
3758 struct sigaction sigact
;
3761 sigaddset(&set
, SIGUSR2
);
3762 sigaddset(&set
, SIGIO
);
3763 sigaddset(&set
, SIGALRM
);
3764 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3767 sigaddset(&set
, SIGUSR1
);
3768 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3770 memset(&sigact
, 0, sizeof(sigact
));
3771 sigact
.sa_handler
= cpu_signal
;
3772 sigaction(SIGUSR1
, &sigact
, NULL
);
3775 static void unblock_io_signals(void)
3780 sigaddset(&set
, SIGUSR2
);
3781 sigaddset(&set
, SIGIO
);
3782 sigaddset(&set
, SIGALRM
);
3783 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3786 sigaddset(&set
, SIGUSR1
);
3787 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3790 static void qemu_signal_lock(unsigned int msecs
)
3792 qemu_mutex_lock(&qemu_fair_mutex
);
3794 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3795 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3796 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3799 qemu_mutex_unlock(&qemu_fair_mutex
);
3802 static void qemu_mutex_lock_iothread(void)
3804 if (kvm_enabled()) {
3805 qemu_mutex_lock(&qemu_fair_mutex
);
3806 qemu_mutex_lock(&qemu_global_mutex
);
3807 qemu_mutex_unlock(&qemu_fair_mutex
);
3809 qemu_signal_lock(100);
3812 static void qemu_mutex_unlock_iothread(void)
3814 qemu_mutex_unlock(&qemu_global_mutex
);
3817 static int all_vcpus_paused(void)
3819 CPUState
*penv
= first_cpu
;
3824 penv
= (CPUState
*)penv
->next_cpu
;
3830 static void pause_all_vcpus(void)
3832 CPUState
*penv
= first_cpu
;
3836 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3837 qemu_cpu_kick(penv
);
3838 penv
= (CPUState
*)penv
->next_cpu
;
3841 while (!all_vcpus_paused()) {
3842 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3845 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3846 penv
= (CPUState
*)penv
->next_cpu
;
3851 static void resume_all_vcpus(void)
3853 CPUState
*penv
= first_cpu
;
3858 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3859 qemu_cpu_kick(penv
);
3860 penv
= (CPUState
*)penv
->next_cpu
;
3864 static void tcg_init_vcpu(void *_env
)
3866 CPUState
*env
= _env
;
3867 /* share a single thread for all cpus with TCG */
3868 if (!tcg_cpu_thread
) {
3869 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3870 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3871 qemu_cond_init(env
->halt_cond
);
3872 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3873 while (env
->created
== 0)
3874 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3875 tcg_cpu_thread
= env
->thread
;
3876 tcg_halt_cond
= env
->halt_cond
;
3878 env
->thread
= tcg_cpu_thread
;
3879 env
->halt_cond
= tcg_halt_cond
;
3883 static void kvm_start_vcpu(CPUState
*env
)
3886 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3887 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3888 qemu_cond_init(env
->halt_cond
);
3889 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3890 while (env
->created
== 0)
3891 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3894 void qemu_init_vcpu(void *_env
)
3896 CPUState
*env
= _env
;
3899 kvm_start_vcpu(env
);
3904 void qemu_notify_event(void)
3906 qemu_event_increment();
3909 void vm_stop(int reason
)
3912 qemu_thread_self(&me
);
3914 if (!qemu_thread_equal(&me
, &io_thread
)) {
3915 qemu_system_vmstop_request(reason
);
3917 * FIXME: should not return to device code in case
3918 * vm_stop() has been requested.
3920 if (cpu_single_env
) {
3921 cpu_exit(cpu_single_env
);
3922 cpu_single_env
->stop
= 1;
3933 static void host_main_loop_wait(int *timeout
)
3939 /* XXX: need to suppress polling by better using win32 events */
3941 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3942 ret
|= pe
->func(pe
->opaque
);
3946 WaitObjects
*w
= &wait_objects
;
3948 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3949 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3950 if (w
->func
[ret
- WAIT_OBJECT_0
])
3951 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3953 /* Check for additional signaled events */
3954 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3956 /* Check if event is signaled */
3957 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3958 if(ret2
== WAIT_OBJECT_0
) {
3960 w
->func
[i
](w
->opaque
[i
]);
3961 } else if (ret2
== WAIT_TIMEOUT
) {
3963 err
= GetLastError();
3964 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3967 } else if (ret
== WAIT_TIMEOUT
) {
3969 err
= GetLastError();
3970 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3977 static void host_main_loop_wait(int *timeout
)
3982 void main_loop_wait(int timeout
)
3984 IOHandlerRecord
*ioh
;
3985 fd_set rfds
, wfds
, xfds
;
3989 qemu_bh_update_timeout(&timeout
);
3991 host_main_loop_wait(&timeout
);
3993 /* poll any events */
3994 /* XXX: separate device handlers from system ones */
3999 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4003 (!ioh
->fd_read_poll
||
4004 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4005 FD_SET(ioh
->fd
, &rfds
);
4009 if (ioh
->fd_write
) {
4010 FD_SET(ioh
->fd
, &wfds
);
4016 tv
.tv_sec
= timeout
/ 1000;
4017 tv
.tv_usec
= (timeout
% 1000) * 1000;
4019 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4021 qemu_mutex_unlock_iothread();
4022 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4023 qemu_mutex_lock_iothread();
4025 IOHandlerRecord
**pioh
;
4027 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4028 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4029 ioh
->fd_read(ioh
->opaque
);
4031 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4032 ioh
->fd_write(ioh
->opaque
);
4036 /* remove deleted IO handlers */
4037 pioh
= &first_io_handler
;
4048 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4050 /* rearm timer, if not periodic */
4051 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4052 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4053 qemu_rearm_alarm_timer(alarm_timer
);
4056 /* vm time timers */
4058 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4059 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4060 qemu_get_clock(vm_clock
));
4063 /* real time timers */
4064 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4065 qemu_get_clock(rt_clock
));
4067 /* Check bottom-halves last in case any of the earlier events triggered
4073 static int qemu_cpu_exec(CPUState
*env
)
4076 #ifdef CONFIG_PROFILER
4080 #ifdef CONFIG_PROFILER
4081 ti
= profile_getclock();
4086 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4087 env
->icount_decr
.u16
.low
= 0;
4088 env
->icount_extra
= 0;
4089 count
= qemu_next_deadline();
4090 count
= (count
+ (1 << icount_time_shift
) - 1)
4091 >> icount_time_shift
;
4092 qemu_icount
+= count
;
4093 decr
= (count
> 0xffff) ? 0xffff : count
;
4095 env
->icount_decr
.u16
.low
= decr
;
4096 env
->icount_extra
= count
;
4098 ret
= cpu_exec(env
);
4099 #ifdef CONFIG_PROFILER
4100 qemu_time
+= profile_getclock() - ti
;
4103 /* Fold pending instructions back into the
4104 instruction counter, and clear the interrupt flag. */
4105 qemu_icount
-= (env
->icount_decr
.u16
.low
4106 + env
->icount_extra
);
4107 env
->icount_decr
.u32
= 0;
4108 env
->icount_extra
= 0;
4113 static void tcg_cpu_exec(void)
4117 if (next_cpu
== NULL
)
4118 next_cpu
= first_cpu
;
4119 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4120 CPUState
*env
= cur_cpu
= next_cpu
;
4124 if (timer_alarm_pending
) {
4125 timer_alarm_pending
= 0;
4128 if (cpu_can_run(env
))
4129 ret
= qemu_cpu_exec(env
);
4130 if (ret
== EXCP_DEBUG
) {
4131 gdb_set_stop_cpu(env
);
4132 debug_requested
= 1;
4138 static int cpu_has_work(CPUState
*env
)
4146 if (qemu_cpu_has_work(env
))
4151 static int tcg_has_work(void)
4155 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4156 if (cpu_has_work(env
))
4161 static int qemu_calculate_timeout(void)
4163 #ifndef CONFIG_IOTHREAD
4168 else if (tcg_has_work())
4170 else if (!use_icount
)
4173 /* XXX: use timeout computed from timers */
4176 /* Advance virtual time to the next event. */
4177 if (use_icount
== 1) {
4178 /* When not using an adaptive execution frequency
4179 we tend to get badly out of sync with real time,
4180 so just delay for a reasonable amount of time. */
4183 delta
= cpu_get_icount() - cpu_get_clock();
4186 /* If virtual time is ahead of real time then just
4188 timeout
= (delta
/ 1000000) + 1;
4190 /* Wait for either IO to occur or the next
4192 add
= qemu_next_deadline();
4193 /* We advance the timer before checking for IO.
4194 Limit the amount we advance so that early IO
4195 activity won't get the guest too far ahead. */
4199 add
= (add
+ (1 << icount_time_shift
) - 1)
4200 >> icount_time_shift
;
4202 timeout
= delta
/ 1000000;
4209 #else /* CONFIG_IOTHREAD */
4214 static int vm_can_run(void)
4216 if (powerdown_requested
)
4218 if (reset_requested
)
4220 if (shutdown_requested
)
4222 if (debug_requested
)
4227 qemu_irq qemu_system_powerdown
;
4229 static void main_loop(void)
4233 #ifdef CONFIG_IOTHREAD
4234 qemu_system_ready
= 1;
4235 qemu_cond_broadcast(&qemu_system_cond
);
4240 #ifdef CONFIG_PROFILER
4243 #ifndef CONFIG_IOTHREAD
4246 #ifdef CONFIG_PROFILER
4247 ti
= profile_getclock();
4249 main_loop_wait(qemu_calculate_timeout());
4250 #ifdef CONFIG_PROFILER
4251 dev_time
+= profile_getclock() - ti
;
4253 } while (vm_can_run());
4255 if (qemu_debug_requested())
4256 vm_stop(EXCP_DEBUG
);
4257 if (qemu_shutdown_requested()) {
4264 if (qemu_reset_requested()) {
4266 qemu_system_reset();
4269 if (qemu_powerdown_requested()) {
4270 qemu_irq_raise(qemu_system_powerdown
);
4272 if ((r
= qemu_vmstop_requested()))
4278 static void version(void)
4280 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4283 static void help(int exitcode
)
4286 printf("usage: %s [options] [disk_image]\n"
4288 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4290 #define DEF(option, opt_arg, opt_enum, opt_help) \
4292 #define DEFHEADING(text) stringify(text) "\n"
4293 #include "qemu-options.h"
4298 "During emulation, the following keys are useful:\n"
4299 "ctrl-alt-f toggle full screen\n"
4300 "ctrl-alt-n switch to virtual console 'n'\n"
4301 "ctrl-alt toggle mouse and keyboard grab\n"
4303 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4308 DEFAULT_NETWORK_SCRIPT
,
4309 DEFAULT_NETWORK_DOWN_SCRIPT
,
4311 DEFAULT_GDBSTUB_PORT
,
4316 #define HAS_ARG 0x0001
4319 #define DEF(option, opt_arg, opt_enum, opt_help) \
4321 #define DEFHEADING(text)
4322 #include "qemu-options.h"
4328 typedef struct QEMUOption
{
4334 static const QEMUOption qemu_options
[] = {
4335 { "h", 0, QEMU_OPTION_h
},
4336 #define DEF(option, opt_arg, opt_enum, opt_help) \
4337 { option, opt_arg, opt_enum },
4338 #define DEFHEADING(text)
4339 #include "qemu-options.h"
4347 struct soundhw soundhw
[] = {
4348 #ifdef HAS_AUDIO_CHOICE
4349 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4355 { .init_isa
= pcspk_audio_init
}
4362 "Creative Sound Blaster 16",
4365 { .init_isa
= SB16_init
}
4369 #ifdef CONFIG_CS4231A
4375 { .init_isa
= cs4231a_init
}
4383 "Yamaha YMF262 (OPL3)",
4385 "Yamaha YM3812 (OPL2)",
4389 { .init_isa
= Adlib_init
}
4396 "Gravis Ultrasound GF1",
4399 { .init_isa
= GUS_init
}
4406 "Intel 82801AA AC97 Audio",
4409 { .init_pci
= ac97_init
}
4413 #ifdef CONFIG_ES1370
4416 "ENSONIQ AudioPCI ES1370",
4419 { .init_pci
= es1370_init
}
4423 #endif /* HAS_AUDIO_CHOICE */
4425 { NULL
, NULL
, 0, 0, { NULL
} }
4428 static void select_soundhw (const char *optarg
)
4432 if (*optarg
== '?') {
4435 printf ("Valid sound card names (comma separated):\n");
4436 for (c
= soundhw
; c
->name
; ++c
) {
4437 printf ("%-11s %s\n", c
->name
, c
->descr
);
4439 printf ("\n-soundhw all will enable all of the above\n");
4440 exit (*optarg
!= '?');
4448 if (!strcmp (optarg
, "all")) {
4449 for (c
= soundhw
; c
->name
; ++c
) {
4457 e
= strchr (p
, ',');
4458 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4460 for (c
= soundhw
; c
->name
; ++c
) {
4461 if (!strncmp (c
->name
, p
, l
)) {
4470 "Unknown sound card name (too big to show)\n");
4473 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4478 p
+= l
+ (e
!= NULL
);
4482 goto show_valid_cards
;
4487 static void select_vgahw (const char *p
)
4491 cirrus_vga_enabled
= 0;
4492 std_vga_enabled
= 0;
4495 if (strstart(p
, "std", &opts
)) {
4496 std_vga_enabled
= 1;
4497 } else if (strstart(p
, "cirrus", &opts
)) {
4498 cirrus_vga_enabled
= 1;
4499 } else if (strstart(p
, "vmware", &opts
)) {
4501 } else if (strstart(p
, "xenfb", &opts
)) {
4503 } else if (!strstart(p
, "none", &opts
)) {
4505 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4509 const char *nextopt
;
4511 if (strstart(opts
, ",retrace=", &nextopt
)) {
4513 if (strstart(opts
, "dumb", &nextopt
))
4514 vga_retrace_method
= VGA_RETRACE_DUMB
;
4515 else if (strstart(opts
, "precise", &nextopt
))
4516 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4517 else goto invalid_vga
;
4518 } else goto invalid_vga
;
4524 static int balloon_parse(const char *arg
)
4529 if (!strcmp(arg
, "none")) {
4531 } else if (!strncmp(arg
, "virtio", 6)) {
4533 if (arg
[6] == ',') {
4535 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4536 virtio_balloon_devaddr
= strdup(buf
);
4547 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4549 exit(STATUS_CONTROL_C_EXIT
);
4554 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4558 if(strlen(str
) != 36)
4561 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4562 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4563 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4569 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4575 #define MAX_NET_CLIENTS 32
4579 static void termsig_handler(int signal
)
4581 qemu_system_shutdown_request();
4584 static void sigchld_handler(int signal
)
4586 waitpid(-1, NULL
, WNOHANG
);
4589 static void sighandler_setup(void)
4591 struct sigaction act
;
4593 memset(&act
, 0, sizeof(act
));
4594 act
.sa_handler
= termsig_handler
;
4595 sigaction(SIGINT
, &act
, NULL
);
4596 sigaction(SIGHUP
, &act
, NULL
);
4597 sigaction(SIGTERM
, &act
, NULL
);
4599 act
.sa_handler
= sigchld_handler
;
4600 act
.sa_flags
= SA_NOCLDSTOP
;
4601 sigaction(SIGCHLD
, &act
, NULL
);
4607 /* Look for support files in the same directory as the executable. */
4608 static char *find_datadir(const char *argv0
)
4614 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4621 while (p
!= buf
&& *p
!= '\\')
4624 if (access(buf
, R_OK
) == 0) {
4625 return qemu_strdup(buf
);
4631 /* Find a likely location for support files using the location of the binary.
4632 For installed binaries this will be "$bindir/../share/qemu". When
4633 running from the build tree this will be "$bindir/../pc-bios". */
4634 #define SHARE_SUFFIX "/share/qemu"
4635 #define BUILD_SUFFIX "/pc-bios"
4636 static char *find_datadir(const char *argv0
)
4646 #if defined(__linux__)
4649 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4655 #elif defined(__FreeBSD__)
4658 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4665 /* If we don't have any way of figuring out the actual executable
4666 location then try argv[0]. */
4671 p
= realpath(argv0
, p
);
4679 max_len
= strlen(dir
) +
4680 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4681 res
= qemu_mallocz(max_len
);
4682 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4683 if (access(res
, R_OK
)) {
4684 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4685 if (access(res
, R_OK
)) {
4699 char *qemu_find_file(int type
, const char *name
)
4705 /* If name contains path separators then try it as a straight path. */
4706 if ((strchr(name
, '/') || strchr(name
, '\\'))
4707 && access(name
, R_OK
) == 0) {
4708 return strdup(name
);
4711 case QEMU_FILE_TYPE_BIOS
:
4714 case QEMU_FILE_TYPE_KEYMAP
:
4715 subdir
= "keymaps/";
4720 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4721 buf
= qemu_mallocz(len
);
4722 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4723 if (access(buf
, R_OK
)) {
4730 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4734 dev
= qdev_device_add(opts
);
4740 struct device_config
{
4742 DEV_USB
, /* -usbdevice */
4745 const char *cmdline
;
4746 TAILQ_ENTRY(device_config
) next
;
4748 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4750 static void add_device_config(int type
, const char *cmdline
)
4752 struct device_config
*conf
;
4754 conf
= qemu_mallocz(sizeof(*conf
));
4756 conf
->cmdline
= cmdline
;
4757 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4760 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4762 struct device_config
*conf
;
4765 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4766 if (conf
->type
!= type
)
4768 rc
= func(conf
->cmdline
);
4775 int main(int argc
, char **argv
, char **envp
)
4777 const char *gdbstub_dev
= NULL
;
4778 uint32_t boot_devices_bitmap
= 0;
4780 int snapshot
, linux_boot
, net_boot
;
4781 const char *initrd_filename
;
4782 const char *kernel_filename
, *kernel_cmdline
;
4783 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4785 DisplayChangeListener
*dcl
;
4786 int cyls
, heads
, secs
, translation
;
4787 const char *net_clients
[MAX_NET_CLIENTS
];
4789 QemuOpts
*hda_opts
= NULL
, *opts
;
4791 const char *r
, *optarg
;
4792 CharDriverState
*monitor_hd
= NULL
;
4793 const char *monitor_device
;
4794 const char *serial_devices
[MAX_SERIAL_PORTS
];
4795 int serial_device_index
;
4796 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4797 int parallel_device_index
;
4798 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4799 int virtio_console_index
;
4800 const char *loadvm
= NULL
;
4801 QEMUMachine
*machine
;
4802 const char *cpu_model
;
4807 const char *pid_file
= NULL
;
4808 const char *incoming
= NULL
;
4811 struct passwd
*pwd
= NULL
;
4812 const char *chroot_dir
= NULL
;
4813 const char *run_as
= NULL
;
4816 int show_vnc_port
= 0;
4818 qemu_cache_utils_init(envp
);
4820 LIST_INIT (&vm_change_state_head
);
4823 struct sigaction act
;
4824 sigfillset(&act
.sa_mask
);
4826 act
.sa_handler
= SIG_IGN
;
4827 sigaction(SIGPIPE
, &act
, NULL
);
4830 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4831 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4832 QEMU to run on a single CPU */
4837 h
= GetCurrentProcess();
4838 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4839 for(i
= 0; i
< 32; i
++) {
4840 if (mask
& (1 << i
))
4845 SetProcessAffinityMask(h
, mask
);
4851 module_call_init(MODULE_INIT_MACHINE
);
4852 machine
= find_default_machine();
4854 initrd_filename
= NULL
;
4857 kernel_filename
= NULL
;
4858 kernel_cmdline
= "";
4859 cyls
= heads
= secs
= 0;
4860 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4861 monitor_device
= "vc:80Cx24C";
4863 serial_devices
[0] = "vc:80Cx24C";
4864 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4865 serial_devices
[i
] = NULL
;
4866 serial_device_index
= 0;
4868 parallel_devices
[0] = "vc:80Cx24C";
4869 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4870 parallel_devices
[i
] = NULL
;
4871 parallel_device_index
= 0;
4873 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4874 virtio_consoles
[i
] = NULL
;
4875 virtio_console_index
= 0;
4877 for (i
= 0; i
< MAX_NODES
; i
++) {
4879 node_cpumask
[i
] = 0;
4889 register_watchdogs();
4897 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4899 const QEMUOption
*popt
;
4902 /* Treat --foo the same as -foo. */
4905 popt
= qemu_options
;
4908 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4912 if (!strcmp(popt
->name
, r
+ 1))
4916 if (popt
->flags
& HAS_ARG
) {
4917 if (optind
>= argc
) {
4918 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4922 optarg
= argv
[optind
++];
4927 switch(popt
->index
) {
4929 machine
= find_machine(optarg
);
4932 printf("Supported machines are:\n");
4933 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4935 printf("%-10s %s (alias of %s)\n",
4936 m
->alias
, m
->desc
, m
->name
);
4937 printf("%-10s %s%s\n",
4939 m
->is_default
? " (default)" : "");
4941 exit(*optarg
!= '?');
4944 case QEMU_OPTION_cpu
:
4945 /* hw initialization will check this */
4946 if (*optarg
== '?') {
4947 /* XXX: implement xxx_cpu_list for targets that still miss it */
4948 #if defined(cpu_list)
4949 cpu_list(stdout
, &fprintf
);
4956 case QEMU_OPTION_initrd
:
4957 initrd_filename
= optarg
;
4959 case QEMU_OPTION_hda
:
4961 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4963 hda_opts
= drive_add(optarg
, HD_ALIAS
4964 ",cyls=%d,heads=%d,secs=%d%s",
4965 0, cyls
, heads
, secs
,
4966 translation
== BIOS_ATA_TRANSLATION_LBA
?
4968 translation
== BIOS_ATA_TRANSLATION_NONE
?
4969 ",trans=none" : "");
4971 case QEMU_OPTION_hdb
:
4972 case QEMU_OPTION_hdc
:
4973 case QEMU_OPTION_hdd
:
4974 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4976 case QEMU_OPTION_drive
:
4977 drive_add(NULL
, "%s", optarg
);
4979 case QEMU_OPTION_set
:
4980 if (qemu_set_option(optarg
) != 0)
4983 case QEMU_OPTION_mtdblock
:
4984 drive_add(optarg
, MTD_ALIAS
);
4986 case QEMU_OPTION_sd
:
4987 drive_add(optarg
, SD_ALIAS
);
4989 case QEMU_OPTION_pflash
:
4990 drive_add(optarg
, PFLASH_ALIAS
);
4992 case QEMU_OPTION_snapshot
:
4995 case QEMU_OPTION_hdachs
:
4999 cyls
= strtol(p
, (char **)&p
, 0);
5000 if (cyls
< 1 || cyls
> 16383)
5005 heads
= strtol(p
, (char **)&p
, 0);
5006 if (heads
< 1 || heads
> 16)
5011 secs
= strtol(p
, (char **)&p
, 0);
5012 if (secs
< 1 || secs
> 63)
5016 if (!strcmp(p
, "none"))
5017 translation
= BIOS_ATA_TRANSLATION_NONE
;
5018 else if (!strcmp(p
, "lba"))
5019 translation
= BIOS_ATA_TRANSLATION_LBA
;
5020 else if (!strcmp(p
, "auto"))
5021 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5024 } else if (*p
!= '\0') {
5026 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5029 if (hda_opts
!= NULL
) {
5031 snprintf(num
, sizeof(num
), "%d", cyls
);
5032 qemu_opt_set(hda_opts
, "cyls", num
);
5033 snprintf(num
, sizeof(num
), "%d", heads
);
5034 qemu_opt_set(hda_opts
, "heads", num
);
5035 snprintf(num
, sizeof(num
), "%d", secs
);
5036 qemu_opt_set(hda_opts
, "secs", num
);
5037 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5038 qemu_opt_set(hda_opts
, "trans", "lba");
5039 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5040 qemu_opt_set(hda_opts
, "trans", "none");
5044 case QEMU_OPTION_numa
:
5045 if (nb_numa_nodes
>= MAX_NODES
) {
5046 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5051 case QEMU_OPTION_nographic
:
5052 display_type
= DT_NOGRAPHIC
;
5054 #ifdef CONFIG_CURSES
5055 case QEMU_OPTION_curses
:
5056 display_type
= DT_CURSES
;
5059 case QEMU_OPTION_portrait
:
5062 case QEMU_OPTION_kernel
:
5063 kernel_filename
= optarg
;
5065 case QEMU_OPTION_append
:
5066 kernel_cmdline
= optarg
;
5068 case QEMU_OPTION_cdrom
:
5069 drive_add(optarg
, CDROM_ALIAS
);
5071 case QEMU_OPTION_boot
:
5073 static const char * const params
[] = {
5074 "order", "once", "menu", NULL
5076 char buf
[sizeof(boot_devices
)];
5077 char *standard_boot_devices
;
5080 if (!strchr(optarg
, '=')) {
5082 pstrcpy(buf
, sizeof(buf
), optarg
);
5083 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5085 "qemu: unknown boot parameter '%s' in '%s'\n",
5091 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5092 boot_devices_bitmap
= parse_bootdevices(buf
);
5093 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5096 if (get_param_value(buf
, sizeof(buf
),
5098 boot_devices_bitmap
|= parse_bootdevices(buf
);
5099 standard_boot_devices
= qemu_strdup(boot_devices
);
5100 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5101 qemu_register_reset(restore_boot_devices
,
5102 standard_boot_devices
);
5104 if (get_param_value(buf
, sizeof(buf
),
5106 if (!strcmp(buf
, "on")) {
5108 } else if (!strcmp(buf
, "off")) {
5112 "qemu: invalid option value '%s'\n",
5120 case QEMU_OPTION_fda
:
5121 case QEMU_OPTION_fdb
:
5122 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5125 case QEMU_OPTION_no_fd_bootchk
:
5129 case QEMU_OPTION_net
:
5130 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5131 fprintf(stderr
, "qemu: too many network clients\n");
5134 net_clients
[nb_net_clients
] = optarg
;
5138 case QEMU_OPTION_tftp
:
5139 legacy_tftp_prefix
= optarg
;
5141 case QEMU_OPTION_bootp
:
5142 legacy_bootp_filename
= optarg
;
5145 case QEMU_OPTION_smb
:
5146 net_slirp_smb(optarg
);
5149 case QEMU_OPTION_redir
:
5150 net_slirp_redir(optarg
);
5153 case QEMU_OPTION_bt
:
5154 add_device_config(DEV_BT
, optarg
);
5157 case QEMU_OPTION_audio_help
:
5161 case QEMU_OPTION_soundhw
:
5162 select_soundhw (optarg
);
5168 case QEMU_OPTION_version
:
5172 case QEMU_OPTION_m
: {
5176 value
= strtoul(optarg
, &ptr
, 10);
5178 case 0: case 'M': case 'm':
5185 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5189 /* On 32-bit hosts, QEMU is limited by virtual address space */
5190 if (value
> (2047 << 20)
5191 #ifndef CONFIG_KQEMU
5192 && HOST_LONG_BITS
== 32
5195 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5198 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5199 fprintf(stderr
, "qemu: ram size too large\n");
5208 const CPULogItem
*item
;
5210 mask
= cpu_str_to_log_mask(optarg
);
5212 printf("Log items (comma separated):\n");
5213 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5214 printf("%-10s %s\n", item
->name
, item
->help
);
5222 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5224 case QEMU_OPTION_gdb
:
5225 gdbstub_dev
= optarg
;
5230 case QEMU_OPTION_bios
:
5233 case QEMU_OPTION_singlestep
:
5241 keyboard_layout
= optarg
;
5244 case QEMU_OPTION_localtime
:
5247 case QEMU_OPTION_vga
:
5248 select_vgahw (optarg
);
5250 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5256 w
= strtol(p
, (char **)&p
, 10);
5259 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5265 h
= strtol(p
, (char **)&p
, 10);
5270 depth
= strtol(p
, (char **)&p
, 10);
5271 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5272 depth
!= 24 && depth
!= 32)
5274 } else if (*p
== '\0') {
5275 depth
= graphic_depth
;
5282 graphic_depth
= depth
;
5286 case QEMU_OPTION_echr
:
5289 term_escape_char
= strtol(optarg
, &r
, 0);
5291 printf("Bad argument to echr\n");
5294 case QEMU_OPTION_monitor
:
5295 monitor_device
= optarg
;
5297 case QEMU_OPTION_serial
:
5298 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5299 fprintf(stderr
, "qemu: too many serial ports\n");
5302 serial_devices
[serial_device_index
] = optarg
;
5303 serial_device_index
++;
5305 case QEMU_OPTION_watchdog
:
5306 i
= select_watchdog(optarg
);
5308 exit (i
== 1 ? 1 : 0);
5310 case QEMU_OPTION_watchdog_action
:
5311 if (select_watchdog_action(optarg
) == -1) {
5312 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5316 case QEMU_OPTION_virtiocon
:
5317 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5318 fprintf(stderr
, "qemu: too many virtio consoles\n");
5321 virtio_consoles
[virtio_console_index
] = optarg
;
5322 virtio_console_index
++;
5324 case QEMU_OPTION_parallel
:
5325 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5326 fprintf(stderr
, "qemu: too many parallel ports\n");
5329 parallel_devices
[parallel_device_index
] = optarg
;
5330 parallel_device_index
++;
5332 case QEMU_OPTION_loadvm
:
5335 case QEMU_OPTION_full_screen
:
5339 case QEMU_OPTION_no_frame
:
5342 case QEMU_OPTION_alt_grab
:
5345 case QEMU_OPTION_no_quit
:
5348 case QEMU_OPTION_sdl
:
5349 display_type
= DT_SDL
;
5352 case QEMU_OPTION_pidfile
:
5356 case QEMU_OPTION_win2k_hack
:
5357 win2k_install_hack
= 1;
5359 case QEMU_OPTION_rtc_td_hack
:
5362 case QEMU_OPTION_acpitable
:
5363 if(acpi_table_add(optarg
) < 0) {
5364 fprintf(stderr
, "Wrong acpi table provided\n");
5368 case QEMU_OPTION_smbios
:
5369 if(smbios_entry_add(optarg
) < 0) {
5370 fprintf(stderr
, "Wrong smbios provided\n");
5376 case QEMU_OPTION_enable_kqemu
:
5379 case QEMU_OPTION_kernel_kqemu
:
5384 case QEMU_OPTION_enable_kvm
:
5391 case QEMU_OPTION_usb
:
5394 case QEMU_OPTION_usbdevice
:
5396 add_device_config(DEV_USB
, optarg
);
5398 case QEMU_OPTION_device
:
5399 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5401 fprintf(stderr
, "parse error: %s\n", optarg
);
5405 case QEMU_OPTION_smp
:
5409 smp_cpus
= strtol(optarg
, &p
, 10);
5411 fprintf(stderr
, "Invalid number of CPUs\n");
5416 if (get_param_value(option
, 128, "maxcpus", p
))
5417 max_cpus
= strtol(option
, NULL
, 0);
5418 if (max_cpus
< smp_cpus
) {
5419 fprintf(stderr
, "maxcpus must be equal to or greater than "
5423 if (max_cpus
> 255) {
5424 fprintf(stderr
, "Unsupported number of maxcpus\n");
5429 case QEMU_OPTION_vnc
:
5430 display_type
= DT_VNC
;
5431 vnc_display
= optarg
;
5434 case QEMU_OPTION_no_acpi
:
5437 case QEMU_OPTION_no_hpet
:
5440 case QEMU_OPTION_balloon
:
5441 if (balloon_parse(optarg
) < 0) {
5442 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5447 case QEMU_OPTION_no_reboot
:
5450 case QEMU_OPTION_no_shutdown
:
5453 case QEMU_OPTION_show_cursor
:
5456 case QEMU_OPTION_uuid
:
5457 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5458 fprintf(stderr
, "Fail to parse UUID string."
5459 " Wrong format.\n");
5464 case QEMU_OPTION_daemonize
:
5468 case QEMU_OPTION_option_rom
:
5469 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5470 fprintf(stderr
, "Too many option ROMs\n");
5473 option_rom
[nb_option_roms
] = optarg
;
5476 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5477 case QEMU_OPTION_semihosting
:
5478 semihosting_enabled
= 1;
5481 case QEMU_OPTION_name
:
5482 qemu_name
= qemu_strdup(optarg
);
5484 char *p
= strchr(qemu_name
, ',');
5487 if (strncmp(p
, "process=", 8)) {
5488 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5496 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5497 case QEMU_OPTION_prom_env
:
5498 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5499 fprintf(stderr
, "Too many prom variables\n");
5502 prom_envs
[nb_prom_envs
] = optarg
;
5507 case QEMU_OPTION_old_param
:
5511 case QEMU_OPTION_clock
:
5512 configure_alarms(optarg
);
5514 case QEMU_OPTION_startdate
:
5517 time_t rtc_start_date
;
5518 if (!strcmp(optarg
, "now")) {
5519 rtc_date_offset
= -1;
5521 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5529 } else if (sscanf(optarg
, "%d-%d-%d",
5532 &tm
.tm_mday
) == 3) {
5541 rtc_start_date
= mktimegm(&tm
);
5542 if (rtc_start_date
== -1) {
5544 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5545 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5548 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5552 case QEMU_OPTION_tb_size
:
5553 tb_size
= strtol(optarg
, NULL
, 0);
5557 case QEMU_OPTION_icount
:
5559 if (strcmp(optarg
, "auto") == 0) {
5560 icount_time_shift
= -1;
5562 icount_time_shift
= strtol(optarg
, NULL
, 0);
5565 case QEMU_OPTION_incoming
:
5569 case QEMU_OPTION_chroot
:
5570 chroot_dir
= optarg
;
5572 case QEMU_OPTION_runas
:
5577 case QEMU_OPTION_xen_domid
:
5578 xen_domid
= atoi(optarg
);
5580 case QEMU_OPTION_xen_create
:
5581 xen_mode
= XEN_CREATE
;
5583 case QEMU_OPTION_xen_attach
:
5584 xen_mode
= XEN_ATTACH
;
5591 /* If no data_dir is specified then try to find it relative to the
5594 data_dir
= find_datadir(argv
[0]);
5596 /* If all else fails use the install patch specified when building. */
5598 data_dir
= CONFIG_QEMU_SHAREDIR
;
5601 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5602 if (kvm_allowed
&& kqemu_allowed
) {
5604 "You can not enable both KVM and kqemu at the same time\n");
5610 * Default to max_cpus = smp_cpus, in case the user doesn't
5611 * specify a max_cpus value.
5614 max_cpus
= smp_cpus
;
5616 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5617 if (smp_cpus
> machine
->max_cpus
) {
5618 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5619 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5624 if (display_type
== DT_NOGRAPHIC
) {
5625 if (serial_device_index
== 0)
5626 serial_devices
[0] = "stdio";
5627 if (parallel_device_index
== 0)
5628 parallel_devices
[0] = "null";
5629 if (strncmp(monitor_device
, "vc", 2) == 0)
5630 monitor_device
= "stdio";
5637 if (pipe(fds
) == -1)
5648 len
= read(fds
[0], &status
, 1);
5649 if (len
== -1 && (errno
== EINTR
))
5654 else if (status
== 1) {
5655 fprintf(stderr
, "Could not acquire pidfile\n");
5672 signal(SIGTSTP
, SIG_IGN
);
5673 signal(SIGTTOU
, SIG_IGN
);
5674 signal(SIGTTIN
, SIG_IGN
);
5677 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5680 write(fds
[1], &status
, 1);
5682 fprintf(stderr
, "Could not acquire pid file\n");
5691 if (qemu_init_main_loop()) {
5692 fprintf(stderr
, "qemu_init_main_loop failed\n");
5695 linux_boot
= (kernel_filename
!= NULL
);
5697 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5698 fprintf(stderr
, "-append only allowed with -kernel option\n");
5702 if (!linux_boot
&& initrd_filename
!= NULL
) {
5703 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5708 /* Win32 doesn't support line-buffering and requires size >= 2 */
5709 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5713 if (init_timer_alarm() < 0) {
5714 fprintf(stderr
, "could not initialize alarm timer\n");
5717 if (use_icount
&& icount_time_shift
< 0) {
5719 /* 125MIPS seems a reasonable initial guess at the guest speed.
5720 It will be corrected fairly quickly anyway. */
5721 icount_time_shift
= 3;
5722 init_icount_adjust();
5729 /* init network clients */
5730 if (nb_net_clients
== 0) {
5731 /* if no clients, we use a default config */
5732 net_clients
[nb_net_clients
++] = "nic";
5734 net_clients
[nb_net_clients
++] = "user";
5738 for(i
= 0;i
< nb_net_clients
; i
++) {
5739 if (net_client_parse(net_clients
[i
]) < 0)
5743 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5744 net_set_boot_mask(net_boot
);
5748 /* init the bluetooth world */
5749 if (foreach_device_config(DEV_BT
, bt_parse
))
5752 /* init the memory */
5754 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5757 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5758 guest ram allocation. It needs to go away. */
5759 if (kqemu_allowed
) {
5760 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5761 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5762 if (!kqemu_phys_ram_base
) {
5763 fprintf(stderr
, "Could not allocate physical memory\n");
5769 /* init the dynamic translator */
5770 cpu_exec_init_all(tb_size
* 1024 * 1024);
5774 /* we always create the cdrom drive, even if no disk is there */
5775 drive_add(NULL
, CDROM_ALIAS
);
5777 /* we always create at least one floppy */
5778 drive_add(NULL
, FD_ALIAS
, 0);
5780 /* we always create one sd slot, even if no card is in it */
5781 drive_add(NULL
, SD_ALIAS
);
5783 /* open the virtual block devices */
5785 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5786 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5789 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5790 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5793 /* must be after terminal init, SDL library changes signal handlers */
5797 /* Maintain compatibility with multiple stdio monitors */
5798 if (!strcmp(monitor_device
,"stdio")) {
5799 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5800 const char *devname
= serial_devices
[i
];
5801 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5802 monitor_device
= NULL
;
5804 } else if (devname
&& !strcmp(devname
,"stdio")) {
5805 monitor_device
= NULL
;
5806 serial_devices
[i
] = "mon:stdio";
5812 if (nb_numa_nodes
> 0) {
5815 if (nb_numa_nodes
> smp_cpus
) {
5816 nb_numa_nodes
= smp_cpus
;
5819 /* If no memory size if given for any node, assume the default case
5820 * and distribute the available memory equally across all nodes
5822 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5823 if (node_mem
[i
] != 0)
5826 if (i
== nb_numa_nodes
) {
5827 uint64_t usedmem
= 0;
5829 /* On Linux, the each node's border has to be 8MB aligned,
5830 * the final node gets the rest.
5832 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5833 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5834 usedmem
+= node_mem
[i
];
5836 node_mem
[i
] = ram_size
- usedmem
;
5839 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5840 if (node_cpumask
[i
] != 0)
5843 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5844 * must cope with this anyway, because there are BIOSes out there in
5845 * real machines which also use this scheme.
5847 if (i
== nb_numa_nodes
) {
5848 for (i
= 0; i
< smp_cpus
; i
++) {
5849 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5854 if (kvm_enabled()) {
5857 ret
= kvm_init(smp_cpus
);
5859 fprintf(stderr
, "failed to initialize KVM\n");
5864 if (monitor_device
) {
5865 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5867 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5872 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5873 const char *devname
= serial_devices
[i
];
5874 if (devname
&& strcmp(devname
, "none")) {
5876 snprintf(label
, sizeof(label
), "serial%d", i
);
5877 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5878 if (!serial_hds
[i
]) {
5879 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5886 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5887 const char *devname
= parallel_devices
[i
];
5888 if (devname
&& strcmp(devname
, "none")) {
5890 snprintf(label
, sizeof(label
), "parallel%d", i
);
5891 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5892 if (!parallel_hds
[i
]) {
5893 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5900 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5901 const char *devname
= virtio_consoles
[i
];
5902 if (devname
&& strcmp(devname
, "none")) {
5904 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5905 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5906 if (!virtcon_hds
[i
]) {
5907 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5914 module_call_init(MODULE_INIT_DEVICE
);
5916 if (machine
->compat_props
) {
5917 qdev_prop_register_compat(machine
->compat_props
);
5919 machine
->init(ram_size
, boot_devices
,
5920 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5923 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5924 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5925 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5931 current_machine
= machine
;
5933 /* init USB devices */
5935 foreach_device_config(DEV_USB
, usb_parse
);
5938 /* init generic devices */
5939 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5943 dumb_display_init();
5944 /* just use the first displaystate for the moment */
5947 if (display_type
== DT_DEFAULT
) {
5948 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5949 display_type
= DT_SDL
;
5951 display_type
= DT_VNC
;
5952 vnc_display
= "localhost:0,to=99";
5958 switch (display_type
) {
5961 #if defined(CONFIG_CURSES)
5963 curses_display_init(ds
, full_screen
);
5966 #if defined(CONFIG_SDL)
5968 sdl_display_init(ds
, full_screen
, no_frame
);
5970 #elif defined(CONFIG_COCOA)
5972 cocoa_display_init(ds
, full_screen
);
5976 vnc_display_init(ds
);
5977 if (vnc_display_open(ds
, vnc_display
) < 0)
5980 if (show_vnc_port
) {
5981 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5989 dcl
= ds
->listeners
;
5990 while (dcl
!= NULL
) {
5991 if (dcl
->dpy_refresh
!= NULL
) {
5992 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5993 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5998 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5999 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6000 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6003 text_consoles_set_display(display_state
);
6004 qemu_chr_initial_reset();
6006 if (monitor_device
&& monitor_hd
)
6007 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6009 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6010 const char *devname
= serial_devices
[i
];
6011 if (devname
&& strcmp(devname
, "none")) {
6012 if (strstart(devname
, "vc", 0))
6013 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6017 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6018 const char *devname
= parallel_devices
[i
];
6019 if (devname
&& strcmp(devname
, "none")) {
6020 if (strstart(devname
, "vc", 0))
6021 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6025 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6026 const char *devname
= virtio_consoles
[i
];
6027 if (virtcon_hds
[i
] && devname
) {
6028 if (strstart(devname
, "vc", 0))
6029 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6033 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6034 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6040 do_loadvm(cur_mon
, loadvm
);
6043 qemu_start_incoming_migration(incoming
);
6044 } else if (autostart
) {
6054 len
= write(fds
[1], &status
, 1);
6055 if (len
== -1 && (errno
== EINTR
))
6062 TFR(fd
= open("/dev/null", O_RDWR
));
6068 pwd
= getpwnam(run_as
);
6070 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6076 if (chroot(chroot_dir
) < 0) {
6077 fprintf(stderr
, "chroot failed\n");
6084 if (setgid(pwd
->pw_gid
) < 0) {
6085 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6088 if (setuid(pwd
->pw_uid
) < 0) {
6089 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6092 if (setuid(0) != -1) {
6093 fprintf(stderr
, "Dropping privileges failed\n");