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 HOST_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #if defined(__NetBSD__)
48 #include <net/if_tap.h>
51 #include <linux/if_tun.h>
53 #include <arpa/inet.h>
56 #include <sys/select.h>
59 #if defined(__FreeBSD__) || defined(__DragonFly__)
64 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65 #include <freebsd/stdlib.h>
70 #include <linux/rtc.h>
71 #include <sys/prctl.h>
73 /* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75 /* #include <linux/hpet.h> */
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
99 #if defined(__OpenBSD__)
103 #if defined(CONFIG_VDE)
104 #include <libvdeplug.h>
110 #include <sys/timeb.h>
111 #include <mmsystem.h>
112 #define getopt_long_only getopt_long
113 #define memalign(align, size) malloc(size)
117 #if defined(__APPLE__) || defined(main)
119 int qemu_main(int argc
, char **argv
, char **envp
);
120 int main(int argc
, char **argv
)
122 return qemu_main(argc
, argv
, NULL
);
125 #define main qemu_main
127 #endif /* CONFIG_SDL */
131 #define main qemu_main
132 #endif /* CONFIG_COCOA */
135 #include "hw/boards.h"
137 #include "hw/pcmcia.h"
139 #include "hw/audiodev.h"
143 #include "hw/watchdog.h"
144 #include "hw/smbios.h"
152 #include "qemu-timer.h"
153 #include "qemu-char.h"
154 #include "cache-utils.h"
157 #include "audio/audio.h"
158 #include "migration.h"
161 #include "qemu-option.h"
162 #include "qemu-kvm.h"
163 #include "hw/device-assignment.h"
167 #include "exec-all.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 /* Max number of USB devices that can be specified on the commandline. */
179 #define MAX_USB_CMDLINE 8
181 /* Max number of bluetooth switches on the commandline. */
182 #define MAX_BT_CMDLINE 10
184 static const char *data_dir
;
185 const char *bios_name
= NULL
;
186 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
187 to store the VM snapshots */
188 DriveInfo drives_table
[MAX_DRIVES
+1];
190 int extboot_drive
= -1;
191 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
192 static DisplayState
*display_state
;
193 DisplayType display_type
= DT_DEFAULT
;
194 const char* keyboard_layout
= NULL
;
195 int64_t ticks_per_sec
;
198 NICInfo nd_table
[MAX_NICS
];
200 static int autostart
;
201 static int rtc_utc
= 1;
202 static int rtc_date_offset
= -1; /* -1 means no change */
203 int cirrus_vga_enabled
= 1;
204 int std_vga_enabled
= 0;
205 int vmsvga_enabled
= 0;
206 int xenfb_enabled
= 0;
208 int graphic_width
= 1024;
209 int graphic_height
= 768;
210 int graphic_depth
= 8;
212 int graphic_width
= 800;
213 int graphic_height
= 600;
214 int graphic_depth
= 15;
216 static int full_screen
= 0;
218 static int no_frame
= 0;
221 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
222 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
223 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
225 int win2k_install_hack
= 0;
230 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
231 int assigned_devices_index
;
233 const char *vnc_display
;
234 int acpi_enabled
= 1;
238 int virtio_balloon
= 1;
239 const char *virtio_balloon_devaddr
;
244 int graphic_rotate
= 0;
245 uint8_t irq0override
= 1;
249 WatchdogTimerModel
*watchdog
= NULL
;
250 int watchdog_action
= WDT_RESET
;
251 const char *option_rom
[MAX_OPTION_ROMS
];
253 int semihosting_enabled
= 0;
254 int time_drift_fix
= 0;
255 unsigned int kvm_shadow_memory
= 0;
256 const char *mem_path
= NULL
;
258 int mem_prealloc
= 1; /* force preallocation of physical target memory */
263 const char *qemu_name
;
265 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
266 unsigned int nb_prom_envs
= 0;
267 const char *prom_envs
[MAX_PROM_ENVS
];
270 const char *nvram
= NULL
;
271 struct drive_opt drives_opt
[MAX_DRIVES
];
274 uint64_t node_mem
[MAX_NODES
];
275 uint64_t node_cpumask
[MAX_NODES
];
277 static CPUState
*cur_cpu
;
278 static CPUState
*next_cpu
;
279 static int timer_alarm_pending
= 1;
280 /* Conversion factor from emulated instructions to virtual clock ticks. */
281 static int icount_time_shift
;
282 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
283 #define MAX_ICOUNT_SHIFT 10
284 /* Compensate for varying guest execution speed. */
285 static int64_t qemu_icount_bias
;
286 static QEMUTimer
*icount_rt_timer
;
287 static QEMUTimer
*icount_vm_timer
;
288 static QEMUTimer
*nographic_timer
;
290 uint8_t qemu_uuid
[16];
292 /***********************************************************/
293 /* x86 ISA bus support */
295 target_phys_addr_t isa_mem_base
= 0;
298 /***********************************************************/
299 void hw_error(const char *fmt
, ...)
305 fprintf(stderr
, "qemu: hardware error: ");
306 vfprintf(stderr
, fmt
, ap
);
307 fprintf(stderr
, "\n");
308 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
309 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
311 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
313 cpu_dump_state(env
, stderr
, fprintf
, 0);
320 static void set_proc_name(const char *s
)
326 name
[sizeof(name
) - 1] = 0;
327 strncpy(name
, s
, sizeof(name
));
328 /* Could rewrite argv[0] too, but that's a bit more complicated.
329 This simple way is enough for `top'. */
330 prctl(PR_SET_NAME
, name
);
337 static QEMUBalloonEvent
*qemu_balloon_event
;
338 void *qemu_balloon_event_opaque
;
340 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
342 qemu_balloon_event
= func
;
343 qemu_balloon_event_opaque
= opaque
;
346 void qemu_balloon(ram_addr_t target
)
348 if (qemu_balloon_event
)
349 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
352 ram_addr_t
qemu_balloon_status(void)
354 if (qemu_balloon_event
)
355 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
359 /***********************************************************/
362 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
363 static void *qemu_put_kbd_event_opaque
;
364 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
365 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
367 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
369 qemu_put_kbd_event_opaque
= opaque
;
370 qemu_put_kbd_event
= func
;
373 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
374 void *opaque
, int absolute
,
377 QEMUPutMouseEntry
*s
, *cursor
;
379 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
381 s
->qemu_put_mouse_event
= func
;
382 s
->qemu_put_mouse_event_opaque
= opaque
;
383 s
->qemu_put_mouse_event_absolute
= absolute
;
384 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
387 if (!qemu_put_mouse_event_head
) {
388 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
392 cursor
= qemu_put_mouse_event_head
;
393 while (cursor
->next
!= NULL
)
394 cursor
= cursor
->next
;
397 qemu_put_mouse_event_current
= s
;
402 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
404 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
406 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
409 cursor
= qemu_put_mouse_event_head
;
410 while (cursor
!= NULL
&& cursor
!= entry
) {
412 cursor
= cursor
->next
;
415 if (cursor
== NULL
) // does not exist or list empty
417 else if (prev
== NULL
) { // entry is head
418 qemu_put_mouse_event_head
= cursor
->next
;
419 if (qemu_put_mouse_event_current
== entry
)
420 qemu_put_mouse_event_current
= cursor
->next
;
421 qemu_free(entry
->qemu_put_mouse_event_name
);
426 prev
->next
= entry
->next
;
428 if (qemu_put_mouse_event_current
== entry
)
429 qemu_put_mouse_event_current
= prev
;
431 qemu_free(entry
->qemu_put_mouse_event_name
);
435 void kbd_put_keycode(int keycode
)
437 if (qemu_put_kbd_event
) {
438 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
442 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
444 QEMUPutMouseEvent
*mouse_event
;
445 void *mouse_event_opaque
;
448 if (!qemu_put_mouse_event_current
) {
453 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
455 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
458 if (graphic_rotate
) {
459 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
462 width
= graphic_width
- 1;
463 mouse_event(mouse_event_opaque
,
464 width
- dy
, dx
, dz
, buttons_state
);
466 mouse_event(mouse_event_opaque
,
467 dx
, dy
, dz
, buttons_state
);
471 int kbd_mouse_is_absolute(void)
473 if (!qemu_put_mouse_event_current
)
476 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
479 void do_info_mice(Monitor
*mon
)
481 QEMUPutMouseEntry
*cursor
;
484 if (!qemu_put_mouse_event_head
) {
485 monitor_printf(mon
, "No mouse devices connected\n");
489 monitor_printf(mon
, "Mouse devices available:\n");
490 cursor
= qemu_put_mouse_event_head
;
491 while (cursor
!= NULL
) {
492 monitor_printf(mon
, "%c Mouse #%d: %s\n",
493 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
494 index
, cursor
->qemu_put_mouse_event_name
);
496 cursor
= cursor
->next
;
500 void do_mouse_set(Monitor
*mon
, int index
)
502 QEMUPutMouseEntry
*cursor
;
505 if (!qemu_put_mouse_event_head
) {
506 monitor_printf(mon
, "No mouse devices connected\n");
510 cursor
= qemu_put_mouse_event_head
;
511 while (cursor
!= NULL
&& index
!= i
) {
513 cursor
= cursor
->next
;
517 qemu_put_mouse_event_current
= cursor
;
519 monitor_printf(mon
, "Mouse at given index not found\n");
522 /* compute with 96 bit intermediate result: (a*b)/c */
523 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
528 #ifdef WORDS_BIGENDIAN
538 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
539 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
542 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
546 /***********************************************************/
547 /* real time host monotonic timer */
549 #define QEMU_TIMER_BASE 1000000000LL
553 static int64_t clock_freq
;
555 static void init_get_clock(void)
559 ret
= QueryPerformanceFrequency(&freq
);
561 fprintf(stderr
, "Could not calibrate ticks\n");
564 clock_freq
= freq
.QuadPart
;
567 static int64_t get_clock(void)
570 QueryPerformanceCounter(&ti
);
571 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
576 static int use_rt_clock
;
578 static void init_get_clock(void)
581 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
582 || defined(__DragonFly__)
585 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
592 static int64_t get_clock(void)
594 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
595 || defined(__DragonFly__)
598 clock_gettime(CLOCK_MONOTONIC
, &ts
);
599 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
603 /* XXX: using gettimeofday leads to problems if the date
604 changes, so it should be avoided. */
606 gettimeofday(&tv
, NULL
);
607 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
612 /* Return the virtual CPU time, based on the instruction counter. */
613 static int64_t cpu_get_icount(void)
616 CPUState
*env
= cpu_single_env
;;
617 icount
= qemu_icount
;
620 fprintf(stderr
, "Bad clock read\n");
621 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
623 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
626 /***********************************************************/
627 /* guest cycle counter */
629 static int64_t cpu_ticks_prev
;
630 static int64_t cpu_ticks_offset
;
631 static int64_t cpu_clock_offset
;
632 static int cpu_ticks_enabled
;
634 /* return the host CPU cycle counter and handle stop/restart */
635 int64_t cpu_get_ticks(void)
638 return cpu_get_icount();
640 if (!cpu_ticks_enabled
) {
641 return cpu_ticks_offset
;
644 ticks
= cpu_get_real_ticks();
645 if (cpu_ticks_prev
> ticks
) {
646 /* Note: non increasing ticks may happen if the host uses
648 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
650 cpu_ticks_prev
= ticks
;
651 return ticks
+ cpu_ticks_offset
;
655 /* return the host CPU monotonic timer and handle stop/restart */
656 static int64_t cpu_get_clock(void)
659 if (!cpu_ticks_enabled
) {
660 return cpu_clock_offset
;
663 return ti
+ cpu_clock_offset
;
667 /* enable cpu_get_ticks() */
668 void cpu_enable_ticks(void)
670 if (!cpu_ticks_enabled
) {
671 cpu_ticks_offset
-= cpu_get_real_ticks();
672 cpu_clock_offset
-= get_clock();
673 cpu_ticks_enabled
= 1;
677 /* disable cpu_get_ticks() : the clock is stopped. You must not call
678 cpu_get_ticks() after that. */
679 void cpu_disable_ticks(void)
681 if (cpu_ticks_enabled
) {
682 cpu_ticks_offset
= cpu_get_ticks();
683 cpu_clock_offset
= cpu_get_clock();
684 cpu_ticks_enabled
= 0;
688 /***********************************************************/
691 #define QEMU_TIMER_REALTIME 0
692 #define QEMU_TIMER_VIRTUAL 1
696 /* XXX: add frequency */
704 struct QEMUTimer
*next
;
707 struct qemu_alarm_timer
{
711 int (*start
)(struct qemu_alarm_timer
*t
);
712 void (*stop
)(struct qemu_alarm_timer
*t
);
713 void (*rearm
)(struct qemu_alarm_timer
*t
);
717 #define ALARM_FLAG_DYNTICKS 0x1
718 #define ALARM_FLAG_EXPIRED 0x2
720 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
722 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
725 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
727 if (!alarm_has_dynticks(t
))
733 /* TODO: MIN_TIMER_REARM_US should be optimized */
734 #define MIN_TIMER_REARM_US 250
736 static struct qemu_alarm_timer
*alarm_timer
;
740 struct qemu_alarm_win32
{
743 } alarm_win32_data
= {0, -1};
745 static int win32_start_timer(struct qemu_alarm_timer
*t
);
746 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
747 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
751 static int unix_start_timer(struct qemu_alarm_timer
*t
);
752 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
756 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
757 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
758 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
760 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
761 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
763 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
764 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
766 #endif /* __linux__ */
770 /* Correlation between real and virtual time is always going to be
771 fairly approximate, so ignore small variation.
772 When the guest is idle real and virtual time will be aligned in
774 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
776 static void icount_adjust(void)
781 static int64_t last_delta
;
782 /* If the VM is not running, then do nothing. */
786 cur_time
= cpu_get_clock();
787 cur_icount
= qemu_get_clock(vm_clock
);
788 delta
= cur_icount
- cur_time
;
789 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
791 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
792 && icount_time_shift
> 0) {
793 /* The guest is getting too far ahead. Slow time down. */
797 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
798 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
799 /* The guest is getting too far behind. Speed time up. */
803 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
806 static void icount_adjust_rt(void * opaque
)
808 qemu_mod_timer(icount_rt_timer
,
809 qemu_get_clock(rt_clock
) + 1000);
813 static void icount_adjust_vm(void * opaque
)
815 qemu_mod_timer(icount_vm_timer
,
816 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
820 static void init_icount_adjust(void)
822 /* Have both realtime and virtual time triggers for speed adjustment.
823 The realtime trigger catches emulated time passing too slowly,
824 the virtual time trigger catches emulated time passing too fast.
825 Realtime triggers occur even when idle, so use them less frequently
827 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
828 qemu_mod_timer(icount_rt_timer
,
829 qemu_get_clock(rt_clock
) + 1000);
830 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
831 qemu_mod_timer(icount_vm_timer
,
832 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
835 static struct qemu_alarm_timer alarm_timers
[] = {
838 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
839 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
840 /* HPET - if available - is preferred */
841 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
842 /* ...otherwise try RTC */
843 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
845 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
847 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
848 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
849 {"win32", 0, win32_start_timer
,
850 win32_stop_timer
, NULL
, &alarm_win32_data
},
855 static void show_available_alarms(void)
859 printf("Available alarm timers, in order of precedence:\n");
860 for (i
= 0; alarm_timers
[i
].name
; i
++)
861 printf("%s\n", alarm_timers
[i
].name
);
864 static void configure_alarms(char const *opt
)
868 int count
= ARRAY_SIZE(alarm_timers
) - 1;
871 struct qemu_alarm_timer tmp
;
873 if (!strcmp(opt
, "?")) {
874 show_available_alarms();
880 /* Reorder the array */
881 name
= strtok(arg
, ",");
883 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
884 if (!strcmp(alarm_timers
[i
].name
, name
))
889 fprintf(stderr
, "Unknown clock %s\n", name
);
898 tmp
= alarm_timers
[i
];
899 alarm_timers
[i
] = alarm_timers
[cur
];
900 alarm_timers
[cur
] = tmp
;
904 name
= strtok(NULL
, ",");
910 /* Disable remaining timers */
911 for (i
= cur
; i
< count
; i
++)
912 alarm_timers
[i
].name
= NULL
;
914 show_available_alarms();
922 static QEMUTimer
*active_timers
[2];
924 static QEMUClock
*qemu_new_clock(int type
)
927 clock
= qemu_mallocz(sizeof(QEMUClock
));
932 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
936 ts
= qemu_mallocz(sizeof(QEMUTimer
));
943 void qemu_free_timer(QEMUTimer
*ts
)
948 /* stop a timer, but do not dealloc it */
949 void qemu_del_timer(QEMUTimer
*ts
)
953 /* NOTE: this code must be signal safe because
954 qemu_timer_expired() can be called from a signal. */
955 pt
= &active_timers
[ts
->clock
->type
];
968 /* modify the current timer so that it will be fired when current_time
969 >= expire_time. The corresponding callback will be called. */
970 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
976 /* add the timer in the sorted list */
977 /* NOTE: this code must be signal safe because
978 qemu_timer_expired() can be called from a signal. */
979 pt
= &active_timers
[ts
->clock
->type
];
984 if (t
->expire_time
> expire_time
)
988 ts
->expire_time
= expire_time
;
992 /* Rearm if necessary */
993 if (pt
== &active_timers
[ts
->clock
->type
]) {
994 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
995 qemu_rearm_alarm_timer(alarm_timer
);
997 /* Interrupt execution to force deadline recalculation. */
1003 int qemu_timer_pending(QEMUTimer
*ts
)
1006 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1013 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1017 return (timer_head
->expire_time
<= current_time
);
1020 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1026 if (!ts
|| ts
->expire_time
> current_time
)
1028 /* remove timer from the list before calling the callback */
1029 *ptimer_head
= ts
->next
;
1032 /* run the callback (the timer list can be modified) */
1037 int64_t qemu_get_clock(QEMUClock
*clock
)
1039 switch(clock
->type
) {
1040 case QEMU_TIMER_REALTIME
:
1041 return get_clock() / 1000000;
1043 case QEMU_TIMER_VIRTUAL
:
1045 return cpu_get_icount();
1047 return cpu_get_clock();
1052 static void init_timers(void)
1055 ticks_per_sec
= QEMU_TIMER_BASE
;
1056 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1057 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1061 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1063 uint64_t expire_time
;
1065 if (qemu_timer_pending(ts
)) {
1066 expire_time
= ts
->expire_time
;
1070 qemu_put_be64(f
, expire_time
);
1073 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1075 uint64_t expire_time
;
1077 expire_time
= qemu_get_be64(f
);
1078 if (expire_time
!= -1) {
1079 qemu_mod_timer(ts
, expire_time
);
1085 static void timer_save(QEMUFile
*f
, void *opaque
)
1087 if (cpu_ticks_enabled
) {
1088 hw_error("cannot save state if virtual timers are running");
1090 qemu_put_be64(f
, cpu_ticks_offset
);
1091 qemu_put_be64(f
, ticks_per_sec
);
1092 qemu_put_be64(f
, cpu_clock_offset
);
1095 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1097 if (version_id
!= 1 && version_id
!= 2)
1099 if (cpu_ticks_enabled
) {
1102 cpu_ticks_offset
=qemu_get_be64(f
);
1103 ticks_per_sec
=qemu_get_be64(f
);
1104 if (version_id
== 2) {
1105 cpu_clock_offset
=qemu_get_be64(f
);
1110 static void qemu_event_increment(void);
1113 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1114 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1117 static void host_alarm_handler(int host_signum
)
1121 #define DISP_FREQ 1000
1123 static int64_t delta_min
= INT64_MAX
;
1124 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1126 ti
= qemu_get_clock(vm_clock
);
1127 if (last_clock
!= 0) {
1128 delta
= ti
- last_clock
;
1129 if (delta
< delta_min
)
1131 if (delta
> delta_max
)
1134 if (++count
== DISP_FREQ
) {
1135 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1136 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1137 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1138 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1139 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1141 delta_min
= INT64_MAX
;
1149 if (alarm_has_dynticks(alarm_timer
) ||
1151 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1152 qemu_get_clock(vm_clock
))) ||
1153 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1154 qemu_get_clock(rt_clock
))) {
1155 qemu_event_increment();
1156 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1158 #ifndef CONFIG_IOTHREAD
1160 /* stop the currently executing cpu because a timer occured */
1163 if (next_cpu
->kqemu_enabled
) {
1164 kqemu_cpu_interrupt(next_cpu
);
1169 timer_alarm_pending
= 1;
1170 qemu_notify_event();
1174 static int64_t qemu_next_deadline(void)
1178 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1179 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1180 qemu_get_clock(vm_clock
);
1182 /* To avoid problems with overflow limit this to 2^32. */
1192 #if defined(__linux__) || defined(_WIN32)
1193 static uint64_t qemu_next_deadline_dyntick(void)
1201 delta
= (qemu_next_deadline() + 999) / 1000;
1203 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1204 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1205 qemu_get_clock(rt_clock
))*1000;
1206 if (rtdelta
< delta
)
1210 if (delta
< MIN_TIMER_REARM_US
)
1211 delta
= MIN_TIMER_REARM_US
;
1219 /* Sets a specific flag */
1220 static int fcntl_setfl(int fd
, int flag
)
1224 flags
= fcntl(fd
, F_GETFL
);
1228 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1234 #if defined(__linux__)
1236 #define RTC_FREQ 1024
1238 static void enable_sigio_timer(int fd
)
1240 struct sigaction act
;
1243 sigfillset(&act
.sa_mask
);
1245 act
.sa_handler
= host_alarm_handler
;
1247 sigaction(SIGIO
, &act
, NULL
);
1248 fcntl_setfl(fd
, O_ASYNC
);
1249 fcntl(fd
, F_SETOWN
, getpid());
1252 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1254 struct hpet_info info
;
1257 fd
= open("/dev/hpet", O_RDONLY
);
1262 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1264 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1265 "error, but for better emulation accuracy type:\n"
1266 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1270 /* Check capabilities */
1271 r
= ioctl(fd
, HPET_INFO
, &info
);
1275 /* Enable periodic mode */
1276 r
= ioctl(fd
, HPET_EPI
, 0);
1277 if (info
.hi_flags
&& (r
< 0))
1280 /* Enable interrupt */
1281 r
= ioctl(fd
, HPET_IE_ON
, 0);
1285 enable_sigio_timer(fd
);
1286 t
->priv
= (void *)(long)fd
;
1294 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1296 int fd
= (long)t
->priv
;
1301 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1304 unsigned long current_rtc_freq
= 0;
1306 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1309 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1310 if (current_rtc_freq
!= RTC_FREQ
&&
1311 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1312 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1313 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1314 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1317 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1323 enable_sigio_timer(rtc_fd
);
1325 t
->priv
= (void *)(long)rtc_fd
;
1330 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1332 int rtc_fd
= (long)t
->priv
;
1337 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1341 struct sigaction act
;
1343 sigfillset(&act
.sa_mask
);
1345 act
.sa_handler
= host_alarm_handler
;
1347 sigaction(SIGALRM
, &act
, NULL
);
1350 * Initialize ev struct to 0 to avoid valgrind complaining
1351 * about uninitialized data in timer_create call
1353 memset(&ev
, 0, sizeof(ev
));
1354 ev
.sigev_value
.sival_int
= 0;
1355 ev
.sigev_notify
= SIGEV_SIGNAL
;
1356 ev
.sigev_signo
= SIGALRM
;
1358 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1359 perror("timer_create");
1361 /* disable dynticks */
1362 fprintf(stderr
, "Dynamic Ticks disabled\n");
1367 t
->priv
= (void *)(long)host_timer
;
1372 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1374 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1376 timer_delete(host_timer
);
1379 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1381 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1382 struct itimerspec timeout
;
1383 int64_t nearest_delta_us
= INT64_MAX
;
1386 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1387 !active_timers
[QEMU_TIMER_VIRTUAL
])
1390 nearest_delta_us
= qemu_next_deadline_dyntick();
1392 /* check whether a timer is already running */
1393 if (timer_gettime(host_timer
, &timeout
)) {
1395 fprintf(stderr
, "Internal timer error: aborting\n");
1398 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1399 if (current_us
&& current_us
<= nearest_delta_us
)
1402 timeout
.it_interval
.tv_sec
= 0;
1403 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1404 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1405 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1406 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1408 fprintf(stderr
, "Internal timer error: aborting\n");
1413 #endif /* defined(__linux__) */
1415 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1417 struct sigaction act
;
1418 struct itimerval itv
;
1422 sigfillset(&act
.sa_mask
);
1424 act
.sa_handler
= host_alarm_handler
;
1426 sigaction(SIGALRM
, &act
, NULL
);
1428 itv
.it_interval
.tv_sec
= 0;
1429 /* for i386 kernel 2.6 to get 1 ms */
1430 itv
.it_interval
.tv_usec
= 999;
1431 itv
.it_value
.tv_sec
= 0;
1432 itv
.it_value
.tv_usec
= 10 * 1000;
1434 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1441 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1443 struct itimerval itv
;
1445 memset(&itv
, 0, sizeof(itv
));
1446 setitimer(ITIMER_REAL
, &itv
, NULL
);
1449 #endif /* !defined(_WIN32) */
1454 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1457 struct qemu_alarm_win32
*data
= t
->priv
;
1460 memset(&tc
, 0, sizeof(tc
));
1461 timeGetDevCaps(&tc
, sizeof(tc
));
1463 if (data
->period
< tc
.wPeriodMin
)
1464 data
->period
= tc
.wPeriodMin
;
1466 timeBeginPeriod(data
->period
);
1468 flags
= TIME_CALLBACK_FUNCTION
;
1469 if (alarm_has_dynticks(t
))
1470 flags
|= TIME_ONESHOT
;
1472 flags
|= TIME_PERIODIC
;
1474 data
->timerId
= timeSetEvent(1, // interval (ms)
1475 data
->period
, // resolution
1476 host_alarm_handler
, // function
1477 (DWORD
)t
, // parameter
1480 if (!data
->timerId
) {
1481 perror("Failed to initialize win32 alarm timer");
1482 timeEndPeriod(data
->period
);
1489 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1491 struct qemu_alarm_win32
*data
= t
->priv
;
1493 timeKillEvent(data
->timerId
);
1494 timeEndPeriod(data
->period
);
1497 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1499 struct qemu_alarm_win32
*data
= t
->priv
;
1500 uint64_t nearest_delta_us
;
1502 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1503 !active_timers
[QEMU_TIMER_VIRTUAL
])
1506 nearest_delta_us
= qemu_next_deadline_dyntick();
1507 nearest_delta_us
/= 1000;
1509 timeKillEvent(data
->timerId
);
1511 data
->timerId
= timeSetEvent(1,
1515 TIME_ONESHOT
| TIME_PERIODIC
);
1517 if (!data
->timerId
) {
1518 perror("Failed to re-arm win32 alarm timer");
1520 timeEndPeriod(data
->period
);
1527 static int init_timer_alarm(void)
1529 struct qemu_alarm_timer
*t
= NULL
;
1532 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1533 t
= &alarm_timers
[i
];
1553 static void quit_timers(void)
1555 alarm_timer
->stop(alarm_timer
);
1559 /***********************************************************/
1560 /* host time/date access */
1561 void qemu_get_timedate(struct tm
*tm
, int offset
)
1568 if (rtc_date_offset
== -1) {
1572 ret
= localtime(&ti
);
1574 ti
-= rtc_date_offset
;
1578 memcpy(tm
, ret
, sizeof(struct tm
));
1581 int qemu_timedate_diff(struct tm
*tm
)
1585 if (rtc_date_offset
== -1)
1587 seconds
= mktimegm(tm
);
1589 seconds
= mktime(tm
);
1591 seconds
= mktimegm(tm
) + rtc_date_offset
;
1593 return seconds
- time(NULL
);
1597 static void socket_cleanup(void)
1602 static int socket_init(void)
1607 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1609 err
= WSAGetLastError();
1610 fprintf(stderr
, "WSAStartup: %d\n", err
);
1613 atexit(socket_cleanup
);
1618 int get_next_param_value(char *buf
, int buf_size
,
1619 const char *tag
, const char **pstr
)
1626 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1630 if (!strcmp(tag
, option
)) {
1631 *pstr
= get_opt_value(buf
, buf_size
, p
);
1632 if (**pstr
== ',') {
1637 p
= get_opt_value(NULL
, 0, p
);
1646 int get_param_value(char *buf
, int buf_size
,
1647 const char *tag
, const char *str
)
1649 return get_next_param_value(buf
, buf_size
, tag
, &str
);
1652 int check_params(char *buf
, int buf_size
,
1653 const char * const *params
, const char *str
)
1659 while (*p
!= '\0') {
1660 p
= get_opt_name(buf
, buf_size
, p
, '=');
1665 for (i
= 0; params
[i
] != NULL
; i
++) {
1666 if (!strcmp(params
[i
], buf
)) {
1670 if (params
[i
] == NULL
) {
1673 p
= get_opt_value(NULL
, 0, p
);
1682 /***********************************************************/
1683 /* Bluetooth support */
1686 static struct HCIInfo
*hci_table
[MAX_NICS
];
1688 static struct bt_vlan_s
{
1689 struct bt_scatternet_s net
;
1691 struct bt_vlan_s
*next
;
1694 /* find or alloc a new bluetooth "VLAN" */
1695 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1697 struct bt_vlan_s
**pvlan
, *vlan
;
1698 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1702 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1704 pvlan
= &first_bt_vlan
;
1705 while (*pvlan
!= NULL
)
1706 pvlan
= &(*pvlan
)->next
;
1711 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1715 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1720 static struct HCIInfo null_hci
= {
1721 .cmd_send
= null_hci_send
,
1722 .sco_send
= null_hci_send
,
1723 .acl_send
= null_hci_send
,
1724 .bdaddr_set
= null_hci_addr_set
,
1727 struct HCIInfo
*qemu_next_hci(void)
1729 if (cur_hci
== nb_hcis
)
1732 return hci_table
[cur_hci
++];
1735 static struct HCIInfo
*hci_init(const char *str
)
1738 struct bt_scatternet_s
*vlan
= 0;
1740 if (!strcmp(str
, "null"))
1743 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1745 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1746 else if (!strncmp(str
, "hci", 3)) {
1749 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1750 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1755 vlan
= qemu_find_bt_vlan(0);
1757 return bt_new_hci(vlan
);
1760 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1765 static int bt_hci_parse(const char *str
)
1767 struct HCIInfo
*hci
;
1770 if (nb_hcis
>= MAX_NICS
) {
1771 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1775 hci
= hci_init(str
);
1784 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1785 hci
->bdaddr_set(hci
, bdaddr
.b
);
1787 hci_table
[nb_hcis
++] = hci
;
1792 static void bt_vhci_add(int vlan_id
)
1794 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1797 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1798 "an empty scatternet %i\n", vlan_id
);
1800 bt_vhci_init(bt_new_hci(vlan
));
1803 static struct bt_device_s
*bt_device_add(const char *opt
)
1805 struct bt_scatternet_s
*vlan
;
1807 char *endp
= strstr(opt
, ",vlan=");
1808 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1811 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1814 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1816 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1821 vlan
= qemu_find_bt_vlan(vlan_id
);
1824 fprintf(stderr
, "qemu: warning: adding a slave device to "
1825 "an empty scatternet %i\n", vlan_id
);
1827 if (!strcmp(devname
, "keyboard"))
1828 return bt_keyboard_init(vlan
);
1830 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1834 static int bt_parse(const char *opt
)
1836 const char *endp
, *p
;
1839 if (strstart(opt
, "hci", &endp
)) {
1840 if (!*endp
|| *endp
== ',') {
1842 if (!strstart(endp
, ",vlan=", 0))
1845 return bt_hci_parse(opt
);
1847 } else if (strstart(opt
, "vhci", &endp
)) {
1848 if (!*endp
|| *endp
== ',') {
1850 if (strstart(endp
, ",vlan=", &p
)) {
1851 vlan
= strtol(p
, (char **) &endp
, 0);
1853 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1857 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1866 } else if (strstart(opt
, "device:", &endp
))
1867 return !bt_device_add(endp
);
1869 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1873 /***********************************************************/
1874 /* QEMU Block devices */
1876 #define HD_ALIAS "index=%d,media=disk"
1877 #define CDROM_ALIAS "index=2,media=cdrom"
1878 #define FD_ALIAS "index=%d,if=floppy"
1879 #define PFLASH_ALIAS "if=pflash"
1880 #define MTD_ALIAS "if=mtd"
1881 #define SD_ALIAS "index=0,if=sd"
1883 static int drive_opt_get_free_idx(void)
1887 for (index
= 0; index
< MAX_DRIVES
; index
++)
1888 if (!drives_opt
[index
].used
) {
1889 drives_opt
[index
].used
= 1;
1896 static int drive_get_free_idx(void)
1900 for (index
= 0; index
< MAX_DRIVES
; index
++)
1901 if (!drives_table
[index
].used
) {
1902 drives_table
[index
].used
= 1;
1909 int drive_add(const char *file
, const char *fmt
, ...)
1912 int index
= drive_opt_get_free_idx();
1914 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
1915 fprintf(stderr
, "qemu: too many drives\n");
1919 drives_opt
[index
].file
= file
;
1921 vsnprintf(drives_opt
[index
].opt
,
1922 sizeof(drives_opt
[0].opt
), fmt
, ap
);
1929 void drive_remove(int index
)
1931 drives_opt
[index
].used
= 0;
1935 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
1939 /* seek interface, bus and unit */
1941 for (index
= 0; index
< MAX_DRIVES
; index
++)
1942 if (drives_table
[index
].type
== type
&&
1943 drives_table
[index
].bus
== bus
&&
1944 drives_table
[index
].unit
== unit
&&
1945 drives_table
[index
].used
)
1951 int drive_get_max_bus(BlockInterfaceType type
)
1957 for (index
= 0; index
< nb_drives
; index
++) {
1958 if(drives_table
[index
].type
== type
&&
1959 drives_table
[index
].bus
> max_bus
)
1960 max_bus
= drives_table
[index
].bus
;
1965 const char *drive_get_serial(BlockDriverState
*bdrv
)
1969 for (index
= 0; index
< nb_drives
; index
++)
1970 if (drives_table
[index
].bdrv
== bdrv
)
1971 return drives_table
[index
].serial
;
1976 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1980 for (index
= 0; index
< nb_drives
; index
++)
1981 if (drives_table
[index
].bdrv
== bdrv
)
1982 return drives_table
[index
].onerror
;
1984 return BLOCK_ERR_STOP_ENOSPC
;
1987 static void bdrv_format_print(void *opaque
, const char *name
)
1989 fprintf(stderr
, " %s", name
);
1992 void drive_uninit(BlockDriverState
*bdrv
)
1996 for (i
= 0; i
< MAX_DRIVES
; i
++)
1997 if (drives_table
[i
].bdrv
== bdrv
) {
1998 drives_table
[i
].bdrv
= NULL
;
1999 drives_table
[i
].used
= 0;
2000 drive_remove(drives_table
[i
].drive_opt_idx
);
2006 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2012 const char *mediastr
= "";
2013 BlockInterfaceType type
;
2014 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2015 int bus_id
, unit_id
;
2016 int cyls
, heads
, secs
, translation
;
2017 BlockDriverState
*bdrv
;
2018 BlockDriver
*drv
= NULL
;
2019 QEMUMachine
*machine
= opaque
;
2023 int bdrv_flags
, onerror
;
2024 const char *devaddr
;
2025 int drives_table_idx
;
2026 char *str
= arg
->opt
;
2027 static const char * const params
[] = { "bus", "unit", "if", "index",
2028 "cyls", "heads", "secs", "trans",
2029 "media", "snapshot", "file",
2030 "cache", "format", "serial",
2035 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2036 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2042 cyls
= heads
= secs
= 0;
2045 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2049 if (machine
->use_scsi
) {
2051 max_devs
= MAX_SCSI_DEVS
;
2052 pstrcpy(devname
, sizeof(devname
), "scsi");
2055 max_devs
= MAX_IDE_DEVS
;
2056 pstrcpy(devname
, sizeof(devname
), "ide");
2060 /* extract parameters */
2062 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2063 bus_id
= strtol(buf
, NULL
, 0);
2065 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2070 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2071 unit_id
= strtol(buf
, NULL
, 0);
2073 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2078 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2079 pstrcpy(devname
, sizeof(devname
), buf
);
2080 if (!strcmp(buf
, "ide")) {
2082 max_devs
= MAX_IDE_DEVS
;
2083 } else if (!strcmp(buf
, "scsi")) {
2085 max_devs
= MAX_SCSI_DEVS
;
2086 } else if (!strcmp(buf
, "floppy")) {
2089 } else if (!strcmp(buf
, "pflash")) {
2092 } else if (!strcmp(buf
, "mtd")) {
2095 } else if (!strcmp(buf
, "sd")) {
2098 } else if (!strcmp(buf
, "virtio")) {
2101 } else if (!strcmp(buf
, "xen")) {
2105 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2110 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2111 index
= strtol(buf
, NULL
, 0);
2113 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2118 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2119 cyls
= strtol(buf
, NULL
, 0);
2122 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2123 heads
= strtol(buf
, NULL
, 0);
2126 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2127 secs
= strtol(buf
, NULL
, 0);
2130 if (cyls
|| heads
|| secs
) {
2131 if (cyls
< 1 || cyls
> 16383) {
2132 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2135 if (heads
< 1 || heads
> 16) {
2136 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2139 if (secs
< 1 || secs
> 63) {
2140 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2145 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2148 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2152 if (!strcmp(buf
, "none"))
2153 translation
= BIOS_ATA_TRANSLATION_NONE
;
2154 else if (!strcmp(buf
, "lba"))
2155 translation
= BIOS_ATA_TRANSLATION_LBA
;
2156 else if (!strcmp(buf
, "auto"))
2157 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2159 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2164 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2165 if (!strcmp(buf
, "disk")) {
2167 } else if (!strcmp(buf
, "cdrom")) {
2168 if (cyls
|| secs
|| heads
) {
2170 "qemu: '%s' invalid physical CHS format\n", str
);
2173 media
= MEDIA_CDROM
;
2175 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2180 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2181 if (!strcmp(buf
, "on"))
2183 else if (!strcmp(buf
, "off"))
2186 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2191 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2192 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2194 else if (!strcmp(buf
, "writethrough"))
2196 else if (!strcmp(buf
, "writeback"))
2199 fprintf(stderr
, "qemu: invalid cache option\n");
2204 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2205 if (strcmp(buf
, "?") == 0) {
2206 fprintf(stderr
, "qemu: Supported formats:");
2207 bdrv_iterate_format(bdrv_format_print
, NULL
);
2208 fprintf(stderr
, "\n");
2211 drv
= bdrv_find_format(buf
);
2213 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2218 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2219 if (!strcmp(buf
, "on")) {
2220 if (extboot_drive
!= -1) {
2221 fprintf(stderr
, "qemu: two bootable drives specified\n");
2224 extboot_drive
= nb_drives
;
2225 } else if (strcmp(buf
, "off")) {
2226 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2231 if (arg
->file
== NULL
)
2232 get_param_value(file
, sizeof(file
), "file", str
);
2234 pstrcpy(file
, sizeof(file
), arg
->file
);
2236 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2237 memset(serial
, 0, sizeof(serial
));
2239 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2240 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2241 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2242 fprintf(stderr
, "werror is no supported by this format\n");
2245 if (!strcmp(buf
, "ignore"))
2246 onerror
= BLOCK_ERR_IGNORE
;
2247 else if (!strcmp(buf
, "enospc"))
2248 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2249 else if (!strcmp(buf
, "stop"))
2250 onerror
= BLOCK_ERR_STOP_ANY
;
2251 else if (!strcmp(buf
, "report"))
2252 onerror
= BLOCK_ERR_REPORT
;
2254 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2260 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2261 if (type
!= IF_VIRTIO
) {
2262 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2265 devaddr
= strdup(buf
);
2268 /* compute bus and unit according index */
2271 if (bus_id
!= 0 || unit_id
!= -1) {
2273 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2281 unit_id
= index
% max_devs
;
2282 bus_id
= index
/ max_devs
;
2286 /* if user doesn't specify a unit_id,
2287 * try to find the first free
2290 if (unit_id
== -1) {
2292 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2294 if (max_devs
&& unit_id
>= max_devs
) {
2295 unit_id
-= max_devs
;
2303 if (max_devs
&& unit_id
>= max_devs
) {
2304 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2305 str
, unit_id
, max_devs
- 1);
2310 * ignore multiple definitions
2313 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2318 if (type
== IF_IDE
|| type
== IF_SCSI
)
2319 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2321 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2322 devname
, bus_id
, mediastr
, unit_id
);
2324 snprintf(buf
, sizeof(buf
), "%s%s%i",
2325 devname
, mediastr
, unit_id
);
2326 bdrv
= bdrv_new(buf
);
2327 drives_table_idx
= drive_get_free_idx();
2328 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2329 drives_table
[drives_table_idx
].devaddr
= devaddr
;
2330 drives_table
[drives_table_idx
].type
= type
;
2331 drives_table
[drives_table_idx
].bus
= bus_id
;
2332 drives_table
[drives_table_idx
].unit
= unit_id
;
2333 drives_table
[drives_table_idx
].onerror
= onerror
;
2334 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2335 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2345 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2346 bdrv_set_translation_hint(bdrv
, translation
);
2350 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2355 /* FIXME: This isn't really a floppy, but it's a reasonable
2358 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2371 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2372 cache
= 2; /* always use write-back with snapshot */
2374 if (cache
== 0) /* no caching */
2375 bdrv_flags
|= BDRV_O_NOCACHE
;
2376 else if (cache
== 2) /* write-back */
2377 bdrv_flags
|= BDRV_O_CACHE_WB
;
2378 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2379 fprintf(stderr
, "qemu: could not open disk image %s\n",
2383 if (bdrv_key_required(bdrv
))
2385 return drives_table_idx
;
2388 static void numa_add(const char *optarg
)
2392 unsigned long long value
, endvalue
;
2395 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2396 if (!strcmp(option
, "node")) {
2397 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2398 nodenr
= nb_numa_nodes
;
2400 nodenr
= strtoull(option
, NULL
, 10);
2403 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2404 node_mem
[nodenr
] = 0;
2406 value
= strtoull(option
, &endptr
, 0);
2408 case 0: case 'M': case 'm':
2415 node_mem
[nodenr
] = value
;
2417 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2418 node_cpumask
[nodenr
] = 0;
2420 value
= strtoull(option
, &endptr
, 10);
2423 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2425 if (*endptr
== '-') {
2426 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2427 if (endvalue
>= 63) {
2430 "only 63 CPUs in NUMA mode supported.\n");
2432 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2437 node_cpumask
[nodenr
] = value
;
2444 /***********************************************************/
2447 static USBPort
*used_usb_ports
;
2448 static USBPort
*free_usb_ports
;
2450 /* ??? Maybe change this to register a hub to keep track of the topology. */
2451 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2452 usb_attachfn attach
)
2454 port
->opaque
= opaque
;
2455 port
->index
= index
;
2456 port
->attach
= attach
;
2457 port
->next
= free_usb_ports
;
2458 free_usb_ports
= port
;
2461 int usb_device_add_dev(USBDevice
*dev
)
2465 /* Find a USB port to add the device to. */
2466 port
= free_usb_ports
;
2470 /* Create a new hub and chain it on. */
2471 free_usb_ports
= NULL
;
2472 port
->next
= used_usb_ports
;
2473 used_usb_ports
= port
;
2475 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2476 usb_attach(port
, hub
);
2477 port
= free_usb_ports
;
2480 free_usb_ports
= port
->next
;
2481 port
->next
= used_usb_ports
;
2482 used_usb_ports
= port
;
2483 usb_attach(port
, dev
);
2487 static void usb_msd_password_cb(void *opaque
, int err
)
2489 USBDevice
*dev
= opaque
;
2492 usb_device_add_dev(dev
);
2494 dev
->handle_destroy(dev
);
2497 static int usb_device_add(const char *devname
, int is_hotplug
)
2502 if (!free_usb_ports
)
2505 if (strstart(devname
, "host:", &p
)) {
2506 dev
= usb_host_device_open(p
);
2507 } else if (!strcmp(devname
, "mouse")) {
2508 dev
= usb_mouse_init();
2509 } else if (!strcmp(devname
, "tablet")) {
2510 dev
= usb_tablet_init();
2511 } else if (!strcmp(devname
, "keyboard")) {
2512 dev
= usb_keyboard_init();
2513 } else if (strstart(devname
, "disk:", &p
)) {
2514 BlockDriverState
*bs
;
2516 dev
= usb_msd_init(p
);
2519 bs
= usb_msd_get_bdrv(dev
);
2520 if (bdrv_key_required(bs
)) {
2523 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2528 } else if (!strcmp(devname
, "wacom-tablet")) {
2529 dev
= usb_wacom_init();
2530 } else if (strstart(devname
, "serial:", &p
)) {
2531 dev
= usb_serial_init(p
);
2532 #ifdef CONFIG_BRLAPI
2533 } else if (!strcmp(devname
, "braille")) {
2534 dev
= usb_baum_init();
2536 } else if (strstart(devname
, "net:", &p
)) {
2539 if (net_client_init(NULL
, "nic", p
) < 0)
2541 nd_table
[nic
].model
= "usb";
2542 dev
= usb_net_init(&nd_table
[nic
]);
2543 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2544 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2545 bt_new_hci(qemu_find_bt_vlan(0)));
2552 return usb_device_add_dev(dev
);
2555 int usb_device_del_addr(int bus_num
, int addr
)
2561 if (!used_usb_ports
)
2567 lastp
= &used_usb_ports
;
2568 port
= used_usb_ports
;
2569 while (port
&& port
->dev
->addr
!= addr
) {
2570 lastp
= &port
->next
;
2578 *lastp
= port
->next
;
2579 usb_attach(port
, NULL
);
2580 dev
->handle_destroy(dev
);
2581 port
->next
= free_usb_ports
;
2582 free_usb_ports
= port
;
2586 static int usb_device_del(const char *devname
)
2591 if (strstart(devname
, "host:", &p
))
2592 return usb_host_device_close(p
);
2594 if (!used_usb_ports
)
2597 p
= strchr(devname
, '.');
2600 bus_num
= strtoul(devname
, NULL
, 0);
2601 addr
= strtoul(p
+ 1, NULL
, 0);
2603 return usb_device_del_addr(bus_num
, addr
);
2606 void do_usb_add(Monitor
*mon
, const char *devname
)
2608 usb_device_add(devname
, 1);
2611 void do_usb_del(Monitor
*mon
, const char *devname
)
2613 usb_device_del(devname
);
2616 void usb_info(Monitor
*mon
)
2620 const char *speed_str
;
2623 monitor_printf(mon
, "USB support not enabled\n");
2627 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2631 switch(dev
->speed
) {
2635 case USB_SPEED_FULL
:
2638 case USB_SPEED_HIGH
:
2645 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2646 0, dev
->addr
, speed_str
, dev
->devname
);
2650 /***********************************************************/
2651 /* PCMCIA/Cardbus */
2653 static struct pcmcia_socket_entry_s
{
2654 PCMCIASocket
*socket
;
2655 struct pcmcia_socket_entry_s
*next
;
2656 } *pcmcia_sockets
= 0;
2658 void pcmcia_socket_register(PCMCIASocket
*socket
)
2660 struct pcmcia_socket_entry_s
*entry
;
2662 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2663 entry
->socket
= socket
;
2664 entry
->next
= pcmcia_sockets
;
2665 pcmcia_sockets
= entry
;
2668 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2670 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2672 ptr
= &pcmcia_sockets
;
2673 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2674 if (entry
->socket
== socket
) {
2680 void pcmcia_info(Monitor
*mon
)
2682 struct pcmcia_socket_entry_s
*iter
;
2684 if (!pcmcia_sockets
)
2685 monitor_printf(mon
, "No PCMCIA sockets\n");
2687 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2688 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2689 iter
->socket
->attached
? iter
->socket
->card_string
:
2693 /***********************************************************/
2694 /* register display */
2696 struct DisplayAllocator default_allocator
= {
2697 defaultallocator_create_displaysurface
,
2698 defaultallocator_resize_displaysurface
,
2699 defaultallocator_free_displaysurface
2702 void register_displaystate(DisplayState
*ds
)
2712 DisplayState
*get_displaystate(void)
2714 return display_state
;
2717 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2719 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2720 return ds
->allocator
;
2725 static void dumb_display_init(void)
2727 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2728 ds
->allocator
= &default_allocator
;
2729 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2730 register_displaystate(ds
);
2733 /***********************************************************/
2736 typedef struct IOHandlerRecord
{
2738 IOCanRWHandler
*fd_read_poll
;
2740 IOHandler
*fd_write
;
2743 /* temporary data */
2745 struct IOHandlerRecord
*next
;
2748 static IOHandlerRecord
*first_io_handler
;
2750 /* XXX: fd_read_poll should be suppressed, but an API change is
2751 necessary in the character devices to suppress fd_can_read(). */
2752 int qemu_set_fd_handler2(int fd
,
2753 IOCanRWHandler
*fd_read_poll
,
2755 IOHandler
*fd_write
,
2758 IOHandlerRecord
**pioh
, *ioh
;
2760 if (!fd_read
&& !fd_write
) {
2761 pioh
= &first_io_handler
;
2766 if (ioh
->fd
== fd
) {
2773 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2777 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2778 ioh
->next
= first_io_handler
;
2779 first_io_handler
= ioh
;
2782 ioh
->fd_read_poll
= fd_read_poll
;
2783 ioh
->fd_read
= fd_read
;
2784 ioh
->fd_write
= fd_write
;
2785 ioh
->opaque
= opaque
;
2788 qemu_notify_event();
2792 int qemu_set_fd_handler(int fd
,
2794 IOHandler
*fd_write
,
2797 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2801 /***********************************************************/
2802 /* Polling handling */
2804 typedef struct PollingEntry
{
2807 struct PollingEntry
*next
;
2810 static PollingEntry
*first_polling_entry
;
2812 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2814 PollingEntry
**ppe
, *pe
;
2815 pe
= qemu_mallocz(sizeof(PollingEntry
));
2817 pe
->opaque
= opaque
;
2818 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2823 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2825 PollingEntry
**ppe
, *pe
;
2826 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2828 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2836 /***********************************************************/
2837 /* Wait objects support */
2838 typedef struct WaitObjects
{
2840 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2841 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2842 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2845 static WaitObjects wait_objects
= {0};
2847 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2849 WaitObjects
*w
= &wait_objects
;
2851 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2853 w
->events
[w
->num
] = handle
;
2854 w
->func
[w
->num
] = func
;
2855 w
->opaque
[w
->num
] = opaque
;
2860 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2863 WaitObjects
*w
= &wait_objects
;
2866 for (i
= 0; i
< w
->num
; i
++) {
2867 if (w
->events
[i
] == handle
)
2870 w
->events
[i
] = w
->events
[i
+ 1];
2871 w
->func
[i
] = w
->func
[i
+ 1];
2872 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2880 /***********************************************************/
2881 /* ram save/restore */
2883 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2887 v
= qemu_get_byte(f
);
2890 if (qemu_get_buffer(f
, buf
, len
) != len
)
2894 v
= qemu_get_byte(f
);
2895 memset(buf
, v
, len
);
2901 if (qemu_file_has_error(f
))
2907 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2912 if (qemu_get_be32(f
) != last_ram_offset
)
2914 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2915 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
2917 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2924 #define BDRV_HASH_BLOCK_SIZE 1024
2925 #define IOBUF_SIZE 4096
2926 #define RAM_CBLOCK_MAGIC 0xfabe
2928 typedef struct RamDecompressState
{
2931 uint8_t buf
[IOBUF_SIZE
];
2932 } RamDecompressState
;
2934 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2937 memset(s
, 0, sizeof(*s
));
2939 ret
= inflateInit(&s
->zstream
);
2945 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2949 s
->zstream
.avail_out
= len
;
2950 s
->zstream
.next_out
= buf
;
2951 while (s
->zstream
.avail_out
> 0) {
2952 if (s
->zstream
.avail_in
== 0) {
2953 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2955 clen
= qemu_get_be16(s
->f
);
2956 if (clen
> IOBUF_SIZE
)
2958 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2959 s
->zstream
.avail_in
= clen
;
2960 s
->zstream
.next_in
= s
->buf
;
2962 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2963 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2970 static void ram_decompress_close(RamDecompressState
*s
)
2972 inflateEnd(&s
->zstream
);
2975 #define RAM_SAVE_FLAG_FULL 0x01
2976 #define RAM_SAVE_FLAG_COMPRESS 0x02
2977 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2978 #define RAM_SAVE_FLAG_PAGE 0x08
2979 #define RAM_SAVE_FLAG_EOS 0x10
2981 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2983 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2984 uint32_t *array
= (uint32_t *)page
;
2987 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2988 if (array
[i
] != val
)
2995 static int ram_save_block(QEMUFile
*f
)
2997 static ram_addr_t current_addr
= 0;
2998 ram_addr_t saved_addr
= current_addr
;
2999 ram_addr_t addr
= 0;
3002 while (addr
< last_ram_offset
) {
3003 if (kvm_enabled() && current_addr
== 0) {
3005 r
= kvm_update_dirty_pages_log();
3007 fprintf(stderr
, "%s: update dirty pages log failed %d\n", __FUNCTION__
, r
);
3008 qemu_file_set_error(f
);
3012 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3015 cpu_physical_memory_reset_dirty(current_addr
,
3016 current_addr
+ TARGET_PAGE_SIZE
,
3017 MIGRATION_DIRTY_FLAG
);
3019 p
= qemu_get_ram_ptr(current_addr
);
3021 if (is_dup_page(p
, *p
)) {
3022 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3023 qemu_put_byte(f
, *p
);
3025 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3026 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3032 addr
+= TARGET_PAGE_SIZE
;
3033 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3039 static uint64_t bytes_transferred
= 0;
3041 static ram_addr_t
ram_save_remaining(void)
3044 ram_addr_t count
= 0;
3046 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3047 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3054 uint64_t ram_bytes_remaining(void)
3056 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3059 uint64_t ram_bytes_transferred(void)
3061 return bytes_transferred
;
3064 uint64_t ram_bytes_total(void)
3066 return last_ram_offset
;
3069 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3072 uint64_t bytes_transferred_last
;
3074 uint64_t expected_time
= 0;
3076 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3077 qemu_file_set_error(f
);
3082 /* Make sure all dirty bits are set */
3083 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3084 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3085 cpu_physical_memory_set_dirty(addr
);
3088 /* Enable dirty memory tracking */
3089 cpu_physical_memory_set_dirty_tracking(1);
3091 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3094 bytes_transferred_last
= bytes_transferred
;
3095 bwidth
= get_clock();
3097 while (!qemu_file_rate_limit(f
)) {
3100 ret
= ram_save_block(f
);
3101 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3102 if (ret
== 0) /* no more blocks */
3106 bwidth
= get_clock() - bwidth
;
3107 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3109 /* if we haven't transferred anything this round, force expected_time to a
3110 * a very high value, but without crashing */
3114 /* try transferring iterative blocks of memory */
3118 /* flush all remaining blocks regardless of rate limiting */
3119 while (ram_save_block(f
) != 0) {
3120 bytes_transferred
+= TARGET_PAGE_SIZE
;
3122 cpu_physical_memory_set_dirty_tracking(0);
3125 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3127 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3129 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3132 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3134 RamDecompressState s1
, *s
= &s1
;
3138 if (ram_decompress_open(s
, f
) < 0)
3140 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3141 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3143 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3144 fprintf(stderr
, "Error while reading ram block header\n");
3148 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3149 BDRV_HASH_BLOCK_SIZE
) < 0) {
3150 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3155 printf("Error block header\n");
3159 ram_decompress_close(s
);
3164 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3169 if (version_id
== 1)
3170 return ram_load_v1(f
, opaque
);
3172 if (version_id
== 2) {
3173 if (qemu_get_be32(f
) != last_ram_offset
)
3175 return ram_load_dead(f
, opaque
);
3178 if (version_id
!= 3)
3182 addr
= qemu_get_be64(f
);
3184 flags
= addr
& ~TARGET_PAGE_MASK
;
3185 addr
&= TARGET_PAGE_MASK
;
3187 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3188 if (addr
!= last_ram_offset
)
3192 if (flags
& RAM_SAVE_FLAG_FULL
) {
3193 if (ram_load_dead(f
, opaque
) < 0)
3197 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3198 uint8_t ch
= qemu_get_byte(f
);
3199 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3202 (!kvm_enabled() || kvm_has_sync_mmu())) {
3203 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3206 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3207 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3208 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3213 void qemu_service_io(void)
3215 qemu_notify_event();
3218 /***********************************************************/
3219 /* bottom halves (can be seen as timers which expire ASAP) */
3230 static QEMUBH
*first_bh
= NULL
;
3232 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3235 bh
= qemu_mallocz(sizeof(QEMUBH
));
3237 bh
->opaque
= opaque
;
3238 bh
->next
= first_bh
;
3243 int qemu_bh_poll(void)
3249 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3250 if (!bh
->deleted
&& bh
->scheduled
) {
3259 /* remove deleted bhs */
3273 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3281 void qemu_bh_schedule(QEMUBH
*bh
)
3287 /* stop the currently executing CPU to execute the BH ASAP */
3288 qemu_notify_event();
3291 void qemu_bh_cancel(QEMUBH
*bh
)
3296 void qemu_bh_delete(QEMUBH
*bh
)
3302 static void qemu_bh_update_timeout(int *timeout
)
3306 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3307 if (!bh
->deleted
&& bh
->scheduled
) {
3309 /* idle bottom halves will be polled at least
3311 *timeout
= MIN(10, *timeout
);
3313 /* non-idle bottom halves will be executed
3322 /***********************************************************/
3323 /* machine registration */
3325 static QEMUMachine
*first_machine
= NULL
;
3326 QEMUMachine
*current_machine
= NULL
;
3328 int qemu_register_machine(QEMUMachine
*m
)
3331 pm
= &first_machine
;
3339 static QEMUMachine
*find_machine(const char *name
)
3343 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3344 if (!strcmp(m
->name
, name
))
3350 static QEMUMachine
*find_default_machine(void)
3354 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3355 if (m
->is_default
) {
3362 /***********************************************************/
3363 /* main execution loop */
3365 static void gui_update(void *opaque
)
3367 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3368 DisplayState
*ds
= opaque
;
3369 DisplayChangeListener
*dcl
= ds
->listeners
;
3373 while (dcl
!= NULL
) {
3374 if (dcl
->gui_timer_interval
&&
3375 dcl
->gui_timer_interval
< interval
)
3376 interval
= dcl
->gui_timer_interval
;
3379 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3382 static void nographic_update(void *opaque
)
3384 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3386 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3389 struct vm_change_state_entry
{
3390 VMChangeStateHandler
*cb
;
3392 LIST_ENTRY (vm_change_state_entry
) entries
;
3395 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3397 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3400 VMChangeStateEntry
*e
;
3402 e
= qemu_mallocz(sizeof (*e
));
3406 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3410 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3412 LIST_REMOVE (e
, entries
);
3416 static void vm_state_notify(int running
, int reason
)
3418 VMChangeStateEntry
*e
;
3420 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3421 e
->cb(e
->opaque
, running
, reason
);
3425 static void resume_all_vcpus(void);
3426 static void pause_all_vcpus(void);
3433 vm_state_notify(1, 0);
3434 qemu_rearm_alarm_timer(alarm_timer
);
3439 /* reset/shutdown handler */
3441 typedef struct QEMUResetEntry
{
3442 QEMUResetHandler
*func
;
3444 struct QEMUResetEntry
*next
;
3447 static QEMUResetEntry
*first_reset_entry
;
3448 static int reset_requested
;
3449 static int shutdown_requested
;
3450 static int powerdown_requested
;
3451 static int debug_requested
;
3452 static int vmstop_requested
;
3454 int qemu_no_shutdown(void)
3456 int r
= no_shutdown
;
3461 int qemu_shutdown_requested(void)
3463 int r
= shutdown_requested
;
3464 shutdown_requested
= 0;
3468 int qemu_reset_requested(void)
3470 int r
= reset_requested
;
3471 reset_requested
= 0;
3475 int qemu_powerdown_requested(void)
3477 int r
= powerdown_requested
;
3478 powerdown_requested
= 0;
3482 static int qemu_debug_requested(void)
3484 int r
= debug_requested
;
3485 debug_requested
= 0;
3489 static int qemu_vmstop_requested(void)
3491 int r
= vmstop_requested
;
3492 vmstop_requested
= 0;
3496 static void do_vm_stop(int reason
)
3499 cpu_disable_ticks();
3502 vm_state_notify(0, reason
);
3506 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3508 QEMUResetEntry
**pre
, *re
;
3510 pre
= &first_reset_entry
;
3511 while (*pre
!= NULL
)
3512 pre
= &(*pre
)->next
;
3513 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3515 re
->opaque
= opaque
;
3520 void qemu_system_reset(void)
3524 /* reset all devices */
3525 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3526 re
->func(re
->opaque
);
3530 void qemu_system_reset_request(void)
3533 shutdown_requested
= 1;
3535 reset_requested
= 1;
3537 if (cpu_single_env
) {
3538 cpu_single_env
->stopped
= 1;
3540 qemu_notify_event();
3543 void qemu_system_shutdown_request(void)
3545 shutdown_requested
= 1;
3546 qemu_notify_event();
3549 void qemu_system_powerdown_request(void)
3551 powerdown_requested
= 1;
3552 qemu_notify_event();
3555 #ifdef CONFIG_IOTHREAD
3556 static void qemu_system_vmstop_request(int reason
)
3558 vmstop_requested
= reason
;
3559 qemu_notify_event();
3564 static int io_thread_fd
= -1;
3566 static void qemu_event_increment(void)
3568 static const char byte
= 0;
3570 if (io_thread_fd
== -1)
3573 write(io_thread_fd
, &byte
, sizeof(byte
));
3576 static void qemu_event_read(void *opaque
)
3578 int fd
= (unsigned long)opaque
;
3581 /* Drain the notify pipe */
3584 len
= read(fd
, buffer
, sizeof(buffer
));
3585 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3588 static int qemu_event_init(void)
3597 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3601 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3605 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3606 (void *)(unsigned long)fds
[0]);
3608 io_thread_fd
= fds
[1];
3617 HANDLE qemu_event_handle
;
3619 static void dummy_event_handler(void *opaque
)
3623 static int qemu_event_init(void)
3625 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3626 if (!qemu_event_handle
) {
3627 perror("Failed CreateEvent");
3630 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3634 static void qemu_event_increment(void)
3636 SetEvent(qemu_event_handle
);
3640 static int cpu_can_run(CPUState
*env
)
3649 #ifndef CONFIG_IOTHREAD
3650 static int qemu_init_main_loop(void)
3652 return qemu_event_init();
3655 void qemu_init_vcpu(void *_env
)
3657 CPUState
*env
= _env
;
3664 int qemu_cpu_self(void *env
)
3669 static void resume_all_vcpus(void)
3673 static void pause_all_vcpus(void)
3677 void qemu_cpu_kick(void *env
)
3682 void qemu_notify_event(void)
3684 CPUState
*env
= cpu_single_env
;
3686 if (kvm_enabled()) {
3687 qemu_kvm_notify_work();
3693 if (env
->kqemu_enabled
)
3694 kqemu_cpu_interrupt(env
);
3700 #define qemu_mutex_lock_iothread() do { } while (0)
3701 #define qemu_mutex_unlock_iothread() do { } while (0)
3704 void vm_stop(int reason
)
3709 #else /* CONFIG_IOTHREAD */
3711 #include "qemu-thread.h"
3713 QemuMutex qemu_global_mutex
;
3714 static QemuMutex qemu_fair_mutex
;
3716 static QemuThread io_thread
;
3718 static QemuThread
*tcg_cpu_thread
;
3719 static QemuCond
*tcg_halt_cond
;
3721 static int qemu_system_ready
;
3723 static QemuCond qemu_cpu_cond
;
3725 static QemuCond qemu_system_cond
;
3726 static QemuCond qemu_pause_cond
;
3728 static void block_io_signals(void);
3729 static void unblock_io_signals(void);
3730 static int tcg_has_work(void);
3732 static int qemu_init_main_loop(void)
3736 ret
= qemu_event_init();
3740 qemu_cond_init(&qemu_pause_cond
);
3741 qemu_mutex_init(&qemu_fair_mutex
);
3742 qemu_mutex_init(&qemu_global_mutex
);
3743 qemu_mutex_lock(&qemu_global_mutex
);
3745 unblock_io_signals();
3746 qemu_thread_self(&io_thread
);
3751 static void qemu_wait_io_event(CPUState
*env
)
3753 while (!tcg_has_work())
3754 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3756 qemu_mutex_unlock(&qemu_global_mutex
);
3759 * Users of qemu_global_mutex can be starved, having no chance
3760 * to acquire it since this path will get to it first.
3761 * So use another lock to provide fairness.
3763 qemu_mutex_lock(&qemu_fair_mutex
);
3764 qemu_mutex_unlock(&qemu_fair_mutex
);
3766 qemu_mutex_lock(&qemu_global_mutex
);
3770 qemu_cond_signal(&qemu_pause_cond
);
3774 static int qemu_cpu_exec(CPUState
*env
);
3776 static void *kvm_cpu_thread_fn(void *arg
)
3778 CPUState
*env
= arg
;
3781 qemu_thread_self(env
->thread
);
3783 /* signal CPU creation */
3784 qemu_mutex_lock(&qemu_global_mutex
);
3786 qemu_cond_signal(&qemu_cpu_cond
);
3788 /* and wait for machine initialization */
3789 while (!qemu_system_ready
)
3790 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3793 if (cpu_can_run(env
))
3795 qemu_wait_io_event(env
);
3801 static void tcg_cpu_exec(void);
3803 static void *tcg_cpu_thread_fn(void *arg
)
3805 CPUState
*env
= arg
;
3808 qemu_thread_self(env
->thread
);
3810 /* signal CPU creation */
3811 qemu_mutex_lock(&qemu_global_mutex
);
3812 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3814 qemu_cond_signal(&qemu_cpu_cond
);
3816 /* and wait for machine initialization */
3817 while (!qemu_system_ready
)
3818 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3822 qemu_wait_io_event(cur_cpu
);
3828 void qemu_cpu_kick(void *_env
)
3830 CPUState
*env
= _env
;
3831 qemu_cond_broadcast(env
->halt_cond
);
3833 qemu_thread_signal(env
->thread
, SIGUSR1
);
3836 int qemu_cpu_self(void *env
)
3838 return (cpu_single_env
!= NULL
);
3841 static void cpu_signal(int sig
)
3844 cpu_exit(cpu_single_env
);
3847 static void block_io_signals(void)
3850 struct sigaction sigact
;
3853 sigaddset(&set
, SIGUSR2
);
3854 sigaddset(&set
, SIGIO
);
3855 sigaddset(&set
, SIGALRM
);
3856 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3859 sigaddset(&set
, SIGUSR1
);
3860 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3862 memset(&sigact
, 0, sizeof(sigact
));
3863 sigact
.sa_handler
= cpu_signal
;
3864 sigaction(SIGUSR1
, &sigact
, NULL
);
3867 static void unblock_io_signals(void)
3872 sigaddset(&set
, SIGUSR2
);
3873 sigaddset(&set
, SIGIO
);
3874 sigaddset(&set
, SIGALRM
);
3875 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3878 sigaddset(&set
, SIGUSR1
);
3879 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3882 static void qemu_signal_lock(unsigned int msecs
)
3884 qemu_mutex_lock(&qemu_fair_mutex
);
3886 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3887 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3888 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3891 qemu_mutex_unlock(&qemu_fair_mutex
);
3894 static void qemu_mutex_lock_iothread(void)
3896 if (kvm_enabled()) {
3897 qemu_mutex_lock(&qemu_fair_mutex
);
3898 qemu_mutex_lock(&qemu_global_mutex
);
3899 qemu_mutex_unlock(&qemu_fair_mutex
);
3901 qemu_signal_lock(100);
3904 static void qemu_mutex_unlock_iothread(void)
3906 qemu_mutex_unlock(&qemu_global_mutex
);
3909 static int all_vcpus_paused(void)
3911 CPUState
*penv
= first_cpu
;
3916 penv
= (CPUState
*)penv
->next_cpu
;
3922 static void pause_all_vcpus(void)
3924 CPUState
*penv
= first_cpu
;
3928 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3929 qemu_cpu_kick(penv
);
3930 penv
= (CPUState
*)penv
->next_cpu
;
3933 while (!all_vcpus_paused()) {
3934 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3937 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3938 penv
= (CPUState
*)penv
->next_cpu
;
3943 static void resume_all_vcpus(void)
3945 CPUState
*penv
= first_cpu
;
3950 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3951 qemu_cpu_kick(penv
);
3952 penv
= (CPUState
*)penv
->next_cpu
;
3956 static void tcg_init_vcpu(void *_env
)
3958 CPUState
*env
= _env
;
3959 /* share a single thread for all cpus with TCG */
3960 if (!tcg_cpu_thread
) {
3961 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3962 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3963 qemu_cond_init(env
->halt_cond
);
3964 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3965 while (env
->created
== 0)
3966 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3967 tcg_cpu_thread
= env
->thread
;
3968 tcg_halt_cond
= env
->halt_cond
;
3970 env
->thread
= tcg_cpu_thread
;
3971 env
->halt_cond
= tcg_halt_cond
;
3975 static void kvm_start_vcpu(CPUState
*env
)
3978 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3979 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3980 qemu_cond_init(env
->halt_cond
);
3981 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3982 while (env
->created
== 0)
3983 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3986 void qemu_init_vcpu(void *_env
)
3988 CPUState
*env
= _env
;
3991 kvm_start_vcpu(env
);
3996 void qemu_notify_event(void)
3998 qemu_event_increment();
4001 void vm_stop(int reason
)
4004 qemu_thread_self(&me
);
4006 if (!qemu_thread_equal(&me
, &io_thread
)) {
4007 qemu_system_vmstop_request(reason
);
4009 * FIXME: should not return to device code in case
4010 * vm_stop() has been requested.
4012 if (cpu_single_env
) {
4013 cpu_exit(cpu_single_env
);
4014 cpu_single_env
->stop
= 1;
4025 static void host_main_loop_wait(int *timeout
)
4031 /* XXX: need to suppress polling by better using win32 events */
4033 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4034 ret
|= pe
->func(pe
->opaque
);
4038 WaitObjects
*w
= &wait_objects
;
4040 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4041 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4042 if (w
->func
[ret
- WAIT_OBJECT_0
])
4043 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4045 /* Check for additional signaled events */
4046 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4048 /* Check if event is signaled */
4049 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4050 if(ret2
== WAIT_OBJECT_0
) {
4052 w
->func
[i
](w
->opaque
[i
]);
4053 } else if (ret2
== WAIT_TIMEOUT
) {
4055 err
= GetLastError();
4056 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4059 } else if (ret
== WAIT_TIMEOUT
) {
4061 err
= GetLastError();
4062 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4069 static void host_main_loop_wait(int *timeout
)
4074 void main_loop_wait(int timeout
)
4076 IOHandlerRecord
*ioh
;
4077 fd_set rfds
, wfds
, xfds
;
4081 qemu_bh_update_timeout(&timeout
);
4083 host_main_loop_wait(&timeout
);
4085 /* poll any events */
4086 /* XXX: separate device handlers from system ones */
4091 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4095 (!ioh
->fd_read_poll
||
4096 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4097 FD_SET(ioh
->fd
, &rfds
);
4101 if (ioh
->fd_write
) {
4102 FD_SET(ioh
->fd
, &wfds
);
4108 tv
.tv_sec
= timeout
/ 1000;
4109 tv
.tv_usec
= (timeout
% 1000) * 1000;
4111 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4113 qemu_mutex_unlock_iothread();
4114 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4115 qemu_mutex_lock_iothread();
4117 IOHandlerRecord
**pioh
;
4119 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4120 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4121 ioh
->fd_read(ioh
->opaque
);
4122 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
4123 FD_CLR(ioh
->fd
, &rfds
);
4125 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4126 ioh
->fd_write(ioh
->opaque
);
4130 /* remove deleted IO handlers */
4131 pioh
= &first_io_handler
;
4142 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4144 /* rearm timer, if not periodic */
4145 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4146 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4147 qemu_rearm_alarm_timer(alarm_timer
);
4150 /* vm time timers */
4152 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4153 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4154 qemu_get_clock(vm_clock
));
4157 /* real time timers */
4158 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4159 qemu_get_clock(rt_clock
));
4161 /* Check bottom-halves last in case any of the earlier events triggered
4167 static int qemu_cpu_exec(CPUState
*env
)
4170 #ifdef CONFIG_PROFILER
4174 #ifdef CONFIG_PROFILER
4175 ti
= profile_getclock();
4180 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4181 env
->icount_decr
.u16
.low
= 0;
4182 env
->icount_extra
= 0;
4183 count
= qemu_next_deadline();
4184 count
= (count
+ (1 << icount_time_shift
) - 1)
4185 >> icount_time_shift
;
4186 qemu_icount
+= count
;
4187 decr
= (count
> 0xffff) ? 0xffff : count
;
4189 env
->icount_decr
.u16
.low
= decr
;
4190 env
->icount_extra
= count
;
4192 ret
= cpu_exec(env
);
4193 #ifdef CONFIG_PROFILER
4194 qemu_time
+= profile_getclock() - ti
;
4197 /* Fold pending instructions back into the
4198 instruction counter, and clear the interrupt flag. */
4199 qemu_icount
-= (env
->icount_decr
.u16
.low
4200 + env
->icount_extra
);
4201 env
->icount_decr
.u32
= 0;
4202 env
->icount_extra
= 0;
4207 static void tcg_cpu_exec(void)
4211 if (next_cpu
== NULL
)
4212 next_cpu
= first_cpu
;
4213 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4214 CPUState
*env
= cur_cpu
= next_cpu
;
4218 if (timer_alarm_pending
) {
4219 timer_alarm_pending
= 0;
4222 if (cpu_can_run(env
))
4223 ret
= qemu_cpu_exec(env
);
4224 if (ret
== EXCP_DEBUG
) {
4225 gdb_set_stop_cpu(env
);
4226 debug_requested
= 1;
4232 static int cpu_has_work(CPUState
*env
)
4240 if (qemu_cpu_has_work(env
))
4245 static int tcg_has_work(void)
4249 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4250 if (cpu_has_work(env
))
4255 static int qemu_calculate_timeout(void)
4257 #ifndef CONFIG_IOTHREAD
4262 else if (tcg_has_work())
4264 else if (!use_icount
)
4267 /* XXX: use timeout computed from timers */
4270 /* Advance virtual time to the next event. */
4271 if (use_icount
== 1) {
4272 /* When not using an adaptive execution frequency
4273 we tend to get badly out of sync with real time,
4274 so just delay for a reasonable amount of time. */
4277 delta
= cpu_get_icount() - cpu_get_clock();
4280 /* If virtual time is ahead of real time then just
4282 timeout
= (delta
/ 1000000) + 1;
4284 /* Wait for either IO to occur or the next
4286 add
= qemu_next_deadline();
4287 /* We advance the timer before checking for IO.
4288 Limit the amount we advance so that early IO
4289 activity won't get the guest too far ahead. */
4293 add
= (add
+ (1 << icount_time_shift
) - 1)
4294 >> icount_time_shift
;
4296 timeout
= delta
/ 1000000;
4303 #else /* CONFIG_IOTHREAD */
4308 static int vm_can_run(void)
4310 if (powerdown_requested
)
4312 if (reset_requested
)
4314 if (shutdown_requested
)
4316 if (debug_requested
)
4321 static void main_loop(void)
4325 if (kvm_enabled()) {
4327 cpu_disable_ticks();
4331 #ifdef CONFIG_IOTHREAD
4332 qemu_system_ready
= 1;
4333 qemu_cond_broadcast(&qemu_system_cond
);
4338 #ifdef CONFIG_PROFILER
4341 #ifndef CONFIG_IOTHREAD
4344 #ifdef CONFIG_PROFILER
4345 ti
= profile_getclock();
4347 main_loop_wait(qemu_calculate_timeout());
4348 #ifdef CONFIG_PROFILER
4349 dev_time
+= profile_getclock() - ti
;
4351 } while (vm_can_run());
4353 if (qemu_debug_requested())
4354 vm_stop(EXCP_DEBUG
);
4355 if (qemu_shutdown_requested()) {
4362 if (qemu_reset_requested()) {
4364 qemu_system_reset();
4367 if (qemu_powerdown_requested())
4368 qemu_system_powerdown();
4369 if ((r
= qemu_vmstop_requested()))
4375 static void version(void)
4377 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4380 static void help(int exitcode
)
4383 printf("usage: %s [options] [disk_image]\n"
4385 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4387 #define DEF(option, opt_arg, opt_enum, opt_help) \
4389 #define DEFHEADING(text) stringify(text) "\n"
4390 #include "qemu-options.h"
4395 "During emulation, the following keys are useful:\n"
4396 "ctrl-alt-f toggle full screen\n"
4397 "ctrl-alt-n switch to virtual console 'n'\n"
4398 "ctrl-alt toggle mouse and keyboard grab\n"
4400 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4405 DEFAULT_NETWORK_SCRIPT
,
4406 DEFAULT_NETWORK_DOWN_SCRIPT
,
4408 DEFAULT_GDBSTUB_PORT
,
4413 #define HAS_ARG 0x0001
4416 #define DEF(option, opt_arg, opt_enum, opt_help) \
4418 #define DEFHEADING(text)
4419 #include "qemu-options.h"
4425 typedef struct QEMUOption
{
4431 static const QEMUOption qemu_options
[] = {
4432 { "h", 0, QEMU_OPTION_h
},
4433 #define DEF(option, opt_arg, opt_enum, opt_help) \
4434 { option, opt_arg, opt_enum },
4435 #define DEFHEADING(text)
4436 #include "qemu-options.h"
4444 struct soundhw soundhw
[] = {
4445 #ifdef HAS_AUDIO_CHOICE
4446 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4452 { .init_isa
= pcspk_audio_init
}
4459 "Creative Sound Blaster 16",
4462 { .init_isa
= SB16_init
}
4466 #ifdef CONFIG_CS4231A
4472 { .init_isa
= cs4231a_init
}
4480 "Yamaha YMF262 (OPL3)",
4482 "Yamaha YM3812 (OPL2)",
4486 { .init_isa
= Adlib_init
}
4493 "Gravis Ultrasound GF1",
4496 { .init_isa
= GUS_init
}
4503 "Intel 82801AA AC97 Audio",
4506 { .init_pci
= ac97_init
}
4510 #ifdef CONFIG_ES1370
4513 "ENSONIQ AudioPCI ES1370",
4516 { .init_pci
= es1370_init
}
4520 #endif /* HAS_AUDIO_CHOICE */
4522 { NULL
, NULL
, 0, 0, { NULL
} }
4525 static void select_soundhw (const char *optarg
)
4529 if (*optarg
== '?') {
4532 printf ("Valid sound card names (comma separated):\n");
4533 for (c
= soundhw
; c
->name
; ++c
) {
4534 printf ("%-11s %s\n", c
->name
, c
->descr
);
4536 printf ("\n-soundhw all will enable all of the above\n");
4537 exit (*optarg
!= '?');
4545 if (!strcmp (optarg
, "all")) {
4546 for (c
= soundhw
; c
->name
; ++c
) {
4554 e
= strchr (p
, ',');
4555 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4557 for (c
= soundhw
; c
->name
; ++c
) {
4558 if (!strncmp (c
->name
, p
, l
)) {
4567 "Unknown sound card name (too big to show)\n");
4570 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4575 p
+= l
+ (e
!= NULL
);
4579 goto show_valid_cards
;
4584 static void select_vgahw (const char *p
)
4588 cirrus_vga_enabled
= 0;
4589 std_vga_enabled
= 0;
4592 if (strstart(p
, "std", &opts
)) {
4593 std_vga_enabled
= 1;
4594 } else if (strstart(p
, "cirrus", &opts
)) {
4595 cirrus_vga_enabled
= 1;
4596 } else if (strstart(p
, "vmware", &opts
)) {
4598 } else if (strstart(p
, "xenfb", &opts
)) {
4600 } else if (!strstart(p
, "none", &opts
)) {
4602 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4606 const char *nextopt
;
4608 if (strstart(opts
, ",retrace=", &nextopt
)) {
4610 if (strstart(opts
, "dumb", &nextopt
))
4611 vga_retrace_method
= VGA_RETRACE_DUMB
;
4612 else if (strstart(opts
, "precise", &nextopt
))
4613 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4614 else goto invalid_vga
;
4615 } else goto invalid_vga
;
4621 static int balloon_parse(const char *arg
)
4626 if (!strcmp(arg
, "none")) {
4628 } else if (!strncmp(arg
, "virtio", 6)) {
4630 if (arg
[6] == ',') {
4632 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4633 virtio_balloon_devaddr
= strdup(buf
);
4644 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4646 exit(STATUS_CONTROL_C_EXIT
);
4651 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4655 if(strlen(str
) != 36)
4658 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4659 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4660 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4666 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4672 #define MAX_NET_CLIENTS 32
4676 static void termsig_handler(int signal
)
4678 qemu_system_shutdown_request();
4681 static void sigchld_handler(int signal
)
4683 waitpid(-1, NULL
, WNOHANG
);
4686 static void sighandler_setup(void)
4688 struct sigaction act
;
4690 memset(&act
, 0, sizeof(act
));
4691 act
.sa_handler
= termsig_handler
;
4692 sigaction(SIGINT
, &act
, NULL
);
4693 sigaction(SIGHUP
, &act
, NULL
);
4694 sigaction(SIGTERM
, &act
, NULL
);
4696 act
.sa_handler
= sigchld_handler
;
4697 act
.sa_flags
= SA_NOCLDSTOP
;
4698 sigaction(SIGCHLD
, &act
, NULL
);
4704 /* Look for support files in the same directory as the executable. */
4705 static char *find_datadir(const char *argv0
)
4711 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4718 while (p
!= buf
&& *p
!= '\\')
4721 if (access(buf
, R_OK
) == 0) {
4722 return qemu_strdup(buf
);
4728 /* Find a likely location for support files using the location of the binary.
4729 For installed binaries this will be "$bindir/../share/qemu". When
4730 running from the build tree this will be "$bindir/../pc-bios". */
4731 #define SHARE_SUFFIX "/share/qemu"
4732 #define BUILD_SUFFIX "/pc-bios"
4733 static char *find_datadir(const char *argv0
)
4743 #if defined(__linux__)
4746 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4752 #elif defined(__FreeBSD__)
4755 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4762 /* If we don't have any way of figuring out the actual executable
4763 location then try argv[0]. */
4768 p
= realpath(argv0
, p
);
4776 max_len
= strlen(dir
) +
4777 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4778 res
= qemu_mallocz(max_len
);
4779 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4780 if (access(res
, R_OK
)) {
4781 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4782 if (access(res
, R_OK
)) {
4796 char *qemu_find_file(int type
, const char *name
)
4802 /* If name contains path separators then try it as a straight path. */
4803 if ((strchr(name
, '/') || strchr(name
, '\\'))
4804 && access(name
, R_OK
) == 0) {
4805 return strdup(name
);
4808 case QEMU_FILE_TYPE_BIOS
:
4811 case QEMU_FILE_TYPE_KEYMAP
:
4812 subdir
= "keymaps/";
4817 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4818 buf
= qemu_mallocz(len
);
4819 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4820 if (access(buf
, R_OK
)) {
4827 int main(int argc
, char **argv
, char **envp
)
4829 const char *gdbstub_dev
= NULL
;
4830 uint32_t boot_devices_bitmap
= 0;
4832 int snapshot
, linux_boot
, net_boot
;
4833 const char *initrd_filename
;
4834 const char *kernel_filename
, *kernel_cmdline
;
4835 const char *boot_devices
= "";
4837 DisplayChangeListener
*dcl
;
4838 int cyls
, heads
, secs
, translation
;
4839 const char *net_clients
[MAX_NET_CLIENTS
];
4841 const char *bt_opts
[MAX_BT_CMDLINE
];
4845 const char *r
, *optarg
;
4846 CharDriverState
*monitor_hd
= NULL
;
4847 const char *monitor_device
;
4848 const char *serial_devices
[MAX_SERIAL_PORTS
];
4849 int serial_device_index
;
4850 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4851 int parallel_device_index
;
4852 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4853 int virtio_console_index
;
4854 const char *loadvm
= NULL
;
4855 QEMUMachine
*machine
;
4856 const char *cpu_model
;
4857 const char *usb_devices
[MAX_USB_CMDLINE
];
4858 int usb_devices_index
;
4863 const char *pid_file
= NULL
;
4864 const char *incoming
= NULL
;
4867 struct passwd
*pwd
= NULL
;
4868 const char *chroot_dir
= NULL
;
4869 const char *run_as
= NULL
;
4872 int show_vnc_port
= 0;
4874 qemu_cache_utils_init(envp
);
4876 LIST_INIT (&vm_change_state_head
);
4879 struct sigaction act
;
4880 sigfillset(&act
.sa_mask
);
4882 act
.sa_handler
= SIG_IGN
;
4883 sigaction(SIGPIPE
, &act
, NULL
);
4886 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4887 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4888 QEMU to run on a single CPU */
4893 h
= GetCurrentProcess();
4894 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4895 for(i
= 0; i
< 32; i
++) {
4896 if (mask
& (1 << i
))
4901 SetProcessAffinityMask(h
, mask
);
4907 module_call_init(MODULE_INIT_MACHINE
);
4908 machine
= find_default_machine();
4910 initrd_filename
= NULL
;
4913 kernel_filename
= NULL
;
4914 kernel_cmdline
= "";
4915 cyls
= heads
= secs
= 0;
4916 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4917 monitor_device
= "vc:80Cx24C";
4919 serial_devices
[0] = "vc:80Cx24C";
4920 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4921 serial_devices
[i
] = NULL
;
4922 serial_device_index
= 0;
4924 parallel_devices
[0] = "vc:80Cx24C";
4925 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4926 parallel_devices
[i
] = NULL
;
4927 parallel_device_index
= 0;
4929 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4930 virtio_consoles
[i
] = NULL
;
4931 virtio_console_index
= 0;
4933 for (i
= 0; i
< MAX_NODES
; i
++) {
4935 node_cpumask
[i
] = 0;
4938 usb_devices_index
= 0;
4939 assigned_devices_index
= 0;
4953 register_watchdogs();
4961 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4963 const QEMUOption
*popt
;
4966 /* Treat --foo the same as -foo. */
4969 popt
= qemu_options
;
4972 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4976 if (!strcmp(popt
->name
, r
+ 1))
4980 if (popt
->flags
& HAS_ARG
) {
4981 if (optind
>= argc
) {
4982 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4986 optarg
= argv
[optind
++];
4991 switch(popt
->index
) {
4993 machine
= find_machine(optarg
);
4996 printf("Supported machines are:\n");
4997 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4998 printf("%-10s %s%s\n",
5000 m
->is_default
? " (default)" : "");
5002 exit(*optarg
!= '?');
5005 case QEMU_OPTION_cpu
:
5006 /* hw initialization will check this */
5007 if (*optarg
== '?') {
5008 /* XXX: implement xxx_cpu_list for targets that still miss it */
5009 #if defined(cpu_list)
5010 cpu_list(stdout
, &fprintf
);
5017 case QEMU_OPTION_initrd
:
5018 initrd_filename
= optarg
;
5020 case QEMU_OPTION_hda
:
5022 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5024 hda_index
= drive_add(optarg
, HD_ALIAS
5025 ",cyls=%d,heads=%d,secs=%d%s",
5026 0, cyls
, heads
, secs
,
5027 translation
== BIOS_ATA_TRANSLATION_LBA
?
5029 translation
== BIOS_ATA_TRANSLATION_NONE
?
5030 ",trans=none" : "");
5032 case QEMU_OPTION_hdb
:
5033 case QEMU_OPTION_hdc
:
5034 case QEMU_OPTION_hdd
:
5035 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5037 case QEMU_OPTION_drive
:
5038 drive_add(NULL
, "%s", optarg
);
5040 case QEMU_OPTION_mtdblock
:
5041 drive_add(optarg
, MTD_ALIAS
);
5043 case QEMU_OPTION_sd
:
5044 drive_add(optarg
, SD_ALIAS
);
5046 case QEMU_OPTION_pflash
:
5047 drive_add(optarg
, PFLASH_ALIAS
);
5049 case QEMU_OPTION_snapshot
:
5052 case QEMU_OPTION_hdachs
:
5056 cyls
= strtol(p
, (char **)&p
, 0);
5057 if (cyls
< 1 || cyls
> 16383)
5062 heads
= strtol(p
, (char **)&p
, 0);
5063 if (heads
< 1 || heads
> 16)
5068 secs
= strtol(p
, (char **)&p
, 0);
5069 if (secs
< 1 || secs
> 63)
5073 if (!strcmp(p
, "none"))
5074 translation
= BIOS_ATA_TRANSLATION_NONE
;
5075 else if (!strcmp(p
, "lba"))
5076 translation
= BIOS_ATA_TRANSLATION_LBA
;
5077 else if (!strcmp(p
, "auto"))
5078 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5081 } else if (*p
!= '\0') {
5083 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5086 if (hda_index
!= -1)
5087 snprintf(drives_opt
[hda_index
].opt
,
5088 sizeof(drives_opt
[hda_index
].opt
),
5089 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5090 0, cyls
, heads
, secs
,
5091 translation
== BIOS_ATA_TRANSLATION_LBA
?
5093 translation
== BIOS_ATA_TRANSLATION_NONE
?
5094 ",trans=none" : "");
5097 case QEMU_OPTION_numa
:
5098 if (nb_numa_nodes
>= MAX_NODES
) {
5099 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5104 case QEMU_OPTION_nographic
:
5105 display_type
= DT_NOGRAPHIC
;
5107 #ifdef CONFIG_CURSES
5108 case QEMU_OPTION_curses
:
5109 display_type
= DT_CURSES
;
5112 case QEMU_OPTION_portrait
:
5115 case QEMU_OPTION_kernel
:
5116 kernel_filename
= optarg
;
5118 case QEMU_OPTION_append
:
5119 kernel_cmdline
= optarg
;
5121 case QEMU_OPTION_cdrom
:
5122 drive_add(optarg
, CDROM_ALIAS
);
5124 case QEMU_OPTION_boot
:
5125 boot_devices
= optarg
;
5126 /* We just do some generic consistency checks */
5128 /* Could easily be extended to 64 devices if needed */
5131 boot_devices_bitmap
= 0;
5132 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5133 /* Allowed boot devices are:
5134 * a b : floppy disk drives
5135 * c ... f : IDE disk drives
5136 * g ... m : machine implementation dependant drives
5137 * n ... p : network devices
5138 * It's up to each machine implementation to check
5139 * if the given boot devices match the actual hardware
5140 * implementation and firmware features.
5142 if (*p
< 'a' || *p
> 'q') {
5143 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5146 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5148 "Boot device '%c' was given twice\n",*p
);
5151 boot_devices_bitmap
|= 1 << (*p
- 'a');
5155 case QEMU_OPTION_fda
:
5156 case QEMU_OPTION_fdb
:
5157 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5160 case QEMU_OPTION_no_fd_bootchk
:
5164 case QEMU_OPTION_net
:
5165 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5166 fprintf(stderr
, "qemu: too many network clients\n");
5169 net_clients
[nb_net_clients
] = optarg
;
5173 case QEMU_OPTION_tftp
:
5174 legacy_tftp_prefix
= optarg
;
5176 case QEMU_OPTION_bootp
:
5177 legacy_bootp_filename
= optarg
;
5180 case QEMU_OPTION_smb
:
5181 net_slirp_smb(optarg
);
5184 case QEMU_OPTION_redir
:
5185 net_slirp_redir(optarg
);
5188 case QEMU_OPTION_bt
:
5189 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5190 fprintf(stderr
, "qemu: too many bluetooth options\n");
5193 bt_opts
[nb_bt_opts
++] = optarg
;
5196 case QEMU_OPTION_audio_help
:
5200 case QEMU_OPTION_soundhw
:
5201 select_soundhw (optarg
);
5207 case QEMU_OPTION_version
:
5211 case QEMU_OPTION_m
: {
5215 value
= strtoul(optarg
, &ptr
, 10);
5217 case 0: case 'M': case 'm':
5224 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5228 /* On 32-bit hosts, QEMU is limited by virtual address space */
5229 if (value
> (2047 << 20)
5230 #ifndef CONFIG_KQEMU
5231 && HOST_LONG_BITS
== 32
5234 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5237 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5238 fprintf(stderr
, "qemu: ram size too large\n");
5247 const CPULogItem
*item
;
5249 mask
= cpu_str_to_log_mask(optarg
);
5251 printf("Log items (comma separated):\n");
5252 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5253 printf("%-10s %s\n", item
->name
, item
->help
);
5261 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5263 case QEMU_OPTION_gdb
:
5264 gdbstub_dev
= optarg
;
5269 case QEMU_OPTION_bios
:
5272 case QEMU_OPTION_singlestep
:
5280 keyboard_layout
= optarg
;
5283 case QEMU_OPTION_localtime
:
5286 case QEMU_OPTION_vga
:
5287 select_vgahw (optarg
);
5289 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5295 w
= strtol(p
, (char **)&p
, 10);
5298 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5304 h
= strtol(p
, (char **)&p
, 10);
5309 depth
= strtol(p
, (char **)&p
, 10);
5310 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5311 depth
!= 24 && depth
!= 32)
5313 } else if (*p
== '\0') {
5314 depth
= graphic_depth
;
5321 graphic_depth
= depth
;
5325 case QEMU_OPTION_echr
:
5328 term_escape_char
= strtol(optarg
, &r
, 0);
5330 printf("Bad argument to echr\n");
5333 case QEMU_OPTION_monitor
:
5334 monitor_device
= optarg
;
5336 case QEMU_OPTION_serial
:
5337 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5338 fprintf(stderr
, "qemu: too many serial ports\n");
5341 serial_devices
[serial_device_index
] = optarg
;
5342 serial_device_index
++;
5344 case QEMU_OPTION_watchdog
:
5345 i
= select_watchdog(optarg
);
5347 exit (i
== 1 ? 1 : 0);
5349 case QEMU_OPTION_watchdog_action
:
5350 if (select_watchdog_action(optarg
) == -1) {
5351 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5355 case QEMU_OPTION_virtiocon
:
5356 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5357 fprintf(stderr
, "qemu: too many virtio consoles\n");
5360 virtio_consoles
[virtio_console_index
] = optarg
;
5361 virtio_console_index
++;
5363 case QEMU_OPTION_parallel
:
5364 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5365 fprintf(stderr
, "qemu: too many parallel ports\n");
5368 parallel_devices
[parallel_device_index
] = optarg
;
5369 parallel_device_index
++;
5371 case QEMU_OPTION_loadvm
:
5374 case QEMU_OPTION_full_screen
:
5378 case QEMU_OPTION_no_frame
:
5381 case QEMU_OPTION_alt_grab
:
5384 case QEMU_OPTION_no_quit
:
5387 case QEMU_OPTION_sdl
:
5388 display_type
= DT_SDL
;
5391 case QEMU_OPTION_pidfile
:
5395 case QEMU_OPTION_win2k_hack
:
5396 win2k_install_hack
= 1;
5398 case QEMU_OPTION_rtc_td_hack
:
5401 case QEMU_OPTION_acpitable
:
5402 if(acpi_table_add(optarg
) < 0) {
5403 fprintf(stderr
, "Wrong acpi table provided\n");
5407 case QEMU_OPTION_smbios
:
5408 if(smbios_entry_add(optarg
) < 0) {
5409 fprintf(stderr
, "Wrong smbios provided\n");
5415 case QEMU_OPTION_enable_kqemu
:
5418 case QEMU_OPTION_kernel_kqemu
:
5424 case QEMU_OPTION_enable_kvm
:
5431 case QEMU_OPTION_no_kvm
:
5434 case QEMU_OPTION_no_kvm_irqchip
: {
5439 case QEMU_OPTION_no_kvm_pit
: {
5443 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5444 kvm_pit_reinject
= 0;
5447 case QEMU_OPTION_enable_nesting
: {
5451 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5452 case QEMU_OPTION_pcidevice
:
5453 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5454 fprintf(stderr
, "Too many assigned devices\n");
5457 assigned_devices
[assigned_devices_index
] = optarg
;
5458 assigned_devices_index
++;
5462 case QEMU_OPTION_usb
:
5465 case QEMU_OPTION_usbdevice
:
5467 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5468 fprintf(stderr
, "Too many USB devices\n");
5471 usb_devices
[usb_devices_index
] = optarg
;
5472 usb_devices_index
++;
5474 case QEMU_OPTION_smp
:
5475 smp_cpus
= atoi(optarg
);
5477 fprintf(stderr
, "Invalid number of CPUs\n");
5481 case QEMU_OPTION_vnc
:
5482 display_type
= DT_VNC
;
5483 vnc_display
= optarg
;
5486 case QEMU_OPTION_no_acpi
:
5489 case QEMU_OPTION_no_hpet
:
5492 case QEMU_OPTION_balloon
:
5493 if (balloon_parse(optarg
) < 0) {
5494 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5499 case QEMU_OPTION_no_reboot
:
5502 case QEMU_OPTION_no_shutdown
:
5505 case QEMU_OPTION_show_cursor
:
5508 case QEMU_OPTION_uuid
:
5509 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5510 fprintf(stderr
, "Fail to parse UUID string."
5511 " Wrong format.\n");
5516 case QEMU_OPTION_daemonize
:
5520 case QEMU_OPTION_option_rom
:
5521 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5522 fprintf(stderr
, "Too many option ROMs\n");
5525 option_rom
[nb_option_roms
] = optarg
;
5528 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5529 case QEMU_OPTION_semihosting
:
5530 semihosting_enabled
= 1;
5533 case QEMU_OPTION_tdf
:
5536 case QEMU_OPTION_kvm_shadow_memory
:
5537 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5539 case QEMU_OPTION_mempath
:
5543 case QEMU_OPTION_mem_prealloc
:
5544 mem_prealloc
= !mem_prealloc
;
5547 case QEMU_OPTION_name
:
5548 qemu_name
= qemu_strdup(optarg
);
5550 char *p
= strchr(qemu_name
, ',');
5553 if (strncmp(p
, "process=", 8)) {
5554 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5562 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5563 case QEMU_OPTION_prom_env
:
5564 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5565 fprintf(stderr
, "Too many prom variables\n");
5568 prom_envs
[nb_prom_envs
] = optarg
;
5573 case QEMU_OPTION_old_param
:
5577 case QEMU_OPTION_clock
:
5578 configure_alarms(optarg
);
5580 case QEMU_OPTION_startdate
:
5583 time_t rtc_start_date
;
5584 if (!strcmp(optarg
, "now")) {
5585 rtc_date_offset
= -1;
5587 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5595 } else if (sscanf(optarg
, "%d-%d-%d",
5598 &tm
.tm_mday
) == 3) {
5607 rtc_start_date
= mktimegm(&tm
);
5608 if (rtc_start_date
== -1) {
5610 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5611 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5614 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5618 case QEMU_OPTION_tb_size
:
5619 tb_size
= strtol(optarg
, NULL
, 0);
5623 case QEMU_OPTION_icount
:
5625 if (strcmp(optarg
, "auto") == 0) {
5626 icount_time_shift
= -1;
5628 icount_time_shift
= strtol(optarg
, NULL
, 0);
5631 case QEMU_OPTION_incoming
:
5635 case QEMU_OPTION_chroot
:
5636 chroot_dir
= optarg
;
5638 case QEMU_OPTION_runas
:
5641 case QEMU_OPTION_nvram
:
5646 case QEMU_OPTION_xen_domid
:
5647 xen_domid
= atoi(optarg
);
5649 case QEMU_OPTION_xen_create
:
5650 xen_mode
= XEN_CREATE
;
5652 case QEMU_OPTION_xen_attach
:
5653 xen_mode
= XEN_ATTACH
;
5660 /* If no data_dir is specified then try to find it relative to the
5663 data_dir
= find_datadir(argv
[0]);
5665 /* If all else fails use the install patch specified when building. */
5667 data_dir
= CONFIG_QEMU_SHAREDIR
;
5670 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5671 if (kvm_allowed
&& kqemu_allowed
) {
5673 "You can not enable both KVM and kqemu at the same time\n");
5678 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5679 if (smp_cpus
> machine
->max_cpus
) {
5680 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5681 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5686 if (display_type
== DT_NOGRAPHIC
) {
5687 if (serial_device_index
== 0)
5688 serial_devices
[0] = "stdio";
5689 if (parallel_device_index
== 0)
5690 parallel_devices
[0] = "null";
5691 if (strncmp(monitor_device
, "vc", 2) == 0)
5692 monitor_device
= "stdio";
5699 if (pipe(fds
) == -1)
5710 len
= read(fds
[0], &status
, 1);
5711 if (len
== -1 && (errno
== EINTR
))
5716 else if (status
== 1) {
5717 fprintf(stderr
, "Could not acquire pidfile\n");
5734 signal(SIGTSTP
, SIG_IGN
);
5735 signal(SIGTTOU
, SIG_IGN
);
5736 signal(SIGTTIN
, SIG_IGN
);
5740 if (kvm_enabled()) {
5741 if (kvm_init(smp_cpus
) < 0) {
5742 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5743 #ifdef NO_CPU_EMULATION
5744 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5752 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5755 write(fds
[1], &status
, 1);
5757 fprintf(stderr
, "Could not acquire pid file\n");
5766 if (qemu_init_main_loop()) {
5767 fprintf(stderr
, "qemu_init_main_loop failed\n");
5770 linux_boot
= (kernel_filename
!= NULL
);
5772 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5773 fprintf(stderr
, "-append only allowed with -kernel option\n");
5777 if (!linux_boot
&& initrd_filename
!= NULL
) {
5778 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5782 /* boot to floppy or the default cd if no hard disk defined yet */
5783 if (!boot_devices
[0]) {
5784 boot_devices
= "cad";
5786 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5789 if (init_timer_alarm() < 0) {
5790 fprintf(stderr
, "could not initialize alarm timer\n");
5793 if (use_icount
&& icount_time_shift
< 0) {
5795 /* 125MIPS seems a reasonable initial guess at the guest speed.
5796 It will be corrected fairly quickly anyway. */
5797 icount_time_shift
= 3;
5798 init_icount_adjust();
5805 /* init network clients */
5806 if (nb_net_clients
== 0) {
5807 /* if no clients, we use a default config */
5808 net_clients
[nb_net_clients
++] = "nic";
5810 net_clients
[nb_net_clients
++] = "user";
5814 for(i
= 0;i
< nb_net_clients
; i
++) {
5815 if (net_client_parse(net_clients
[i
]) < 0)
5819 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5820 net_set_boot_mask(net_boot
);
5824 /* init the bluetooth world */
5825 for (i
= 0; i
< nb_bt_opts
; i
++)
5826 if (bt_parse(bt_opts
[i
]))
5829 /* init the memory */
5831 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5834 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5835 guest ram allocation. It needs to go away. */
5836 if (kqemu_allowed
) {
5837 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5838 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5839 if (!kqemu_phys_ram_base
) {
5840 fprintf(stderr
, "Could not allocate physical memory\n");
5846 /* init the dynamic translator */
5847 cpu_exec_init_all(tb_size
* 1024 * 1024);
5851 /* we always create the cdrom drive, even if no disk is there */
5853 if (nb_drives_opt
< MAX_DRIVES
)
5854 drive_add(NULL
, CDROM_ALIAS
);
5856 /* we always create at least one floppy */
5858 if (nb_drives_opt
< MAX_DRIVES
)
5859 drive_add(NULL
, FD_ALIAS
, 0);
5861 /* we always create one sd slot, even if no card is in it */
5863 if (nb_drives_opt
< MAX_DRIVES
)
5864 drive_add(NULL
, SD_ALIAS
);
5866 /* open the virtual block devices */
5868 for(i
= 0; i
< nb_drives_opt
; i
++)
5869 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5872 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5873 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5876 /* must be after terminal init, SDL library changes signal handlers */
5880 /* Maintain compatibility with multiple stdio monitors */
5881 if (!strcmp(monitor_device
,"stdio")) {
5882 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5883 const char *devname
= serial_devices
[i
];
5884 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5885 monitor_device
= NULL
;
5887 } else if (devname
&& !strcmp(devname
,"stdio")) {
5888 monitor_device
= NULL
;
5889 serial_devices
[i
] = "mon:stdio";
5895 if (nb_numa_nodes
> 0) {
5898 if (nb_numa_nodes
> smp_cpus
) {
5899 nb_numa_nodes
= smp_cpus
;
5902 /* If no memory size if given for any node, assume the default case
5903 * and distribute the available memory equally across all nodes
5905 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5906 if (node_mem
[i
] != 0)
5909 if (i
== nb_numa_nodes
) {
5910 uint64_t usedmem
= 0;
5912 /* On Linux, the each node's border has to be 8MB aligned,
5913 * the final node gets the rest.
5915 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5916 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5917 usedmem
+= node_mem
[i
];
5919 node_mem
[i
] = ram_size
- usedmem
;
5922 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5923 if (node_cpumask
[i
] != 0)
5926 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5927 * must cope with this anyway, because there are BIOSes out there in
5928 * real machines which also use this scheme.
5930 if (i
== nb_numa_nodes
) {
5931 for (i
= 0; i
< smp_cpus
; i
++) {
5932 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5938 if (kvm_enabled()) {
5941 ret
= kvm_init(smp_cpus
);
5943 fprintf(stderr
, "failed to initialize KVM\n");
5949 if (monitor_device
) {
5950 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5952 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5957 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5958 const char *devname
= serial_devices
[i
];
5959 if (devname
&& strcmp(devname
, "none")) {
5961 snprintf(label
, sizeof(label
), "serial%d", i
);
5962 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5963 if (!serial_hds
[i
]) {
5964 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5971 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5972 const char *devname
= parallel_devices
[i
];
5973 if (devname
&& strcmp(devname
, "none")) {
5975 snprintf(label
, sizeof(label
), "parallel%d", i
);
5976 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5977 if (!parallel_hds
[i
]) {
5978 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5985 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5986 const char *devname
= virtio_consoles
[i
];
5987 if (devname
&& strcmp(devname
, "none")) {
5989 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5990 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5991 if (!virtcon_hds
[i
]) {
5992 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5999 module_call_init(MODULE_INIT_DEVICE
);
6001 if (kvm_enabled()) {
6005 if (!qemu_kvm_has_gsi_routing()) {
6008 /* if kernel can't do irq routing, interrupt source
6009 * override 0->2 can not be set up as required by hpet,
6013 } else if (!qemu_kvm_has_pit_state2()) {
6023 machine
->init(ram_size
, boot_devices
,
6024 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6027 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
6028 for (i
= 0; i
< nb_numa_nodes
; i
++) {
6029 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
6035 current_machine
= machine
;
6037 /* init USB devices */
6039 for(i
= 0; i
< usb_devices_index
; i
++) {
6040 if (usb_device_add(usb_devices
[i
], 0) < 0) {
6041 fprintf(stderr
, "Warning: could not add USB device %s\n",
6048 dumb_display_init();
6049 /* just use the first displaystate for the moment */
6052 if (display_type
== DT_DEFAULT
) {
6053 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6054 display_type
= DT_SDL
;
6056 display_type
= DT_VNC
;
6057 vnc_display
= "localhost:0,to=99";
6063 switch (display_type
) {
6066 #if defined(CONFIG_CURSES)
6068 curses_display_init(ds
, full_screen
);
6071 #if defined(CONFIG_SDL)
6073 sdl_display_init(ds
, full_screen
, no_frame
);
6075 #elif defined(CONFIG_COCOA)
6077 cocoa_display_init(ds
, full_screen
);
6081 vnc_display_init(ds
);
6082 if (vnc_display_open(ds
, vnc_display
) < 0)
6085 if (show_vnc_port
) {
6086 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6094 dcl
= ds
->listeners
;
6095 while (dcl
!= NULL
) {
6096 if (dcl
->dpy_refresh
!= NULL
) {
6097 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6098 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6103 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6104 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6105 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6108 text_consoles_set_display(display_state
);
6109 qemu_chr_initial_reset();
6111 if (monitor_device
&& monitor_hd
)
6112 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6114 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6115 const char *devname
= serial_devices
[i
];
6116 if (devname
&& strcmp(devname
, "none")) {
6117 if (strstart(devname
, "vc", 0))
6118 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6122 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6123 const char *devname
= parallel_devices
[i
];
6124 if (devname
&& strcmp(devname
, "none")) {
6125 if (strstart(devname
, "vc", 0))
6126 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6130 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6131 const char *devname
= virtio_consoles
[i
];
6132 if (virtcon_hds
[i
] && devname
) {
6133 if (strstart(devname
, "vc", 0))
6134 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6138 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6139 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6145 do_loadvm(cur_mon
, loadvm
);
6148 autostart
= 0; /* fixme how to deal with -daemonize */
6149 qemu_start_incoming_migration(incoming
);
6161 len
= write(fds
[1], &status
, 1);
6162 if (len
== -1 && (errno
== EINTR
))
6169 TFR(fd
= open("/dev/null", O_RDWR
));
6175 pwd
= getpwnam(run_as
);
6177 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6183 if (chroot(chroot_dir
) < 0) {
6184 fprintf(stderr
, "chroot failed\n");
6191 if (setgid(pwd
->pw_gid
) < 0) {
6192 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6195 if (setuid(pwd
->pw_uid
) < 0) {
6196 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6199 if (setuid(0) != -1) {
6200 fprintf(stderr
, "Dropping privileges failed\n");