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"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
112 #include <sys/timeb.h>
113 #include <mmsystem.h>
114 #define getopt_long_only getopt_long
115 #define memalign(align, size) malloc(size)
119 #if defined(__APPLE__) || defined(main)
121 int qemu_main(int argc
, char **argv
, char **envp
);
122 int main(int argc
, char **argv
)
124 return qemu_main(argc
, argv
, NULL
);
127 #define main qemu_main
129 #endif /* CONFIG_SDL */
133 #define main qemu_main
134 #endif /* CONFIG_COCOA */
137 #include "hw/boards.h"
139 #include "hw/pcmcia.h"
141 #include "hw/audiodev.h"
145 #include "hw/watchdog.h"
146 #include "hw/smbios.h"
154 #include "qemu-timer.h"
155 #include "qemu-char.h"
156 #include "cache-utils.h"
159 #include "audio/audio.h"
160 #include "migration.h"
163 #include "qemu-option.h"
164 #include "qemu-kvm.h"
165 #include "hw/device-assignment.h"
169 #include "exec-all.h"
171 #include "qemu_socket.h"
173 #include "slirp/libslirp.h"
176 //#define DEBUG_SLIRP
178 #define DEFAULT_RAM_SIZE 128
180 /* Max number of USB devices that can be specified on the commandline. */
181 #define MAX_USB_CMDLINE 8
183 /* Max number of bluetooth switches on the commandline. */
184 #define MAX_BT_CMDLINE 10
186 static const char *data_dir
;
187 const char *bios_name
= NULL
;
188 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
189 to store the VM snapshots */
190 DriveInfo drives_table
[MAX_DRIVES
+1];
192 int extboot_drive
= -1;
193 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
194 static DisplayState
*display_state
;
195 DisplayType display_type
= DT_DEFAULT
;
196 const char* keyboard_layout
= NULL
;
197 int64_t ticks_per_sec
;
200 NICInfo nd_table
[MAX_NICS
];
203 static int rtc_utc
= 1;
204 static int rtc_date_offset
= -1; /* -1 means no change */
205 int vga_interface_type
= VGA_CIRRUS
;
207 int graphic_width
= 1024;
208 int graphic_height
= 768;
209 int graphic_depth
= 8;
211 int graphic_width
= 800;
212 int graphic_height
= 600;
213 int graphic_depth
= 15;
215 static int full_screen
= 0;
217 static int no_frame
= 0;
220 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
221 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
222 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
224 int win2k_install_hack
= 0;
229 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
230 int assigned_devices_index
;
232 const char *vnc_display
;
233 int acpi_enabled
= 1;
237 int virtio_balloon
= 1;
238 const char *virtio_balloon_devaddr
;
243 int graphic_rotate
= 0;
244 uint8_t irq0override
= 1;
248 WatchdogTimerModel
*watchdog
= NULL
;
249 int watchdog_action
= WDT_RESET
;
250 const char *option_rom
[MAX_OPTION_ROMS
];
252 int semihosting_enabled
= 0;
253 int time_drift_fix
= 0;
254 unsigned int kvm_shadow_memory
= 0;
255 const char *mem_path
= NULL
;
257 int mem_prealloc
= 1; /* force preallocation of physical target memory */
262 const char *qemu_name
;
264 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
265 unsigned int nb_prom_envs
= 0;
266 const char *prom_envs
[MAX_PROM_ENVS
];
269 const char *nvram
= NULL
;
270 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 static QEMUBootSetHandler
*boot_set_handler
;
293 static void *boot_set_opaque
;
295 /***********************************************************/
296 /* x86 ISA bus support */
298 target_phys_addr_t isa_mem_base
= 0;
301 /***********************************************************/
302 void hw_error(const char *fmt
, ...)
308 fprintf(stderr
, "qemu: hardware error: ");
309 vfprintf(stderr
, fmt
, ap
);
310 fprintf(stderr
, "\n");
311 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
312 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
314 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
316 cpu_dump_state(env
, stderr
, fprintf
, 0);
323 static void set_proc_name(const char *s
)
325 #if defined(__linux__) && defined(PR_SET_NAME)
329 name
[sizeof(name
) - 1] = 0;
330 strncpy(name
, s
, sizeof(name
));
331 /* Could rewrite argv[0] too, but that's a bit more complicated.
332 This simple way is enough for `top'. */
333 prctl(PR_SET_NAME
, name
);
340 static QEMUBalloonEvent
*qemu_balloon_event
;
341 void *qemu_balloon_event_opaque
;
343 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
345 qemu_balloon_event
= func
;
346 qemu_balloon_event_opaque
= opaque
;
349 void qemu_balloon(ram_addr_t target
)
351 if (qemu_balloon_event
)
352 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
355 ram_addr_t
qemu_balloon_status(void)
357 if (qemu_balloon_event
)
358 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
362 /***********************************************************/
365 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
366 static void *qemu_put_kbd_event_opaque
;
367 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
368 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
370 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
372 qemu_put_kbd_event_opaque
= opaque
;
373 qemu_put_kbd_event
= func
;
376 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
377 void *opaque
, int absolute
,
380 QEMUPutMouseEntry
*s
, *cursor
;
382 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
384 s
->qemu_put_mouse_event
= func
;
385 s
->qemu_put_mouse_event_opaque
= opaque
;
386 s
->qemu_put_mouse_event_absolute
= absolute
;
387 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
390 if (!qemu_put_mouse_event_head
) {
391 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
395 cursor
= qemu_put_mouse_event_head
;
396 while (cursor
->next
!= NULL
)
397 cursor
= cursor
->next
;
400 qemu_put_mouse_event_current
= s
;
405 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
407 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
409 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
412 cursor
= qemu_put_mouse_event_head
;
413 while (cursor
!= NULL
&& cursor
!= entry
) {
415 cursor
= cursor
->next
;
418 if (cursor
== NULL
) // does not exist or list empty
420 else if (prev
== NULL
) { // entry is head
421 qemu_put_mouse_event_head
= cursor
->next
;
422 if (qemu_put_mouse_event_current
== entry
)
423 qemu_put_mouse_event_current
= cursor
->next
;
424 qemu_free(entry
->qemu_put_mouse_event_name
);
429 prev
->next
= entry
->next
;
431 if (qemu_put_mouse_event_current
== entry
)
432 qemu_put_mouse_event_current
= prev
;
434 qemu_free(entry
->qemu_put_mouse_event_name
);
438 void kbd_put_keycode(int keycode
)
440 if (qemu_put_kbd_event
) {
441 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
445 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
447 QEMUPutMouseEvent
*mouse_event
;
448 void *mouse_event_opaque
;
451 if (!qemu_put_mouse_event_current
) {
456 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
458 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
461 if (graphic_rotate
) {
462 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
465 width
= graphic_width
- 1;
466 mouse_event(mouse_event_opaque
,
467 width
- dy
, dx
, dz
, buttons_state
);
469 mouse_event(mouse_event_opaque
,
470 dx
, dy
, dz
, buttons_state
);
474 int kbd_mouse_is_absolute(void)
476 if (!qemu_put_mouse_event_current
)
479 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
482 void do_info_mice(Monitor
*mon
)
484 QEMUPutMouseEntry
*cursor
;
487 if (!qemu_put_mouse_event_head
) {
488 monitor_printf(mon
, "No mouse devices connected\n");
492 monitor_printf(mon
, "Mouse devices available:\n");
493 cursor
= qemu_put_mouse_event_head
;
494 while (cursor
!= NULL
) {
495 monitor_printf(mon
, "%c Mouse #%d: %s\n",
496 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
497 index
, cursor
->qemu_put_mouse_event_name
);
499 cursor
= cursor
->next
;
503 void do_mouse_set(Monitor
*mon
, int index
)
505 QEMUPutMouseEntry
*cursor
;
508 if (!qemu_put_mouse_event_head
) {
509 monitor_printf(mon
, "No mouse devices connected\n");
513 cursor
= qemu_put_mouse_event_head
;
514 while (cursor
!= NULL
&& index
!= i
) {
516 cursor
= cursor
->next
;
520 qemu_put_mouse_event_current
= cursor
;
522 monitor_printf(mon
, "Mouse at given index not found\n");
525 /* compute with 96 bit intermediate result: (a*b)/c */
526 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
531 #ifdef WORDS_BIGENDIAN
541 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
542 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
545 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
549 /***********************************************************/
550 /* real time host monotonic timer */
552 #define QEMU_TIMER_BASE 1000000000LL
556 static int64_t clock_freq
;
558 static void init_get_clock(void)
562 ret
= QueryPerformanceFrequency(&freq
);
564 fprintf(stderr
, "Could not calibrate ticks\n");
567 clock_freq
= freq
.QuadPart
;
570 static int64_t get_clock(void)
573 QueryPerformanceCounter(&ti
);
574 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
579 static int use_rt_clock
;
581 static void init_get_clock(void)
584 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
585 || defined(__DragonFly__)
588 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
595 static int64_t get_clock(void)
597 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
598 || defined(__DragonFly__)
601 clock_gettime(CLOCK_MONOTONIC
, &ts
);
602 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
606 /* XXX: using gettimeofday leads to problems if the date
607 changes, so it should be avoided. */
609 gettimeofday(&tv
, NULL
);
610 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
615 /* Return the virtual CPU time, based on the instruction counter. */
616 static int64_t cpu_get_icount(void)
619 CPUState
*env
= cpu_single_env
;;
620 icount
= qemu_icount
;
623 fprintf(stderr
, "Bad clock read\n");
624 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
626 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
629 /***********************************************************/
630 /* guest cycle counter */
632 static int64_t cpu_ticks_prev
;
633 static int64_t cpu_ticks_offset
;
634 static int64_t cpu_clock_offset
;
635 static int cpu_ticks_enabled
;
637 /* return the host CPU cycle counter and handle stop/restart */
638 int64_t cpu_get_ticks(void)
641 return cpu_get_icount();
643 if (!cpu_ticks_enabled
) {
644 return cpu_ticks_offset
;
647 ticks
= cpu_get_real_ticks();
648 if (cpu_ticks_prev
> ticks
) {
649 /* Note: non increasing ticks may happen if the host uses
651 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
653 cpu_ticks_prev
= ticks
;
654 return ticks
+ cpu_ticks_offset
;
658 /* return the host CPU monotonic timer and handle stop/restart */
659 static int64_t cpu_get_clock(void)
662 if (!cpu_ticks_enabled
) {
663 return cpu_clock_offset
;
666 return ti
+ cpu_clock_offset
;
670 /* enable cpu_get_ticks() */
671 void cpu_enable_ticks(void)
673 if (!cpu_ticks_enabled
) {
674 cpu_ticks_offset
-= cpu_get_real_ticks();
675 cpu_clock_offset
-= get_clock();
676 cpu_ticks_enabled
= 1;
680 /* disable cpu_get_ticks() : the clock is stopped. You must not call
681 cpu_get_ticks() after that. */
682 void cpu_disable_ticks(void)
684 if (cpu_ticks_enabled
) {
685 cpu_ticks_offset
= cpu_get_ticks();
686 cpu_clock_offset
= cpu_get_clock();
687 cpu_ticks_enabled
= 0;
691 /***********************************************************/
694 #define QEMU_TIMER_REALTIME 0
695 #define QEMU_TIMER_VIRTUAL 1
699 /* XXX: add frequency */
707 struct QEMUTimer
*next
;
710 struct qemu_alarm_timer
{
714 int (*start
)(struct qemu_alarm_timer
*t
);
715 void (*stop
)(struct qemu_alarm_timer
*t
);
716 void (*rearm
)(struct qemu_alarm_timer
*t
);
720 #define ALARM_FLAG_DYNTICKS 0x1
721 #define ALARM_FLAG_EXPIRED 0x2
723 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
725 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
728 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
730 if (!alarm_has_dynticks(t
))
736 /* TODO: MIN_TIMER_REARM_US should be optimized */
737 #define MIN_TIMER_REARM_US 250
739 static struct qemu_alarm_timer
*alarm_timer
;
743 struct qemu_alarm_win32
{
746 } alarm_win32_data
= {0, -1};
748 static int win32_start_timer(struct qemu_alarm_timer
*t
);
749 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
750 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
754 static int unix_start_timer(struct qemu_alarm_timer
*t
);
755 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
759 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
760 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
761 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
763 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
764 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
766 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
767 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
769 #endif /* __linux__ */
773 /* Correlation between real and virtual time is always going to be
774 fairly approximate, so ignore small variation.
775 When the guest is idle real and virtual time will be aligned in
777 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
779 static void icount_adjust(void)
784 static int64_t last_delta
;
785 /* If the VM is not running, then do nothing. */
789 cur_time
= cpu_get_clock();
790 cur_icount
= qemu_get_clock(vm_clock
);
791 delta
= cur_icount
- cur_time
;
792 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
794 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
795 && icount_time_shift
> 0) {
796 /* The guest is getting too far ahead. Slow time down. */
800 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
801 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
802 /* The guest is getting too far behind. Speed time up. */
806 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
809 static void icount_adjust_rt(void * opaque
)
811 qemu_mod_timer(icount_rt_timer
,
812 qemu_get_clock(rt_clock
) + 1000);
816 static void icount_adjust_vm(void * opaque
)
818 qemu_mod_timer(icount_vm_timer
,
819 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
823 static void init_icount_adjust(void)
825 /* Have both realtime and virtual time triggers for speed adjustment.
826 The realtime trigger catches emulated time passing too slowly,
827 the virtual time trigger catches emulated time passing too fast.
828 Realtime triggers occur even when idle, so use them less frequently
830 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
831 qemu_mod_timer(icount_rt_timer
,
832 qemu_get_clock(rt_clock
) + 1000);
833 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
834 qemu_mod_timer(icount_vm_timer
,
835 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
838 static struct qemu_alarm_timer alarm_timers
[] = {
841 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
842 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
843 /* HPET - if available - is preferred */
844 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
845 /* ...otherwise try RTC */
846 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
848 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
850 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
851 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
852 {"win32", 0, win32_start_timer
,
853 win32_stop_timer
, NULL
, &alarm_win32_data
},
858 static void show_available_alarms(void)
862 printf("Available alarm timers, in order of precedence:\n");
863 for (i
= 0; alarm_timers
[i
].name
; i
++)
864 printf("%s\n", alarm_timers
[i
].name
);
867 static void configure_alarms(char const *opt
)
871 int count
= ARRAY_SIZE(alarm_timers
) - 1;
874 struct qemu_alarm_timer tmp
;
876 if (!strcmp(opt
, "?")) {
877 show_available_alarms();
883 /* Reorder the array */
884 name
= strtok(arg
, ",");
886 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
887 if (!strcmp(alarm_timers
[i
].name
, name
))
892 fprintf(stderr
, "Unknown clock %s\n", name
);
901 tmp
= alarm_timers
[i
];
902 alarm_timers
[i
] = alarm_timers
[cur
];
903 alarm_timers
[cur
] = tmp
;
907 name
= strtok(NULL
, ",");
913 /* Disable remaining timers */
914 for (i
= cur
; i
< count
; i
++)
915 alarm_timers
[i
].name
= NULL
;
917 show_available_alarms();
925 static QEMUTimer
*active_timers
[2];
927 static QEMUClock
*qemu_new_clock(int type
)
930 clock
= qemu_mallocz(sizeof(QEMUClock
));
935 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
939 ts
= qemu_mallocz(sizeof(QEMUTimer
));
946 void qemu_free_timer(QEMUTimer
*ts
)
951 /* stop a timer, but do not dealloc it */
952 void qemu_del_timer(QEMUTimer
*ts
)
956 /* NOTE: this code must be signal safe because
957 qemu_timer_expired() can be called from a signal. */
958 pt
= &active_timers
[ts
->clock
->type
];
971 /* modify the current timer so that it will be fired when current_time
972 >= expire_time. The corresponding callback will be called. */
973 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
979 /* add the timer in the sorted list */
980 /* NOTE: this code must be signal safe because
981 qemu_timer_expired() can be called from a signal. */
982 pt
= &active_timers
[ts
->clock
->type
];
987 if (t
->expire_time
> expire_time
)
991 ts
->expire_time
= expire_time
;
995 /* Rearm if necessary */
996 if (pt
== &active_timers
[ts
->clock
->type
]) {
997 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
998 qemu_rearm_alarm_timer(alarm_timer
);
1000 /* Interrupt execution to force deadline recalculation. */
1002 qemu_notify_event();
1006 int qemu_timer_pending(QEMUTimer
*ts
)
1009 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1016 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1020 return (timer_head
->expire_time
<= current_time
);
1023 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1029 if (!ts
|| ts
->expire_time
> current_time
)
1031 /* remove timer from the list before calling the callback */
1032 *ptimer_head
= ts
->next
;
1035 /* run the callback (the timer list can be modified) */
1040 int64_t qemu_get_clock(QEMUClock
*clock
)
1042 switch(clock
->type
) {
1043 case QEMU_TIMER_REALTIME
:
1044 return get_clock() / 1000000;
1046 case QEMU_TIMER_VIRTUAL
:
1048 return cpu_get_icount();
1050 return cpu_get_clock();
1055 static void init_timers(void)
1058 ticks_per_sec
= QEMU_TIMER_BASE
;
1059 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1060 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1064 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1066 uint64_t expire_time
;
1068 if (qemu_timer_pending(ts
)) {
1069 expire_time
= ts
->expire_time
;
1073 qemu_put_be64(f
, expire_time
);
1076 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1078 uint64_t expire_time
;
1080 expire_time
= qemu_get_be64(f
);
1081 if (expire_time
!= -1) {
1082 qemu_mod_timer(ts
, expire_time
);
1088 static void timer_save(QEMUFile
*f
, void *opaque
)
1090 if (cpu_ticks_enabled
) {
1091 hw_error("cannot save state if virtual timers are running");
1093 qemu_put_be64(f
, cpu_ticks_offset
);
1094 qemu_put_be64(f
, ticks_per_sec
);
1095 qemu_put_be64(f
, cpu_clock_offset
);
1098 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1100 if (version_id
!= 1 && version_id
!= 2)
1102 if (cpu_ticks_enabled
) {
1105 cpu_ticks_offset
=qemu_get_be64(f
);
1106 ticks_per_sec
=qemu_get_be64(f
);
1107 if (version_id
== 2) {
1108 cpu_clock_offset
=qemu_get_be64(f
);
1113 static void qemu_event_increment(void);
1116 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1117 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1120 static void host_alarm_handler(int host_signum
)
1124 #define DISP_FREQ 1000
1126 static int64_t delta_min
= INT64_MAX
;
1127 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1129 ti
= qemu_get_clock(vm_clock
);
1130 if (last_clock
!= 0) {
1131 delta
= ti
- last_clock
;
1132 if (delta
< delta_min
)
1134 if (delta
> delta_max
)
1137 if (++count
== DISP_FREQ
) {
1138 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1139 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1140 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1141 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1142 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1144 delta_min
= INT64_MAX
;
1152 if (alarm_has_dynticks(alarm_timer
) ||
1154 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1155 qemu_get_clock(vm_clock
))) ||
1156 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1157 qemu_get_clock(rt_clock
))) {
1158 qemu_event_increment();
1159 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1161 #ifndef CONFIG_IOTHREAD
1163 /* stop the currently executing cpu because a timer occured */
1166 if (next_cpu
->kqemu_enabled
) {
1167 kqemu_cpu_interrupt(next_cpu
);
1172 timer_alarm_pending
= 1;
1173 qemu_notify_event();
1177 static int64_t qemu_next_deadline(void)
1181 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1182 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1183 qemu_get_clock(vm_clock
);
1185 /* To avoid problems with overflow limit this to 2^32. */
1195 #if defined(__linux__) || defined(_WIN32)
1196 static uint64_t qemu_next_deadline_dyntick(void)
1204 delta
= (qemu_next_deadline() + 999) / 1000;
1206 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1207 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1208 qemu_get_clock(rt_clock
))*1000;
1209 if (rtdelta
< delta
)
1213 if (delta
< MIN_TIMER_REARM_US
)
1214 delta
= MIN_TIMER_REARM_US
;
1222 /* Sets a specific flag */
1223 static int fcntl_setfl(int fd
, int flag
)
1227 flags
= fcntl(fd
, F_GETFL
);
1231 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1237 #if defined(__linux__)
1239 #define RTC_FREQ 1024
1241 static void enable_sigio_timer(int fd
)
1243 struct sigaction act
;
1246 sigfillset(&act
.sa_mask
);
1248 act
.sa_handler
= host_alarm_handler
;
1250 sigaction(SIGIO
, &act
, NULL
);
1251 fcntl_setfl(fd
, O_ASYNC
);
1252 fcntl(fd
, F_SETOWN
, getpid());
1255 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1257 struct hpet_info info
;
1260 fd
= open("/dev/hpet", O_RDONLY
);
1265 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1267 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1268 "error, but for better emulation accuracy type:\n"
1269 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1273 /* Check capabilities */
1274 r
= ioctl(fd
, HPET_INFO
, &info
);
1278 /* Enable periodic mode */
1279 r
= ioctl(fd
, HPET_EPI
, 0);
1280 if (info
.hi_flags
&& (r
< 0))
1283 /* Enable interrupt */
1284 r
= ioctl(fd
, HPET_IE_ON
, 0);
1288 enable_sigio_timer(fd
);
1289 t
->priv
= (void *)(long)fd
;
1297 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1299 int fd
= (long)t
->priv
;
1304 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1307 unsigned long current_rtc_freq
= 0;
1309 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1312 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1313 if (current_rtc_freq
!= RTC_FREQ
&&
1314 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1315 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1316 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1317 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1320 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1326 enable_sigio_timer(rtc_fd
);
1328 t
->priv
= (void *)(long)rtc_fd
;
1333 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1335 int rtc_fd
= (long)t
->priv
;
1340 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1344 struct sigaction act
;
1346 sigfillset(&act
.sa_mask
);
1348 act
.sa_handler
= host_alarm_handler
;
1350 sigaction(SIGALRM
, &act
, NULL
);
1353 * Initialize ev struct to 0 to avoid valgrind complaining
1354 * about uninitialized data in timer_create call
1356 memset(&ev
, 0, sizeof(ev
));
1357 ev
.sigev_value
.sival_int
= 0;
1358 ev
.sigev_notify
= SIGEV_SIGNAL
;
1359 ev
.sigev_signo
= SIGALRM
;
1361 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1362 perror("timer_create");
1364 /* disable dynticks */
1365 fprintf(stderr
, "Dynamic Ticks disabled\n");
1370 t
->priv
= (void *)(long)host_timer
;
1375 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1377 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1379 timer_delete(host_timer
);
1382 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1384 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1385 struct itimerspec timeout
;
1386 int64_t nearest_delta_us
= INT64_MAX
;
1389 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1390 !active_timers
[QEMU_TIMER_VIRTUAL
])
1393 nearest_delta_us
= qemu_next_deadline_dyntick();
1395 /* check whether a timer is already running */
1396 if (timer_gettime(host_timer
, &timeout
)) {
1398 fprintf(stderr
, "Internal timer error: aborting\n");
1401 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1402 if (current_us
&& current_us
<= nearest_delta_us
)
1405 timeout
.it_interval
.tv_sec
= 0;
1406 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1407 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1408 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1409 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1411 fprintf(stderr
, "Internal timer error: aborting\n");
1416 #endif /* defined(__linux__) */
1418 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1420 struct sigaction act
;
1421 struct itimerval itv
;
1425 sigfillset(&act
.sa_mask
);
1427 act
.sa_handler
= host_alarm_handler
;
1429 sigaction(SIGALRM
, &act
, NULL
);
1431 itv
.it_interval
.tv_sec
= 0;
1432 /* for i386 kernel 2.6 to get 1 ms */
1433 itv
.it_interval
.tv_usec
= 999;
1434 itv
.it_value
.tv_sec
= 0;
1435 itv
.it_value
.tv_usec
= 10 * 1000;
1437 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1444 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1446 struct itimerval itv
;
1448 memset(&itv
, 0, sizeof(itv
));
1449 setitimer(ITIMER_REAL
, &itv
, NULL
);
1452 #endif /* !defined(_WIN32) */
1457 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1460 struct qemu_alarm_win32
*data
= t
->priv
;
1463 memset(&tc
, 0, sizeof(tc
));
1464 timeGetDevCaps(&tc
, sizeof(tc
));
1466 if (data
->period
< tc
.wPeriodMin
)
1467 data
->period
= tc
.wPeriodMin
;
1469 timeBeginPeriod(data
->period
);
1471 flags
= TIME_CALLBACK_FUNCTION
;
1472 if (alarm_has_dynticks(t
))
1473 flags
|= TIME_ONESHOT
;
1475 flags
|= TIME_PERIODIC
;
1477 data
->timerId
= timeSetEvent(1, // interval (ms)
1478 data
->period
, // resolution
1479 host_alarm_handler
, // function
1480 (DWORD
)t
, // parameter
1483 if (!data
->timerId
) {
1484 perror("Failed to initialize win32 alarm timer");
1485 timeEndPeriod(data
->period
);
1492 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1494 struct qemu_alarm_win32
*data
= t
->priv
;
1496 timeKillEvent(data
->timerId
);
1497 timeEndPeriod(data
->period
);
1500 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1502 struct qemu_alarm_win32
*data
= t
->priv
;
1503 uint64_t nearest_delta_us
;
1505 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1506 !active_timers
[QEMU_TIMER_VIRTUAL
])
1509 nearest_delta_us
= qemu_next_deadline_dyntick();
1510 nearest_delta_us
/= 1000;
1512 timeKillEvent(data
->timerId
);
1514 data
->timerId
= timeSetEvent(1,
1518 TIME_ONESHOT
| TIME_PERIODIC
);
1520 if (!data
->timerId
) {
1521 perror("Failed to re-arm win32 alarm timer");
1523 timeEndPeriod(data
->period
);
1530 static int init_timer_alarm(void)
1532 struct qemu_alarm_timer
*t
= NULL
;
1535 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1536 t
= &alarm_timers
[i
];
1556 static void quit_timers(void)
1558 alarm_timer
->stop(alarm_timer
);
1562 /***********************************************************/
1563 /* host time/date access */
1564 void qemu_get_timedate(struct tm
*tm
, int offset
)
1571 if (rtc_date_offset
== -1) {
1575 ret
= localtime(&ti
);
1577 ti
-= rtc_date_offset
;
1581 memcpy(tm
, ret
, sizeof(struct tm
));
1584 int qemu_timedate_diff(struct tm
*tm
)
1588 if (rtc_date_offset
== -1)
1590 seconds
= mktimegm(tm
);
1592 seconds
= mktime(tm
);
1594 seconds
= mktimegm(tm
) + rtc_date_offset
;
1596 return seconds
- time(NULL
);
1600 static void socket_cleanup(void)
1605 static int socket_init(void)
1610 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1612 err
= WSAGetLastError();
1613 fprintf(stderr
, "WSAStartup: %d\n", err
);
1616 atexit(socket_cleanup
);
1621 int get_next_param_value(char *buf
, int buf_size
,
1622 const char *tag
, const char **pstr
)
1629 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1633 if (!strcmp(tag
, option
)) {
1634 *pstr
= get_opt_value(buf
, buf_size
, p
);
1635 if (**pstr
== ',') {
1640 p
= get_opt_value(NULL
, 0, p
);
1649 int get_param_value(char *buf
, int buf_size
,
1650 const char *tag
, const char *str
)
1652 return get_next_param_value(buf
, buf_size
, tag
, &str
);
1655 int check_params(char *buf
, int buf_size
,
1656 const char * const *params
, const char *str
)
1662 while (*p
!= '\0') {
1663 p
= get_opt_name(buf
, buf_size
, p
, '=');
1668 for (i
= 0; params
[i
] != NULL
; i
++) {
1669 if (!strcmp(params
[i
], buf
)) {
1673 if (params
[i
] == NULL
) {
1676 p
= get_opt_value(NULL
, 0, p
);
1685 /***********************************************************/
1686 /* Bluetooth support */
1689 static struct HCIInfo
*hci_table
[MAX_NICS
];
1691 static struct bt_vlan_s
{
1692 struct bt_scatternet_s net
;
1694 struct bt_vlan_s
*next
;
1697 /* find or alloc a new bluetooth "VLAN" */
1698 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1700 struct bt_vlan_s
**pvlan
, *vlan
;
1701 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1705 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1707 pvlan
= &first_bt_vlan
;
1708 while (*pvlan
!= NULL
)
1709 pvlan
= &(*pvlan
)->next
;
1714 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1718 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1723 static struct HCIInfo null_hci
= {
1724 .cmd_send
= null_hci_send
,
1725 .sco_send
= null_hci_send
,
1726 .acl_send
= null_hci_send
,
1727 .bdaddr_set
= null_hci_addr_set
,
1730 struct HCIInfo
*qemu_next_hci(void)
1732 if (cur_hci
== nb_hcis
)
1735 return hci_table
[cur_hci
++];
1738 static struct HCIInfo
*hci_init(const char *str
)
1741 struct bt_scatternet_s
*vlan
= 0;
1743 if (!strcmp(str
, "null"))
1746 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1748 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1749 else if (!strncmp(str
, "hci", 3)) {
1752 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1753 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1758 vlan
= qemu_find_bt_vlan(0);
1760 return bt_new_hci(vlan
);
1763 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1768 static int bt_hci_parse(const char *str
)
1770 struct HCIInfo
*hci
;
1773 if (nb_hcis
>= MAX_NICS
) {
1774 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1778 hci
= hci_init(str
);
1787 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1788 hci
->bdaddr_set(hci
, bdaddr
.b
);
1790 hci_table
[nb_hcis
++] = hci
;
1795 static void bt_vhci_add(int vlan_id
)
1797 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1800 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1801 "an empty scatternet %i\n", vlan_id
);
1803 bt_vhci_init(bt_new_hci(vlan
));
1806 static struct bt_device_s
*bt_device_add(const char *opt
)
1808 struct bt_scatternet_s
*vlan
;
1810 char *endp
= strstr(opt
, ",vlan=");
1811 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1814 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1817 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1819 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1824 vlan
= qemu_find_bt_vlan(vlan_id
);
1827 fprintf(stderr
, "qemu: warning: adding a slave device to "
1828 "an empty scatternet %i\n", vlan_id
);
1830 if (!strcmp(devname
, "keyboard"))
1831 return bt_keyboard_init(vlan
);
1833 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1837 static int bt_parse(const char *opt
)
1839 const char *endp
, *p
;
1842 if (strstart(opt
, "hci", &endp
)) {
1843 if (!*endp
|| *endp
== ',') {
1845 if (!strstart(endp
, ",vlan=", 0))
1848 return bt_hci_parse(opt
);
1850 } else if (strstart(opt
, "vhci", &endp
)) {
1851 if (!*endp
|| *endp
== ',') {
1853 if (strstart(endp
, ",vlan=", &p
)) {
1854 vlan
= strtol(p
, (char **) &endp
, 0);
1856 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1860 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1869 } else if (strstart(opt
, "device:", &endp
))
1870 return !bt_device_add(endp
);
1872 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1876 /***********************************************************/
1877 /* QEMU Block devices */
1879 #define HD_ALIAS "index=%d,media=disk"
1880 #define CDROM_ALIAS "index=2,media=cdrom"
1881 #define FD_ALIAS "index=%d,if=floppy"
1882 #define PFLASH_ALIAS "if=pflash"
1883 #define MTD_ALIAS "if=mtd"
1884 #define SD_ALIAS "index=0,if=sd"
1886 static int drive_opt_get_free_idx(void)
1890 for (index
= 0; index
< MAX_DRIVES
; index
++)
1891 if (!drives_opt
[index
].used
) {
1892 drives_opt
[index
].used
= 1;
1899 static int drive_get_free_idx(void)
1903 for (index
= 0; index
< MAX_DRIVES
; index
++)
1904 if (!drives_table
[index
].used
) {
1905 drives_table
[index
].used
= 1;
1912 int drive_add(const char *file
, const char *fmt
, ...)
1915 int index
= drive_opt_get_free_idx();
1917 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
1918 fprintf(stderr
, "qemu: too many drives\n");
1922 drives_opt
[index
].file
= file
;
1924 vsnprintf(drives_opt
[index
].opt
,
1925 sizeof(drives_opt
[0].opt
), fmt
, ap
);
1932 void drive_remove(int index
)
1934 drives_opt
[index
].used
= 0;
1938 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
1942 /* seek interface, bus and unit */
1944 for (index
= 0; index
< MAX_DRIVES
; index
++)
1945 if (drives_table
[index
].type
== type
&&
1946 drives_table
[index
].bus
== bus
&&
1947 drives_table
[index
].unit
== unit
&&
1948 drives_table
[index
].used
)
1954 int drive_get_max_bus(BlockInterfaceType type
)
1960 for (index
= 0; index
< nb_drives
; index
++) {
1961 if(drives_table
[index
].type
== type
&&
1962 drives_table
[index
].bus
> max_bus
)
1963 max_bus
= drives_table
[index
].bus
;
1968 const char *drive_get_serial(BlockDriverState
*bdrv
)
1972 for (index
= 0; index
< nb_drives
; index
++)
1973 if (drives_table
[index
].bdrv
== bdrv
)
1974 return drives_table
[index
].serial
;
1979 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1983 for (index
= 0; index
< nb_drives
; index
++)
1984 if (drives_table
[index
].bdrv
== bdrv
)
1985 return drives_table
[index
].onerror
;
1987 return BLOCK_ERR_STOP_ENOSPC
;
1990 static void bdrv_format_print(void *opaque
, const char *name
)
1992 fprintf(stderr
, " %s", name
);
1995 void drive_uninit(BlockDriverState
*bdrv
)
1999 for (i
= 0; i
< MAX_DRIVES
; i
++)
2000 if (drives_table
[i
].bdrv
== bdrv
) {
2001 drives_table
[i
].bdrv
= NULL
;
2002 drives_table
[i
].used
= 0;
2003 drive_remove(drives_table
[i
].drive_opt_idx
);
2009 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2015 const char *mediastr
= "";
2016 BlockInterfaceType type
;
2017 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2018 int bus_id
, unit_id
;
2019 int cyls
, heads
, secs
, translation
;
2020 BlockDriverState
*bdrv
;
2021 BlockDriver
*drv
= NULL
;
2022 QEMUMachine
*machine
= opaque
;
2026 int bdrv_flags
, onerror
;
2027 const char *devaddr
;
2028 int drives_table_idx
;
2029 char *str
= arg
->opt
;
2030 static const char * const params
[] = { "bus", "unit", "if", "index",
2031 "cyls", "heads", "secs", "trans",
2032 "media", "snapshot", "file",
2033 "cache", "format", "serial",
2038 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2039 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2045 cyls
= heads
= secs
= 0;
2048 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2052 if (machine
->use_scsi
) {
2054 max_devs
= MAX_SCSI_DEVS
;
2055 pstrcpy(devname
, sizeof(devname
), "scsi");
2058 max_devs
= MAX_IDE_DEVS
;
2059 pstrcpy(devname
, sizeof(devname
), "ide");
2063 /* extract parameters */
2065 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2066 bus_id
= strtol(buf
, NULL
, 0);
2068 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2073 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2074 unit_id
= strtol(buf
, NULL
, 0);
2076 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2081 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2082 pstrcpy(devname
, sizeof(devname
), buf
);
2083 if (!strcmp(buf
, "ide")) {
2085 max_devs
= MAX_IDE_DEVS
;
2086 } else if (!strcmp(buf
, "scsi")) {
2088 max_devs
= MAX_SCSI_DEVS
;
2089 } else if (!strcmp(buf
, "floppy")) {
2092 } else if (!strcmp(buf
, "pflash")) {
2095 } else if (!strcmp(buf
, "mtd")) {
2098 } else if (!strcmp(buf
, "sd")) {
2101 } else if (!strcmp(buf
, "virtio")) {
2104 } else if (!strcmp(buf
, "xen")) {
2108 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2113 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2114 index
= strtol(buf
, NULL
, 0);
2116 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2121 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2122 cyls
= strtol(buf
, NULL
, 0);
2125 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2126 heads
= strtol(buf
, NULL
, 0);
2129 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2130 secs
= strtol(buf
, NULL
, 0);
2133 if (cyls
|| heads
|| secs
) {
2134 if (cyls
< 1 || cyls
> 16383) {
2135 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2138 if (heads
< 1 || heads
> 16) {
2139 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2142 if (secs
< 1 || secs
> 63) {
2143 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2148 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2151 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2155 if (!strcmp(buf
, "none"))
2156 translation
= BIOS_ATA_TRANSLATION_NONE
;
2157 else if (!strcmp(buf
, "lba"))
2158 translation
= BIOS_ATA_TRANSLATION_LBA
;
2159 else if (!strcmp(buf
, "auto"))
2160 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2162 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2167 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2168 if (!strcmp(buf
, "disk")) {
2170 } else if (!strcmp(buf
, "cdrom")) {
2171 if (cyls
|| secs
|| heads
) {
2173 "qemu: '%s' invalid physical CHS format\n", str
);
2176 media
= MEDIA_CDROM
;
2178 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2183 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2184 if (!strcmp(buf
, "on"))
2186 else if (!strcmp(buf
, "off"))
2189 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2194 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2195 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2197 else if (!strcmp(buf
, "writethrough"))
2199 else if (!strcmp(buf
, "writeback"))
2202 fprintf(stderr
, "qemu: invalid cache option\n");
2207 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2208 if (strcmp(buf
, "?") == 0) {
2209 fprintf(stderr
, "qemu: Supported formats:");
2210 bdrv_iterate_format(bdrv_format_print
, NULL
);
2211 fprintf(stderr
, "\n");
2214 drv
= bdrv_find_format(buf
);
2216 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2221 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2222 if (!strcmp(buf
, "on")) {
2223 if (extboot_drive
!= -1) {
2224 fprintf(stderr
, "qemu: two bootable drives specified\n");
2227 extboot_drive
= nb_drives
;
2228 } else if (strcmp(buf
, "off")) {
2229 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2234 if (arg
->file
== NULL
)
2235 get_param_value(file
, sizeof(file
), "file", str
);
2237 pstrcpy(file
, sizeof(file
), arg
->file
);
2239 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2240 memset(serial
, 0, sizeof(serial
));
2242 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2243 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2244 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2245 fprintf(stderr
, "werror is no supported by this format\n");
2248 if (!strcmp(buf
, "ignore"))
2249 onerror
= BLOCK_ERR_IGNORE
;
2250 else if (!strcmp(buf
, "enospc"))
2251 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2252 else if (!strcmp(buf
, "stop"))
2253 onerror
= BLOCK_ERR_STOP_ANY
;
2254 else if (!strcmp(buf
, "report"))
2255 onerror
= BLOCK_ERR_REPORT
;
2257 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2263 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2264 if (type
!= IF_VIRTIO
) {
2265 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2268 devaddr
= strdup(buf
);
2271 /* compute bus and unit according index */
2274 if (bus_id
!= 0 || unit_id
!= -1) {
2276 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2284 unit_id
= index
% max_devs
;
2285 bus_id
= index
/ max_devs
;
2289 /* if user doesn't specify a unit_id,
2290 * try to find the first free
2293 if (unit_id
== -1) {
2295 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2297 if (max_devs
&& unit_id
>= max_devs
) {
2298 unit_id
-= max_devs
;
2306 if (max_devs
&& unit_id
>= max_devs
) {
2307 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2308 str
, unit_id
, max_devs
- 1);
2313 * ignore multiple definitions
2316 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2321 if (type
== IF_IDE
|| type
== IF_SCSI
)
2322 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2324 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2325 devname
, bus_id
, mediastr
, unit_id
);
2327 snprintf(buf
, sizeof(buf
), "%s%s%i",
2328 devname
, mediastr
, unit_id
);
2329 bdrv
= bdrv_new(buf
);
2330 drives_table_idx
= drive_get_free_idx();
2331 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2332 drives_table
[drives_table_idx
].devaddr
= devaddr
;
2333 drives_table
[drives_table_idx
].type
= type
;
2334 drives_table
[drives_table_idx
].bus
= bus_id
;
2335 drives_table
[drives_table_idx
].unit
= unit_id
;
2336 drives_table
[drives_table_idx
].onerror
= onerror
;
2337 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2338 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2348 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2349 bdrv_set_translation_hint(bdrv
, translation
);
2353 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2358 /* FIXME: This isn't really a floppy, but it's a reasonable
2361 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2374 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2375 cache
= 2; /* always use write-back with snapshot */
2377 if (cache
== 0) /* no caching */
2378 bdrv_flags
|= BDRV_O_NOCACHE
;
2379 else if (cache
== 2) /* write-back */
2380 bdrv_flags
|= BDRV_O_CACHE_WB
;
2381 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2382 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2383 file
, strerror(errno
));
2386 if (bdrv_key_required(bdrv
))
2388 return drives_table_idx
;
2391 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2393 boot_set_handler
= func
;
2394 boot_set_opaque
= opaque
;
2397 int qemu_boot_set(const char *boot_devices
)
2399 if (!boot_set_handler
) {
2402 return boot_set_handler(boot_set_opaque
, boot_devices
);
2405 static int parse_bootdevices(char *devices
)
2407 /* We just do some generic consistency checks */
2411 for (p
= devices
; *p
!= '\0'; p
++) {
2412 /* Allowed boot devices are:
2413 * a-b: floppy disk drives
2414 * c-f: IDE disk drives
2415 * g-m: machine implementation dependant drives
2416 * n-p: network devices
2417 * It's up to each machine implementation to check if the given boot
2418 * devices match the actual hardware implementation and firmware
2421 if (*p
< 'a' || *p
> 'p') {
2422 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2425 if (bitmap
& (1 << (*p
- 'a'))) {
2426 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2429 bitmap
|= 1 << (*p
- 'a');
2434 static void restore_boot_devices(void *opaque
)
2436 char *standard_boot_devices
= opaque
;
2438 qemu_boot_set(standard_boot_devices
);
2440 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2441 qemu_free(standard_boot_devices
);
2444 static void numa_add(const char *optarg
)
2448 unsigned long long value
, endvalue
;
2451 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2452 if (!strcmp(option
, "node")) {
2453 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2454 nodenr
= nb_numa_nodes
;
2456 nodenr
= strtoull(option
, NULL
, 10);
2459 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2460 node_mem
[nodenr
] = 0;
2462 value
= strtoull(option
, &endptr
, 0);
2464 case 0: case 'M': case 'm':
2471 node_mem
[nodenr
] = value
;
2473 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2474 node_cpumask
[nodenr
] = 0;
2476 value
= strtoull(option
, &endptr
, 10);
2479 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2481 if (*endptr
== '-') {
2482 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2483 if (endvalue
>= 63) {
2486 "only 63 CPUs in NUMA mode supported.\n");
2488 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2493 node_cpumask
[nodenr
] = value
;
2500 /***********************************************************/
2503 static USBPort
*used_usb_ports
;
2504 static USBPort
*free_usb_ports
;
2506 /* ??? Maybe change this to register a hub to keep track of the topology. */
2507 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2508 usb_attachfn attach
)
2510 port
->opaque
= opaque
;
2511 port
->index
= index
;
2512 port
->attach
= attach
;
2513 port
->next
= free_usb_ports
;
2514 free_usb_ports
= port
;
2517 int usb_device_add_dev(USBDevice
*dev
)
2521 /* Find a USB port to add the device to. */
2522 port
= free_usb_ports
;
2526 /* Create a new hub and chain it on. */
2527 free_usb_ports
= NULL
;
2528 port
->next
= used_usb_ports
;
2529 used_usb_ports
= port
;
2531 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2532 usb_attach(port
, hub
);
2533 port
= free_usb_ports
;
2536 free_usb_ports
= port
->next
;
2537 port
->next
= used_usb_ports
;
2538 used_usb_ports
= port
;
2539 usb_attach(port
, dev
);
2543 static void usb_msd_password_cb(void *opaque
, int err
)
2545 USBDevice
*dev
= opaque
;
2548 usb_device_add_dev(dev
);
2550 dev
->handle_destroy(dev
);
2553 static int usb_device_add(const char *devname
, int is_hotplug
)
2558 if (!free_usb_ports
)
2561 if (strstart(devname
, "host:", &p
)) {
2562 dev
= usb_host_device_open(p
);
2563 } else if (!strcmp(devname
, "mouse")) {
2564 dev
= usb_mouse_init();
2565 } else if (!strcmp(devname
, "tablet")) {
2566 dev
= usb_tablet_init();
2567 } else if (!strcmp(devname
, "keyboard")) {
2568 dev
= usb_keyboard_init();
2569 } else if (strstart(devname
, "disk:", &p
)) {
2570 BlockDriverState
*bs
;
2572 dev
= usb_msd_init(p
);
2575 bs
= usb_msd_get_bdrv(dev
);
2576 if (bdrv_key_required(bs
)) {
2579 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2584 } else if (!strcmp(devname
, "wacom-tablet")) {
2585 dev
= usb_wacom_init();
2586 } else if (strstart(devname
, "serial:", &p
)) {
2587 dev
= usb_serial_init(p
);
2588 #ifdef CONFIG_BRLAPI
2589 } else if (!strcmp(devname
, "braille")) {
2590 dev
= usb_baum_init();
2592 } else if (strstart(devname
, "net:", &p
)) {
2595 if (net_client_init(NULL
, "nic", p
) < 0)
2597 nd_table
[nic
].model
= qemu_strdup("usb");
2598 dev
= usb_net_init(&nd_table
[nic
]);
2599 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2600 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2601 bt_new_hci(qemu_find_bt_vlan(0)));
2608 return usb_device_add_dev(dev
);
2611 int usb_device_del_addr(int bus_num
, int addr
)
2617 if (!used_usb_ports
)
2623 lastp
= &used_usb_ports
;
2624 port
= used_usb_ports
;
2625 while (port
&& port
->dev
->addr
!= addr
) {
2626 lastp
= &port
->next
;
2634 *lastp
= port
->next
;
2635 usb_attach(port
, NULL
);
2636 dev
->handle_destroy(dev
);
2637 port
->next
= free_usb_ports
;
2638 free_usb_ports
= port
;
2642 static int usb_device_del(const char *devname
)
2647 if (strstart(devname
, "host:", &p
))
2648 return usb_host_device_close(p
);
2650 if (!used_usb_ports
)
2653 p
= strchr(devname
, '.');
2656 bus_num
= strtoul(devname
, NULL
, 0);
2657 addr
= strtoul(p
+ 1, NULL
, 0);
2659 return usb_device_del_addr(bus_num
, addr
);
2662 void do_usb_add(Monitor
*mon
, const char *devname
)
2664 usb_device_add(devname
, 1);
2667 void do_usb_del(Monitor
*mon
, const char *devname
)
2669 usb_device_del(devname
);
2672 void usb_info(Monitor
*mon
)
2676 const char *speed_str
;
2679 monitor_printf(mon
, "USB support not enabled\n");
2683 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2687 switch(dev
->speed
) {
2691 case USB_SPEED_FULL
:
2694 case USB_SPEED_HIGH
:
2701 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2702 0, dev
->addr
, speed_str
, dev
->devname
);
2706 /***********************************************************/
2707 /* PCMCIA/Cardbus */
2709 static struct pcmcia_socket_entry_s
{
2710 PCMCIASocket
*socket
;
2711 struct pcmcia_socket_entry_s
*next
;
2712 } *pcmcia_sockets
= 0;
2714 void pcmcia_socket_register(PCMCIASocket
*socket
)
2716 struct pcmcia_socket_entry_s
*entry
;
2718 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2719 entry
->socket
= socket
;
2720 entry
->next
= pcmcia_sockets
;
2721 pcmcia_sockets
= entry
;
2724 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2726 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2728 ptr
= &pcmcia_sockets
;
2729 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2730 if (entry
->socket
== socket
) {
2736 void pcmcia_info(Monitor
*mon
)
2738 struct pcmcia_socket_entry_s
*iter
;
2740 if (!pcmcia_sockets
)
2741 monitor_printf(mon
, "No PCMCIA sockets\n");
2743 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2744 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2745 iter
->socket
->attached
? iter
->socket
->card_string
:
2749 /***********************************************************/
2750 /* register display */
2752 struct DisplayAllocator default_allocator
= {
2753 defaultallocator_create_displaysurface
,
2754 defaultallocator_resize_displaysurface
,
2755 defaultallocator_free_displaysurface
2758 void register_displaystate(DisplayState
*ds
)
2768 DisplayState
*get_displaystate(void)
2770 return display_state
;
2773 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2775 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2776 return ds
->allocator
;
2781 static void dumb_display_init(void)
2783 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2784 ds
->allocator
= &default_allocator
;
2785 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2786 register_displaystate(ds
);
2789 /***********************************************************/
2792 typedef struct IOHandlerRecord
{
2794 IOCanRWHandler
*fd_read_poll
;
2796 IOHandler
*fd_write
;
2799 /* temporary data */
2801 struct IOHandlerRecord
*next
;
2804 static IOHandlerRecord
*first_io_handler
;
2806 /* XXX: fd_read_poll should be suppressed, but an API change is
2807 necessary in the character devices to suppress fd_can_read(). */
2808 int qemu_set_fd_handler2(int fd
,
2809 IOCanRWHandler
*fd_read_poll
,
2811 IOHandler
*fd_write
,
2814 IOHandlerRecord
**pioh
, *ioh
;
2816 if (!fd_read
&& !fd_write
) {
2817 pioh
= &first_io_handler
;
2822 if (ioh
->fd
== fd
) {
2829 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2833 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2834 ioh
->next
= first_io_handler
;
2835 first_io_handler
= ioh
;
2838 ioh
->fd_read_poll
= fd_read_poll
;
2839 ioh
->fd_read
= fd_read
;
2840 ioh
->fd_write
= fd_write
;
2841 ioh
->opaque
= opaque
;
2844 qemu_notify_event();
2848 int qemu_set_fd_handler(int fd
,
2850 IOHandler
*fd_write
,
2853 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2857 /***********************************************************/
2858 /* Polling handling */
2860 typedef struct PollingEntry
{
2863 struct PollingEntry
*next
;
2866 static PollingEntry
*first_polling_entry
;
2868 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2870 PollingEntry
**ppe
, *pe
;
2871 pe
= qemu_mallocz(sizeof(PollingEntry
));
2873 pe
->opaque
= opaque
;
2874 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2879 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2881 PollingEntry
**ppe
, *pe
;
2882 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2884 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2892 /***********************************************************/
2893 /* Wait objects support */
2894 typedef struct WaitObjects
{
2896 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2897 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2898 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2901 static WaitObjects wait_objects
= {0};
2903 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2905 WaitObjects
*w
= &wait_objects
;
2907 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2909 w
->events
[w
->num
] = handle
;
2910 w
->func
[w
->num
] = func
;
2911 w
->opaque
[w
->num
] = opaque
;
2916 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2919 WaitObjects
*w
= &wait_objects
;
2922 for (i
= 0; i
< w
->num
; i
++) {
2923 if (w
->events
[i
] == handle
)
2926 w
->events
[i
] = w
->events
[i
+ 1];
2927 w
->func
[i
] = w
->func
[i
+ 1];
2928 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2936 /***********************************************************/
2937 /* ram save/restore */
2939 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2943 v
= qemu_get_byte(f
);
2946 if (qemu_get_buffer(f
, buf
, len
) != len
)
2950 v
= qemu_get_byte(f
);
2951 memset(buf
, v
, len
);
2957 if (qemu_file_has_error(f
))
2963 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2968 if (qemu_get_be32(f
) != last_ram_offset
)
2970 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2971 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
2973 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2980 #define BDRV_HASH_BLOCK_SIZE 1024
2981 #define IOBUF_SIZE 4096
2982 #define RAM_CBLOCK_MAGIC 0xfabe
2984 typedef struct RamDecompressState
{
2987 uint8_t buf
[IOBUF_SIZE
];
2988 } RamDecompressState
;
2990 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2993 memset(s
, 0, sizeof(*s
));
2995 ret
= inflateInit(&s
->zstream
);
3001 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3005 s
->zstream
.avail_out
= len
;
3006 s
->zstream
.next_out
= buf
;
3007 while (s
->zstream
.avail_out
> 0) {
3008 if (s
->zstream
.avail_in
== 0) {
3009 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3011 clen
= qemu_get_be16(s
->f
);
3012 if (clen
> IOBUF_SIZE
)
3014 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3015 s
->zstream
.avail_in
= clen
;
3016 s
->zstream
.next_in
= s
->buf
;
3018 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3019 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3026 static void ram_decompress_close(RamDecompressState
*s
)
3028 inflateEnd(&s
->zstream
);
3031 #define RAM_SAVE_FLAG_FULL 0x01
3032 #define RAM_SAVE_FLAG_COMPRESS 0x02
3033 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3034 #define RAM_SAVE_FLAG_PAGE 0x08
3035 #define RAM_SAVE_FLAG_EOS 0x10
3037 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3039 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3040 uint32_t *array
= (uint32_t *)page
;
3043 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3044 if (array
[i
] != val
)
3051 static int ram_save_block(QEMUFile
*f
)
3053 static ram_addr_t current_addr
= 0;
3054 ram_addr_t saved_addr
= current_addr
;
3055 ram_addr_t addr
= 0;
3058 while (addr
< last_ram_offset
) {
3059 if (kvm_enabled() && current_addr
== 0) {
3061 r
= kvm_update_dirty_pages_log();
3063 fprintf(stderr
, "%s: update dirty pages log failed %d\n", __FUNCTION__
, r
);
3064 qemu_file_set_error(f
);
3068 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3071 cpu_physical_memory_reset_dirty(current_addr
,
3072 current_addr
+ TARGET_PAGE_SIZE
,
3073 MIGRATION_DIRTY_FLAG
);
3075 p
= qemu_get_ram_ptr(current_addr
);
3077 if (is_dup_page(p
, *p
)) {
3078 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3079 qemu_put_byte(f
, *p
);
3081 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3082 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3088 addr
+= TARGET_PAGE_SIZE
;
3089 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3095 static uint64_t bytes_transferred
= 0;
3097 static ram_addr_t
ram_save_remaining(void)
3100 ram_addr_t count
= 0;
3102 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3103 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3110 uint64_t ram_bytes_remaining(void)
3112 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3115 uint64_t ram_bytes_transferred(void)
3117 return bytes_transferred
;
3120 uint64_t ram_bytes_total(void)
3122 return last_ram_offset
;
3125 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3128 uint64_t bytes_transferred_last
;
3130 uint64_t expected_time
= 0;
3132 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3133 qemu_file_set_error(f
);
3138 /* Make sure all dirty bits are set */
3139 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3140 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3141 cpu_physical_memory_set_dirty(addr
);
3144 /* Enable dirty memory tracking */
3145 cpu_physical_memory_set_dirty_tracking(1);
3147 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3150 bytes_transferred_last
= bytes_transferred
;
3151 bwidth
= get_clock();
3153 while (!qemu_file_rate_limit(f
)) {
3156 ret
= ram_save_block(f
);
3157 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3158 if (ret
== 0) /* no more blocks */
3162 bwidth
= get_clock() - bwidth
;
3163 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3165 /* if we haven't transferred anything this round, force expected_time to a
3166 * a very high value, but without crashing */
3170 /* try transferring iterative blocks of memory */
3174 /* flush all remaining blocks regardless of rate limiting */
3175 while (ram_save_block(f
) != 0) {
3176 bytes_transferred
+= TARGET_PAGE_SIZE
;
3178 cpu_physical_memory_set_dirty_tracking(0);
3181 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3183 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3185 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3188 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3190 RamDecompressState s1
, *s
= &s1
;
3194 if (ram_decompress_open(s
, f
) < 0)
3196 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3197 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3199 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3200 fprintf(stderr
, "Error while reading ram block header\n");
3204 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3205 BDRV_HASH_BLOCK_SIZE
) < 0) {
3206 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3211 printf("Error block header\n");
3215 ram_decompress_close(s
);
3220 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3225 if (version_id
== 1)
3226 return ram_load_v1(f
, opaque
);
3228 if (version_id
== 2) {
3229 if (qemu_get_be32(f
) != last_ram_offset
)
3231 return ram_load_dead(f
, opaque
);
3234 if (version_id
!= 3)
3238 addr
= qemu_get_be64(f
);
3240 flags
= addr
& ~TARGET_PAGE_MASK
;
3241 addr
&= TARGET_PAGE_MASK
;
3243 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3244 if (addr
!= last_ram_offset
)
3248 if (flags
& RAM_SAVE_FLAG_FULL
) {
3249 if (ram_load_dead(f
, opaque
) < 0)
3253 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3254 uint8_t ch
= qemu_get_byte(f
);
3255 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3258 (!kvm_enabled() || kvm_has_sync_mmu())) {
3259 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3262 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3263 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3264 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3269 void qemu_service_io(void)
3271 qemu_notify_event();
3274 /***********************************************************/
3275 /* bottom halves (can be seen as timers which expire ASAP) */
3286 static QEMUBH
*first_bh
= NULL
;
3288 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3291 bh
= qemu_mallocz(sizeof(QEMUBH
));
3293 bh
->opaque
= opaque
;
3294 bh
->next
= first_bh
;
3299 int qemu_bh_poll(void)
3305 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3306 if (!bh
->deleted
&& bh
->scheduled
) {
3315 /* remove deleted bhs */
3329 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3337 void qemu_bh_schedule(QEMUBH
*bh
)
3343 /* stop the currently executing CPU to execute the BH ASAP */
3344 qemu_notify_event();
3347 void qemu_bh_cancel(QEMUBH
*bh
)
3352 void qemu_bh_delete(QEMUBH
*bh
)
3358 static void qemu_bh_update_timeout(int *timeout
)
3362 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3363 if (!bh
->deleted
&& bh
->scheduled
) {
3365 /* idle bottom halves will be polled at least
3367 *timeout
= MIN(10, *timeout
);
3369 /* non-idle bottom halves will be executed
3378 /***********************************************************/
3379 /* machine registration */
3381 static QEMUMachine
*first_machine
= NULL
;
3382 QEMUMachine
*current_machine
= NULL
;
3384 int qemu_register_machine(QEMUMachine
*m
)
3387 pm
= &first_machine
;
3395 static QEMUMachine
*find_machine(const char *name
)
3399 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3400 if (!strcmp(m
->name
, name
))
3402 if (m
->alias
&& !strcmp(m
->alias
, name
))
3408 static QEMUMachine
*find_default_machine(void)
3412 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3413 if (m
->is_default
) {
3420 /***********************************************************/
3421 /* main execution loop */
3423 static void gui_update(void *opaque
)
3425 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3426 DisplayState
*ds
= opaque
;
3427 DisplayChangeListener
*dcl
= ds
->listeners
;
3431 while (dcl
!= NULL
) {
3432 if (dcl
->gui_timer_interval
&&
3433 dcl
->gui_timer_interval
< interval
)
3434 interval
= dcl
->gui_timer_interval
;
3437 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3440 static void nographic_update(void *opaque
)
3442 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3444 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3447 struct vm_change_state_entry
{
3448 VMChangeStateHandler
*cb
;
3450 LIST_ENTRY (vm_change_state_entry
) entries
;
3453 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3455 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3458 VMChangeStateEntry
*e
;
3460 e
= qemu_mallocz(sizeof (*e
));
3464 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3468 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3470 LIST_REMOVE (e
, entries
);
3474 static void vm_state_notify(int running
, int reason
)
3476 VMChangeStateEntry
*e
;
3478 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3479 e
->cb(e
->opaque
, running
, reason
);
3483 static void resume_all_vcpus(void);
3484 static void pause_all_vcpus(void);
3491 vm_state_notify(1, 0);
3492 qemu_rearm_alarm_timer(alarm_timer
);
3497 /* reset/shutdown handler */
3499 typedef struct QEMUResetEntry
{
3500 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3501 QEMUResetHandler
*func
;
3505 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3506 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3507 static int reset_requested
;
3508 static int shutdown_requested
;
3509 static int powerdown_requested
;
3510 static int debug_requested
;
3511 static int vmstop_requested
;
3513 int qemu_no_shutdown(void)
3515 int r
= no_shutdown
;
3520 int qemu_shutdown_requested(void)
3522 int r
= shutdown_requested
;
3523 shutdown_requested
= 0;
3527 int qemu_reset_requested(void)
3529 int r
= reset_requested
;
3530 reset_requested
= 0;
3534 int qemu_powerdown_requested(void)
3536 int r
= powerdown_requested
;
3537 powerdown_requested
= 0;
3541 static int qemu_debug_requested(void)
3543 int r
= debug_requested
;
3544 debug_requested
= 0;
3548 static int qemu_vmstop_requested(void)
3550 int r
= vmstop_requested
;
3551 vmstop_requested
= 0;
3555 static void do_vm_stop(int reason
)
3558 cpu_disable_ticks();
3561 vm_state_notify(0, reason
);
3565 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3567 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3570 re
->opaque
= opaque
;
3571 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3574 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3578 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3579 if (re
->func
== func
&& re
->opaque
== opaque
) {
3580 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3587 void qemu_system_reset(void)
3589 QEMUResetEntry
*re
, *nre
;
3591 /* reset all devices */
3592 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3593 re
->func(re
->opaque
);
3597 void qemu_system_reset_request(void)
3600 shutdown_requested
= 1;
3602 reset_requested
= 1;
3604 if (cpu_single_env
) {
3605 cpu_single_env
->stopped
= 1;
3607 qemu_notify_event();
3610 void qemu_system_shutdown_request(void)
3612 shutdown_requested
= 1;
3613 qemu_notify_event();
3616 void qemu_system_powerdown_request(void)
3618 powerdown_requested
= 1;
3619 qemu_notify_event();
3622 #ifdef CONFIG_IOTHREAD
3623 static void qemu_system_vmstop_request(int reason
)
3625 vmstop_requested
= reason
;
3626 qemu_notify_event();
3631 static int io_thread_fd
= -1;
3633 static void qemu_event_increment(void)
3635 static const char byte
= 0;
3637 if (io_thread_fd
== -1)
3640 write(io_thread_fd
, &byte
, sizeof(byte
));
3643 static void qemu_event_read(void *opaque
)
3645 int fd
= (unsigned long)opaque
;
3648 /* Drain the notify pipe */
3651 len
= read(fd
, buffer
, sizeof(buffer
));
3652 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3655 static int qemu_event_init(void)
3664 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3668 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3672 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3673 (void *)(unsigned long)fds
[0]);
3675 io_thread_fd
= fds
[1];
3684 HANDLE qemu_event_handle
;
3686 static void dummy_event_handler(void *opaque
)
3690 static int qemu_event_init(void)
3692 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3693 if (!qemu_event_handle
) {
3694 perror("Failed CreateEvent");
3697 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3701 static void qemu_event_increment(void)
3703 SetEvent(qemu_event_handle
);
3707 static int cpu_can_run(CPUState
*env
)
3716 #ifndef CONFIG_IOTHREAD
3717 static int qemu_init_main_loop(void)
3719 return qemu_event_init();
3722 void qemu_init_vcpu(void *_env
)
3724 CPUState
*env
= _env
;
3731 int qemu_cpu_self(void *env
)
3736 static void resume_all_vcpus(void)
3740 static void pause_all_vcpus(void)
3744 void qemu_cpu_kick(void *env
)
3749 void qemu_notify_event(void)
3751 CPUState
*env
= cpu_single_env
;
3753 if (kvm_enabled()) {
3754 qemu_kvm_notify_work();
3760 if (env
->kqemu_enabled
)
3761 kqemu_cpu_interrupt(env
);
3767 #define qemu_mutex_lock_iothread() do { } while (0)
3768 #define qemu_mutex_unlock_iothread() do { } while (0)
3771 void vm_stop(int reason
)
3776 #else /* CONFIG_IOTHREAD */
3778 #include "qemu-thread.h"
3780 QemuMutex qemu_global_mutex
;
3781 static QemuMutex qemu_fair_mutex
;
3783 static QemuThread io_thread
;
3785 static QemuThread
*tcg_cpu_thread
;
3786 static QemuCond
*tcg_halt_cond
;
3788 static int qemu_system_ready
;
3790 static QemuCond qemu_cpu_cond
;
3792 static QemuCond qemu_system_cond
;
3793 static QemuCond qemu_pause_cond
;
3795 static void block_io_signals(void);
3796 static void unblock_io_signals(void);
3797 static int tcg_has_work(void);
3799 static int qemu_init_main_loop(void)
3803 ret
= qemu_event_init();
3807 qemu_cond_init(&qemu_pause_cond
);
3808 qemu_mutex_init(&qemu_fair_mutex
);
3809 qemu_mutex_init(&qemu_global_mutex
);
3810 qemu_mutex_lock(&qemu_global_mutex
);
3812 unblock_io_signals();
3813 qemu_thread_self(&io_thread
);
3818 static void qemu_wait_io_event(CPUState
*env
)
3820 while (!tcg_has_work())
3821 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3823 qemu_mutex_unlock(&qemu_global_mutex
);
3826 * Users of qemu_global_mutex can be starved, having no chance
3827 * to acquire it since this path will get to it first.
3828 * So use another lock to provide fairness.
3830 qemu_mutex_lock(&qemu_fair_mutex
);
3831 qemu_mutex_unlock(&qemu_fair_mutex
);
3833 qemu_mutex_lock(&qemu_global_mutex
);
3837 qemu_cond_signal(&qemu_pause_cond
);
3841 static int qemu_cpu_exec(CPUState
*env
);
3843 static void *kvm_cpu_thread_fn(void *arg
)
3845 CPUState
*env
= arg
;
3848 qemu_thread_self(env
->thread
);
3850 /* signal CPU creation */
3851 qemu_mutex_lock(&qemu_global_mutex
);
3853 qemu_cond_signal(&qemu_cpu_cond
);
3855 /* and wait for machine initialization */
3856 while (!qemu_system_ready
)
3857 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3860 if (cpu_can_run(env
))
3862 qemu_wait_io_event(env
);
3868 static void tcg_cpu_exec(void);
3870 static void *tcg_cpu_thread_fn(void *arg
)
3872 CPUState
*env
= arg
;
3875 qemu_thread_self(env
->thread
);
3877 /* signal CPU creation */
3878 qemu_mutex_lock(&qemu_global_mutex
);
3879 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3881 qemu_cond_signal(&qemu_cpu_cond
);
3883 /* and wait for machine initialization */
3884 while (!qemu_system_ready
)
3885 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3889 qemu_wait_io_event(cur_cpu
);
3895 void qemu_cpu_kick(void *_env
)
3897 CPUState
*env
= _env
;
3898 qemu_cond_broadcast(env
->halt_cond
);
3900 qemu_thread_signal(env
->thread
, SIGUSR1
);
3903 int qemu_cpu_self(void *env
)
3905 return (cpu_single_env
!= NULL
);
3908 static void cpu_signal(int sig
)
3911 cpu_exit(cpu_single_env
);
3914 static void block_io_signals(void)
3917 struct sigaction sigact
;
3920 sigaddset(&set
, SIGUSR2
);
3921 sigaddset(&set
, SIGIO
);
3922 sigaddset(&set
, SIGALRM
);
3923 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3926 sigaddset(&set
, SIGUSR1
);
3927 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3929 memset(&sigact
, 0, sizeof(sigact
));
3930 sigact
.sa_handler
= cpu_signal
;
3931 sigaction(SIGUSR1
, &sigact
, NULL
);
3934 static void unblock_io_signals(void)
3939 sigaddset(&set
, SIGUSR2
);
3940 sigaddset(&set
, SIGIO
);
3941 sigaddset(&set
, SIGALRM
);
3942 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3945 sigaddset(&set
, SIGUSR1
);
3946 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3949 static void qemu_signal_lock(unsigned int msecs
)
3951 qemu_mutex_lock(&qemu_fair_mutex
);
3953 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3954 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3955 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3958 qemu_mutex_unlock(&qemu_fair_mutex
);
3961 static void qemu_mutex_lock_iothread(void)
3963 if (kvm_enabled()) {
3964 qemu_mutex_lock(&qemu_fair_mutex
);
3965 qemu_mutex_lock(&qemu_global_mutex
);
3966 qemu_mutex_unlock(&qemu_fair_mutex
);
3968 qemu_signal_lock(100);
3971 static void qemu_mutex_unlock_iothread(void)
3973 qemu_mutex_unlock(&qemu_global_mutex
);
3976 static int all_vcpus_paused(void)
3978 CPUState
*penv
= first_cpu
;
3983 penv
= (CPUState
*)penv
->next_cpu
;
3989 static void pause_all_vcpus(void)
3991 CPUState
*penv
= first_cpu
;
3995 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3996 qemu_cpu_kick(penv
);
3997 penv
= (CPUState
*)penv
->next_cpu
;
4000 while (!all_vcpus_paused()) {
4001 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
4004 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4005 penv
= (CPUState
*)penv
->next_cpu
;
4010 static void resume_all_vcpus(void)
4012 CPUState
*penv
= first_cpu
;
4017 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4018 qemu_cpu_kick(penv
);
4019 penv
= (CPUState
*)penv
->next_cpu
;
4023 static void tcg_init_vcpu(void *_env
)
4025 CPUState
*env
= _env
;
4026 /* share a single thread for all cpus with TCG */
4027 if (!tcg_cpu_thread
) {
4028 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
4029 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
4030 qemu_cond_init(env
->halt_cond
);
4031 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
4032 while (env
->created
== 0)
4033 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
4034 tcg_cpu_thread
= env
->thread
;
4035 tcg_halt_cond
= env
->halt_cond
;
4037 env
->thread
= tcg_cpu_thread
;
4038 env
->halt_cond
= tcg_halt_cond
;
4042 static void kvm_start_vcpu(CPUState
*env
)
4045 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
4046 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
4047 qemu_cond_init(env
->halt_cond
);
4048 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
4049 while (env
->created
== 0)
4050 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
4053 void qemu_init_vcpu(void *_env
)
4055 CPUState
*env
= _env
;
4058 kvm_start_vcpu(env
);
4063 void qemu_notify_event(void)
4065 qemu_event_increment();
4068 void vm_stop(int reason
)
4071 qemu_thread_self(&me
);
4073 if (!qemu_thread_equal(&me
, &io_thread
)) {
4074 qemu_system_vmstop_request(reason
);
4076 * FIXME: should not return to device code in case
4077 * vm_stop() has been requested.
4079 if (cpu_single_env
) {
4080 cpu_exit(cpu_single_env
);
4081 cpu_single_env
->stop
= 1;
4092 static void host_main_loop_wait(int *timeout
)
4098 /* XXX: need to suppress polling by better using win32 events */
4100 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4101 ret
|= pe
->func(pe
->opaque
);
4105 WaitObjects
*w
= &wait_objects
;
4107 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4108 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4109 if (w
->func
[ret
- WAIT_OBJECT_0
])
4110 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4112 /* Check for additional signaled events */
4113 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4115 /* Check if event is signaled */
4116 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4117 if(ret2
== WAIT_OBJECT_0
) {
4119 w
->func
[i
](w
->opaque
[i
]);
4120 } else if (ret2
== WAIT_TIMEOUT
) {
4122 err
= GetLastError();
4123 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4126 } else if (ret
== WAIT_TIMEOUT
) {
4128 err
= GetLastError();
4129 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4136 static void host_main_loop_wait(int *timeout
)
4141 void main_loop_wait(int timeout
)
4143 IOHandlerRecord
*ioh
;
4144 fd_set rfds
, wfds
, xfds
;
4148 qemu_bh_update_timeout(&timeout
);
4150 host_main_loop_wait(&timeout
);
4152 /* poll any events */
4153 /* XXX: separate device handlers from system ones */
4158 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4162 (!ioh
->fd_read_poll
||
4163 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4164 FD_SET(ioh
->fd
, &rfds
);
4168 if (ioh
->fd_write
) {
4169 FD_SET(ioh
->fd
, &wfds
);
4175 tv
.tv_sec
= timeout
/ 1000;
4176 tv
.tv_usec
= (timeout
% 1000) * 1000;
4178 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4180 qemu_mutex_unlock_iothread();
4181 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4182 qemu_mutex_lock_iothread();
4184 IOHandlerRecord
**pioh
;
4186 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4187 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4188 ioh
->fd_read(ioh
->opaque
);
4189 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
4190 FD_CLR(ioh
->fd
, &rfds
);
4192 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4193 ioh
->fd_write(ioh
->opaque
);
4197 /* remove deleted IO handlers */
4198 pioh
= &first_io_handler
;
4209 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4211 /* rearm timer, if not periodic */
4212 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4213 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4214 qemu_rearm_alarm_timer(alarm_timer
);
4217 /* vm time timers */
4219 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4220 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4221 qemu_get_clock(vm_clock
));
4224 /* real time timers */
4225 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4226 qemu_get_clock(rt_clock
));
4228 /* Check bottom-halves last in case any of the earlier events triggered
4234 static int qemu_cpu_exec(CPUState
*env
)
4237 #ifdef CONFIG_PROFILER
4241 #ifdef CONFIG_PROFILER
4242 ti
= profile_getclock();
4247 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4248 env
->icount_decr
.u16
.low
= 0;
4249 env
->icount_extra
= 0;
4250 count
= qemu_next_deadline();
4251 count
= (count
+ (1 << icount_time_shift
) - 1)
4252 >> icount_time_shift
;
4253 qemu_icount
+= count
;
4254 decr
= (count
> 0xffff) ? 0xffff : count
;
4256 env
->icount_decr
.u16
.low
= decr
;
4257 env
->icount_extra
= count
;
4259 ret
= cpu_exec(env
);
4260 #ifdef CONFIG_PROFILER
4261 qemu_time
+= profile_getclock() - ti
;
4264 /* Fold pending instructions back into the
4265 instruction counter, and clear the interrupt flag. */
4266 qemu_icount
-= (env
->icount_decr
.u16
.low
4267 + env
->icount_extra
);
4268 env
->icount_decr
.u32
= 0;
4269 env
->icount_extra
= 0;
4274 static void tcg_cpu_exec(void)
4278 if (next_cpu
== NULL
)
4279 next_cpu
= first_cpu
;
4280 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4281 CPUState
*env
= cur_cpu
= next_cpu
;
4285 if (timer_alarm_pending
) {
4286 timer_alarm_pending
= 0;
4289 if (cpu_can_run(env
))
4290 ret
= qemu_cpu_exec(env
);
4291 if (ret
== EXCP_DEBUG
) {
4292 gdb_set_stop_cpu(env
);
4293 debug_requested
= 1;
4299 static int cpu_has_work(CPUState
*env
)
4307 if (qemu_cpu_has_work(env
))
4312 static int tcg_has_work(void)
4316 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4317 if (cpu_has_work(env
))
4322 static int qemu_calculate_timeout(void)
4324 #ifndef CONFIG_IOTHREAD
4329 else if (tcg_has_work())
4331 else if (!use_icount
)
4334 /* XXX: use timeout computed from timers */
4337 /* Advance virtual time to the next event. */
4338 if (use_icount
== 1) {
4339 /* When not using an adaptive execution frequency
4340 we tend to get badly out of sync with real time,
4341 so just delay for a reasonable amount of time. */
4344 delta
= cpu_get_icount() - cpu_get_clock();
4347 /* If virtual time is ahead of real time then just
4349 timeout
= (delta
/ 1000000) + 1;
4351 /* Wait for either IO to occur or the next
4353 add
= qemu_next_deadline();
4354 /* We advance the timer before checking for IO.
4355 Limit the amount we advance so that early IO
4356 activity won't get the guest too far ahead. */
4360 add
= (add
+ (1 << icount_time_shift
) - 1)
4361 >> icount_time_shift
;
4363 timeout
= delta
/ 1000000;
4370 #else /* CONFIG_IOTHREAD */
4375 static int vm_can_run(void)
4377 if (powerdown_requested
)
4379 if (reset_requested
)
4381 if (shutdown_requested
)
4383 if (debug_requested
)
4388 static void main_loop(void)
4392 if (kvm_enabled()) {
4394 cpu_disable_ticks();
4398 #ifdef CONFIG_IOTHREAD
4399 qemu_system_ready
= 1;
4400 qemu_cond_broadcast(&qemu_system_cond
);
4405 #ifdef CONFIG_PROFILER
4408 #ifndef CONFIG_IOTHREAD
4411 #ifdef CONFIG_PROFILER
4412 ti
= profile_getclock();
4414 main_loop_wait(qemu_calculate_timeout());
4415 #ifdef CONFIG_PROFILER
4416 dev_time
+= profile_getclock() - ti
;
4418 } while (vm_can_run());
4420 if (qemu_debug_requested())
4421 vm_stop(EXCP_DEBUG
);
4422 if (qemu_shutdown_requested()) {
4429 if (qemu_reset_requested()) {
4431 qemu_system_reset();
4434 if (qemu_powerdown_requested())
4435 qemu_system_powerdown();
4436 if ((r
= qemu_vmstop_requested()))
4442 static void version(void)
4444 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4447 static void help(int exitcode
)
4450 printf("usage: %s [options] [disk_image]\n"
4452 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4454 #define DEF(option, opt_arg, opt_enum, opt_help) \
4456 #define DEFHEADING(text) stringify(text) "\n"
4457 #include "qemu-options.h"
4462 "During emulation, the following keys are useful:\n"
4463 "ctrl-alt-f toggle full screen\n"
4464 "ctrl-alt-n switch to virtual console 'n'\n"
4465 "ctrl-alt toggle mouse and keyboard grab\n"
4467 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4472 DEFAULT_NETWORK_SCRIPT
,
4473 DEFAULT_NETWORK_DOWN_SCRIPT
,
4475 DEFAULT_GDBSTUB_PORT
,
4480 #define HAS_ARG 0x0001
4483 #define DEF(option, opt_arg, opt_enum, opt_help) \
4485 #define DEFHEADING(text)
4486 #include "qemu-options.h"
4492 typedef struct QEMUOption
{
4498 static const QEMUOption qemu_options
[] = {
4499 { "h", 0, QEMU_OPTION_h
},
4500 #define DEF(option, opt_arg, opt_enum, opt_help) \
4501 { option, opt_arg, opt_enum },
4502 #define DEFHEADING(text)
4503 #include "qemu-options.h"
4511 struct soundhw soundhw
[] = {
4512 #ifdef HAS_AUDIO_CHOICE
4513 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4519 { .init_isa
= pcspk_audio_init
}
4526 "Creative Sound Blaster 16",
4529 { .init_isa
= SB16_init
}
4533 #ifdef CONFIG_CS4231A
4539 { .init_isa
= cs4231a_init
}
4547 "Yamaha YMF262 (OPL3)",
4549 "Yamaha YM3812 (OPL2)",
4553 { .init_isa
= Adlib_init
}
4560 "Gravis Ultrasound GF1",
4563 { .init_isa
= GUS_init
}
4570 "Intel 82801AA AC97 Audio",
4573 { .init_pci
= ac97_init
}
4577 #ifdef CONFIG_ES1370
4580 "ENSONIQ AudioPCI ES1370",
4583 { .init_pci
= es1370_init
}
4587 #endif /* HAS_AUDIO_CHOICE */
4589 { NULL
, NULL
, 0, 0, { NULL
} }
4592 static void select_soundhw (const char *optarg
)
4596 if (*optarg
== '?') {
4599 printf ("Valid sound card names (comma separated):\n");
4600 for (c
= soundhw
; c
->name
; ++c
) {
4601 printf ("%-11s %s\n", c
->name
, c
->descr
);
4603 printf ("\n-soundhw all will enable all of the above\n");
4604 exit (*optarg
!= '?');
4612 if (!strcmp (optarg
, "all")) {
4613 for (c
= soundhw
; c
->name
; ++c
) {
4621 e
= strchr (p
, ',');
4622 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4624 for (c
= soundhw
; c
->name
; ++c
) {
4625 if (!strncmp (c
->name
, p
, l
)) {
4634 "Unknown sound card name (too big to show)\n");
4637 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4642 p
+= l
+ (e
!= NULL
);
4646 goto show_valid_cards
;
4651 static void select_vgahw (const char *p
)
4655 vga_interface_type
= VGA_NONE
;
4656 if (strstart(p
, "std", &opts
)) {
4657 vga_interface_type
= VGA_STD
;
4658 } else if (strstart(p
, "cirrus", &opts
)) {
4659 vga_interface_type
= VGA_CIRRUS
;
4660 } else if (strstart(p
, "vmware", &opts
)) {
4661 vga_interface_type
= VGA_VMWARE
;
4662 } else if (strstart(p
, "xenfb", &opts
)) {
4663 vga_interface_type
= VGA_XENFB
;
4664 } else if (!strstart(p
, "none", &opts
)) {
4666 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4670 const char *nextopt
;
4672 if (strstart(opts
, ",retrace=", &nextopt
)) {
4674 if (strstart(opts
, "dumb", &nextopt
))
4675 vga_retrace_method
= VGA_RETRACE_DUMB
;
4676 else if (strstart(opts
, "precise", &nextopt
))
4677 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4678 else goto invalid_vga
;
4679 } else goto invalid_vga
;
4685 static int balloon_parse(const char *arg
)
4690 if (!strcmp(arg
, "none")) {
4692 } else if (!strncmp(arg
, "virtio", 6)) {
4694 if (arg
[6] == ',') {
4696 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4697 virtio_balloon_devaddr
= strdup(buf
);
4708 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4710 exit(STATUS_CONTROL_C_EXIT
);
4715 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4719 if(strlen(str
) != 36)
4722 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4723 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4724 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4730 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4736 #define MAX_NET_CLIENTS 32
4740 static void termsig_handler(int signal
)
4742 qemu_system_shutdown_request();
4745 static void sigchld_handler(int signal
)
4747 waitpid(-1, NULL
, WNOHANG
);
4750 static void sighandler_setup(void)
4752 struct sigaction act
;
4754 memset(&act
, 0, sizeof(act
));
4755 act
.sa_handler
= termsig_handler
;
4756 sigaction(SIGINT
, &act
, NULL
);
4757 sigaction(SIGHUP
, &act
, NULL
);
4758 sigaction(SIGTERM
, &act
, NULL
);
4760 act
.sa_handler
= sigchld_handler
;
4761 act
.sa_flags
= SA_NOCLDSTOP
;
4762 sigaction(SIGCHLD
, &act
, NULL
);
4768 /* Look for support files in the same directory as the executable. */
4769 static char *find_datadir(const char *argv0
)
4775 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4782 while (p
!= buf
&& *p
!= '\\')
4785 if (access(buf
, R_OK
) == 0) {
4786 return qemu_strdup(buf
);
4792 /* Find a likely location for support files using the location of the binary.
4793 For installed binaries this will be "$bindir/../share/qemu". When
4794 running from the build tree this will be "$bindir/../pc-bios". */
4795 #define SHARE_SUFFIX "/share/qemu"
4796 #define BUILD_SUFFIX "/pc-bios"
4797 static char *find_datadir(const char *argv0
)
4807 #if defined(__linux__)
4810 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4816 #elif defined(__FreeBSD__)
4819 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4826 /* If we don't have any way of figuring out the actual executable
4827 location then try argv[0]. */
4832 p
= realpath(argv0
, p
);
4840 max_len
= strlen(dir
) +
4841 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4842 res
= qemu_mallocz(max_len
);
4843 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4844 if (access(res
, R_OK
)) {
4845 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4846 if (access(res
, R_OK
)) {
4860 char *qemu_find_file(int type
, const char *name
)
4866 /* If name contains path separators then try it as a straight path. */
4867 if ((strchr(name
, '/') || strchr(name
, '\\'))
4868 && access(name
, R_OK
) == 0) {
4869 return strdup(name
);
4872 case QEMU_FILE_TYPE_BIOS
:
4875 case QEMU_FILE_TYPE_KEYMAP
:
4876 subdir
= "keymaps/";
4881 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4882 buf
= qemu_mallocz(len
);
4883 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4884 if (access(buf
, R_OK
)) {
4891 int main(int argc
, char **argv
, char **envp
)
4893 const char *gdbstub_dev
= NULL
;
4894 uint32_t boot_devices_bitmap
= 0;
4896 int snapshot
, linux_boot
, net_boot
;
4897 const char *initrd_filename
;
4898 const char *kernel_filename
, *kernel_cmdline
;
4899 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4901 DisplayChangeListener
*dcl
;
4902 int cyls
, heads
, secs
, translation
;
4903 const char *net_clients
[MAX_NET_CLIENTS
];
4905 const char *bt_opts
[MAX_BT_CMDLINE
];
4909 const char *r
, *optarg
;
4910 CharDriverState
*monitor_hd
= NULL
;
4911 const char *monitor_device
;
4912 const char *serial_devices
[MAX_SERIAL_PORTS
];
4913 int serial_device_index
;
4914 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4915 int parallel_device_index
;
4916 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4917 int virtio_console_index
;
4918 const char *loadvm
= NULL
;
4919 QEMUMachine
*machine
;
4920 const char *cpu_model
;
4921 const char *usb_devices
[MAX_USB_CMDLINE
];
4922 int usb_devices_index
;
4927 const char *pid_file
= NULL
;
4928 const char *incoming
= NULL
;
4931 struct passwd
*pwd
= NULL
;
4932 const char *chroot_dir
= NULL
;
4933 const char *run_as
= NULL
;
4936 int show_vnc_port
= 0;
4938 qemu_cache_utils_init(envp
);
4940 LIST_INIT (&vm_change_state_head
);
4943 struct sigaction act
;
4944 sigfillset(&act
.sa_mask
);
4946 act
.sa_handler
= SIG_IGN
;
4947 sigaction(SIGPIPE
, &act
, NULL
);
4950 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4951 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4952 QEMU to run on a single CPU */
4957 h
= GetCurrentProcess();
4958 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4959 for(i
= 0; i
< 32; i
++) {
4960 if (mask
& (1 << i
))
4965 SetProcessAffinityMask(h
, mask
);
4971 module_call_init(MODULE_INIT_MACHINE
);
4972 machine
= find_default_machine();
4974 initrd_filename
= NULL
;
4977 kernel_filename
= NULL
;
4978 kernel_cmdline
= "";
4979 cyls
= heads
= secs
= 0;
4980 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4981 monitor_device
= "vc:80Cx24C";
4983 serial_devices
[0] = "vc:80Cx24C";
4984 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4985 serial_devices
[i
] = NULL
;
4986 serial_device_index
= 0;
4988 parallel_devices
[0] = "vc:80Cx24C";
4989 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4990 parallel_devices
[i
] = NULL
;
4991 parallel_device_index
= 0;
4993 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4994 virtio_consoles
[i
] = NULL
;
4995 virtio_console_index
= 0;
4997 for (i
= 0; i
< MAX_NODES
; i
++) {
4999 node_cpumask
[i
] = 0;
5002 usb_devices_index
= 0;
5003 assigned_devices_index
= 0;
5017 register_watchdogs();
5025 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
5027 const QEMUOption
*popt
;
5030 /* Treat --foo the same as -foo. */
5033 popt
= qemu_options
;
5036 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5040 if (!strcmp(popt
->name
, r
+ 1))
5044 if (popt
->flags
& HAS_ARG
) {
5045 if (optind
>= argc
) {
5046 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5050 optarg
= argv
[optind
++];
5055 switch(popt
->index
) {
5057 machine
= find_machine(optarg
);
5060 printf("Supported machines are:\n");
5061 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5063 printf("%-10s %s (alias of %s)\n",
5064 m
->alias
, m
->desc
, m
->name
);
5065 printf("%-10s %s%s\n",
5067 m
->is_default
? " (default)" : "");
5069 exit(*optarg
!= '?');
5072 case QEMU_OPTION_cpu
:
5073 /* hw initialization will check this */
5074 if (*optarg
== '?') {
5075 /* XXX: implement xxx_cpu_list for targets that still miss it */
5076 #if defined(cpu_list)
5077 cpu_list(stdout
, &fprintf
);
5084 case QEMU_OPTION_initrd
:
5085 initrd_filename
= optarg
;
5087 case QEMU_OPTION_hda
:
5089 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5091 hda_index
= drive_add(optarg
, HD_ALIAS
5092 ",cyls=%d,heads=%d,secs=%d%s",
5093 0, cyls
, heads
, secs
,
5094 translation
== BIOS_ATA_TRANSLATION_LBA
?
5096 translation
== BIOS_ATA_TRANSLATION_NONE
?
5097 ",trans=none" : "");
5099 case QEMU_OPTION_hdb
:
5100 case QEMU_OPTION_hdc
:
5101 case QEMU_OPTION_hdd
:
5102 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5104 case QEMU_OPTION_drive
:
5105 drive_add(NULL
, "%s", optarg
);
5107 case QEMU_OPTION_mtdblock
:
5108 drive_add(optarg
, MTD_ALIAS
);
5110 case QEMU_OPTION_sd
:
5111 drive_add(optarg
, SD_ALIAS
);
5113 case QEMU_OPTION_pflash
:
5114 drive_add(optarg
, PFLASH_ALIAS
);
5116 case QEMU_OPTION_snapshot
:
5119 case QEMU_OPTION_hdachs
:
5123 cyls
= strtol(p
, (char **)&p
, 0);
5124 if (cyls
< 1 || cyls
> 16383)
5129 heads
= strtol(p
, (char **)&p
, 0);
5130 if (heads
< 1 || heads
> 16)
5135 secs
= strtol(p
, (char **)&p
, 0);
5136 if (secs
< 1 || secs
> 63)
5140 if (!strcmp(p
, "none"))
5141 translation
= BIOS_ATA_TRANSLATION_NONE
;
5142 else if (!strcmp(p
, "lba"))
5143 translation
= BIOS_ATA_TRANSLATION_LBA
;
5144 else if (!strcmp(p
, "auto"))
5145 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5148 } else if (*p
!= '\0') {
5150 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5153 if (hda_index
!= -1)
5154 snprintf(drives_opt
[hda_index
].opt
,
5155 sizeof(drives_opt
[hda_index
].opt
),
5156 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5157 0, cyls
, heads
, secs
,
5158 translation
== BIOS_ATA_TRANSLATION_LBA
?
5160 translation
== BIOS_ATA_TRANSLATION_NONE
?
5161 ",trans=none" : "");
5164 case QEMU_OPTION_numa
:
5165 if (nb_numa_nodes
>= MAX_NODES
) {
5166 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5171 case QEMU_OPTION_nographic
:
5172 display_type
= DT_NOGRAPHIC
;
5174 #ifdef CONFIG_CURSES
5175 case QEMU_OPTION_curses
:
5176 display_type
= DT_CURSES
;
5179 case QEMU_OPTION_portrait
:
5182 case QEMU_OPTION_kernel
:
5183 kernel_filename
= optarg
;
5185 case QEMU_OPTION_append
:
5186 kernel_cmdline
= optarg
;
5188 case QEMU_OPTION_cdrom
:
5189 drive_add(optarg
, CDROM_ALIAS
);
5191 case QEMU_OPTION_boot
:
5193 static const char * const params
[] = {
5194 "order", "once", "menu", NULL
5196 char buf
[sizeof(boot_devices
)];
5197 char *standard_boot_devices
;
5200 if (!strchr(optarg
, '=')) {
5202 pstrcpy(buf
, sizeof(buf
), optarg
);
5203 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5205 "qemu: unknown boot parameter '%s' in '%s'\n",
5211 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5212 boot_devices_bitmap
= parse_bootdevices(buf
);
5213 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5216 if (get_param_value(buf
, sizeof(buf
),
5218 boot_devices_bitmap
|= parse_bootdevices(buf
);
5219 standard_boot_devices
= qemu_strdup(boot_devices
);
5220 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5221 qemu_register_reset(restore_boot_devices
,
5222 standard_boot_devices
);
5224 if (get_param_value(buf
, sizeof(buf
),
5226 if (!strcmp(buf
, "on")) {
5228 } else if (!strcmp(buf
, "off")) {
5232 "qemu: invalid option value '%s'\n",
5240 case QEMU_OPTION_fda
:
5241 case QEMU_OPTION_fdb
:
5242 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5245 case QEMU_OPTION_no_fd_bootchk
:
5249 case QEMU_OPTION_net
:
5250 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5251 fprintf(stderr
, "qemu: too many network clients\n");
5254 net_clients
[nb_net_clients
] = optarg
;
5258 case QEMU_OPTION_tftp
:
5259 legacy_tftp_prefix
= optarg
;
5261 case QEMU_OPTION_bootp
:
5262 legacy_bootp_filename
= optarg
;
5265 case QEMU_OPTION_smb
:
5266 net_slirp_smb(optarg
);
5269 case QEMU_OPTION_redir
:
5270 net_slirp_redir(optarg
);
5273 case QEMU_OPTION_bt
:
5274 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5275 fprintf(stderr
, "qemu: too many bluetooth options\n");
5278 bt_opts
[nb_bt_opts
++] = optarg
;
5281 case QEMU_OPTION_audio_help
:
5285 case QEMU_OPTION_soundhw
:
5286 select_soundhw (optarg
);
5292 case QEMU_OPTION_version
:
5296 case QEMU_OPTION_m
: {
5300 value
= strtoul(optarg
, &ptr
, 10);
5302 case 0: case 'M': case 'm':
5309 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5313 /* On 32-bit hosts, QEMU is limited by virtual address space */
5314 if (value
> (2047 << 20)
5315 #ifndef CONFIG_KQEMU
5316 && HOST_LONG_BITS
== 32
5319 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5322 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5323 fprintf(stderr
, "qemu: ram size too large\n");
5332 const CPULogItem
*item
;
5334 mask
= cpu_str_to_log_mask(optarg
);
5336 printf("Log items (comma separated):\n");
5337 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5338 printf("%-10s %s\n", item
->name
, item
->help
);
5346 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5348 case QEMU_OPTION_gdb
:
5349 gdbstub_dev
= optarg
;
5354 case QEMU_OPTION_bios
:
5357 case QEMU_OPTION_singlestep
:
5365 keyboard_layout
= optarg
;
5368 case QEMU_OPTION_localtime
:
5371 case QEMU_OPTION_vga
:
5372 select_vgahw (optarg
);
5374 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5380 w
= strtol(p
, (char **)&p
, 10);
5383 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5389 h
= strtol(p
, (char **)&p
, 10);
5394 depth
= strtol(p
, (char **)&p
, 10);
5395 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5396 depth
!= 24 && depth
!= 32)
5398 } else if (*p
== '\0') {
5399 depth
= graphic_depth
;
5406 graphic_depth
= depth
;
5410 case QEMU_OPTION_echr
:
5413 term_escape_char
= strtol(optarg
, &r
, 0);
5415 printf("Bad argument to echr\n");
5418 case QEMU_OPTION_monitor
:
5419 monitor_device
= optarg
;
5421 case QEMU_OPTION_serial
:
5422 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5423 fprintf(stderr
, "qemu: too many serial ports\n");
5426 serial_devices
[serial_device_index
] = optarg
;
5427 serial_device_index
++;
5429 case QEMU_OPTION_watchdog
:
5430 i
= select_watchdog(optarg
);
5432 exit (i
== 1 ? 1 : 0);
5434 case QEMU_OPTION_watchdog_action
:
5435 if (select_watchdog_action(optarg
) == -1) {
5436 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5440 case QEMU_OPTION_virtiocon
:
5441 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5442 fprintf(stderr
, "qemu: too many virtio consoles\n");
5445 virtio_consoles
[virtio_console_index
] = optarg
;
5446 virtio_console_index
++;
5448 case QEMU_OPTION_parallel
:
5449 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5450 fprintf(stderr
, "qemu: too many parallel ports\n");
5453 parallel_devices
[parallel_device_index
] = optarg
;
5454 parallel_device_index
++;
5456 case QEMU_OPTION_loadvm
:
5459 case QEMU_OPTION_full_screen
:
5463 case QEMU_OPTION_no_frame
:
5466 case QEMU_OPTION_alt_grab
:
5469 case QEMU_OPTION_no_quit
:
5472 case QEMU_OPTION_sdl
:
5473 display_type
= DT_SDL
;
5476 case QEMU_OPTION_pidfile
:
5480 case QEMU_OPTION_win2k_hack
:
5481 win2k_install_hack
= 1;
5483 case QEMU_OPTION_rtc_td_hack
:
5486 case QEMU_OPTION_acpitable
:
5487 if(acpi_table_add(optarg
) < 0) {
5488 fprintf(stderr
, "Wrong acpi table provided\n");
5492 case QEMU_OPTION_smbios
:
5493 if(smbios_entry_add(optarg
) < 0) {
5494 fprintf(stderr
, "Wrong smbios provided\n");
5500 case QEMU_OPTION_enable_kqemu
:
5503 case QEMU_OPTION_kernel_kqemu
:
5509 case QEMU_OPTION_enable_kvm
:
5516 case QEMU_OPTION_no_kvm
:
5519 case QEMU_OPTION_no_kvm_irqchip
: {
5524 case QEMU_OPTION_no_kvm_pit
: {
5528 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5529 kvm_pit_reinject
= 0;
5532 case QEMU_OPTION_enable_nesting
: {
5536 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5537 case QEMU_OPTION_pcidevice
:
5538 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5539 fprintf(stderr
, "Too many assigned devices\n");
5542 assigned_devices
[assigned_devices_index
] = optarg
;
5543 assigned_devices_index
++;
5547 case QEMU_OPTION_usb
:
5550 case QEMU_OPTION_usbdevice
:
5552 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5553 fprintf(stderr
, "Too many USB devices\n");
5556 usb_devices
[usb_devices_index
] = optarg
;
5557 usb_devices_index
++;
5559 case QEMU_OPTION_smp
:
5560 smp_cpus
= atoi(optarg
);
5562 fprintf(stderr
, "Invalid number of CPUs\n");
5566 case QEMU_OPTION_vnc
:
5567 display_type
= DT_VNC
;
5568 vnc_display
= optarg
;
5571 case QEMU_OPTION_no_acpi
:
5574 case QEMU_OPTION_no_hpet
:
5577 case QEMU_OPTION_balloon
:
5578 if (balloon_parse(optarg
) < 0) {
5579 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5584 case QEMU_OPTION_no_reboot
:
5587 case QEMU_OPTION_no_shutdown
:
5590 case QEMU_OPTION_show_cursor
:
5593 case QEMU_OPTION_uuid
:
5594 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5595 fprintf(stderr
, "Fail to parse UUID string."
5596 " Wrong format.\n");
5601 case QEMU_OPTION_daemonize
:
5605 case QEMU_OPTION_option_rom
:
5606 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5607 fprintf(stderr
, "Too many option ROMs\n");
5610 option_rom
[nb_option_roms
] = optarg
;
5613 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5614 case QEMU_OPTION_semihosting
:
5615 semihosting_enabled
= 1;
5618 case QEMU_OPTION_tdf
:
5621 case QEMU_OPTION_kvm_shadow_memory
:
5622 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5624 case QEMU_OPTION_mempath
:
5628 case QEMU_OPTION_mem_prealloc
:
5629 mem_prealloc
= !mem_prealloc
;
5632 case QEMU_OPTION_name
:
5633 qemu_name
= qemu_strdup(optarg
);
5635 char *p
= strchr(qemu_name
, ',');
5638 if (strncmp(p
, "process=", 8)) {
5639 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5647 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5648 case QEMU_OPTION_prom_env
:
5649 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5650 fprintf(stderr
, "Too many prom variables\n");
5653 prom_envs
[nb_prom_envs
] = optarg
;
5658 case QEMU_OPTION_old_param
:
5662 case QEMU_OPTION_clock
:
5663 configure_alarms(optarg
);
5665 case QEMU_OPTION_startdate
:
5668 time_t rtc_start_date
;
5669 if (!strcmp(optarg
, "now")) {
5670 rtc_date_offset
= -1;
5672 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5680 } else if (sscanf(optarg
, "%d-%d-%d",
5683 &tm
.tm_mday
) == 3) {
5692 rtc_start_date
= mktimegm(&tm
);
5693 if (rtc_start_date
== -1) {
5695 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5696 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5699 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5703 case QEMU_OPTION_tb_size
:
5704 tb_size
= strtol(optarg
, NULL
, 0);
5708 case QEMU_OPTION_icount
:
5710 if (strcmp(optarg
, "auto") == 0) {
5711 icount_time_shift
= -1;
5713 icount_time_shift
= strtol(optarg
, NULL
, 0);
5716 case QEMU_OPTION_incoming
:
5720 case QEMU_OPTION_chroot
:
5721 chroot_dir
= optarg
;
5723 case QEMU_OPTION_runas
:
5726 case QEMU_OPTION_nvram
:
5731 case QEMU_OPTION_xen_domid
:
5732 xen_domid
= atoi(optarg
);
5734 case QEMU_OPTION_xen_create
:
5735 xen_mode
= XEN_CREATE
;
5737 case QEMU_OPTION_xen_attach
:
5738 xen_mode
= XEN_ATTACH
;
5745 /* If no data_dir is specified then try to find it relative to the
5748 data_dir
= find_datadir(argv
[0]);
5750 /* If all else fails use the install patch specified when building. */
5752 data_dir
= CONFIG_QEMU_SHAREDIR
;
5755 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5756 if (kvm_allowed
&& kqemu_allowed
) {
5758 "You can not enable both KVM and kqemu at the same time\n");
5763 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5764 if (smp_cpus
> machine
->max_cpus
) {
5765 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5766 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5771 if (display_type
== DT_NOGRAPHIC
) {
5772 if (serial_device_index
== 0)
5773 serial_devices
[0] = "stdio";
5774 if (parallel_device_index
== 0)
5775 parallel_devices
[0] = "null";
5776 if (strncmp(monitor_device
, "vc", 2) == 0)
5777 monitor_device
= "stdio";
5784 if (pipe(fds
) == -1)
5795 len
= read(fds
[0], &status
, 1);
5796 if (len
== -1 && (errno
== EINTR
))
5801 else if (status
== 1) {
5802 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5819 signal(SIGTSTP
, SIG_IGN
);
5820 signal(SIGTTOU
, SIG_IGN
);
5821 signal(SIGTTIN
, SIG_IGN
);
5824 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5827 write(fds
[1], &status
, 1);
5829 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5834 if (kvm_enabled()) {
5837 ret
= kvm_init(smp_cpus
);
5839 #if defined(KVM_UPSTREAM) || defined(NO_CPU_EMULATION)
5840 fprintf(stderr
, "failed to initialize KVM\n");
5843 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5852 if (qemu_init_main_loop()) {
5853 fprintf(stderr
, "qemu_init_main_loop failed\n");
5856 linux_boot
= (kernel_filename
!= NULL
);
5858 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5859 fprintf(stderr
, "-append only allowed with -kernel option\n");
5863 if (!linux_boot
&& initrd_filename
!= NULL
) {
5864 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5868 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5871 if (init_timer_alarm() < 0) {
5872 fprintf(stderr
, "could not initialize alarm timer\n");
5875 if (use_icount
&& icount_time_shift
< 0) {
5877 /* 125MIPS seems a reasonable initial guess at the guest speed.
5878 It will be corrected fairly quickly anyway. */
5879 icount_time_shift
= 3;
5880 init_icount_adjust();
5887 /* init network clients */
5888 if (nb_net_clients
== 0) {
5889 /* if no clients, we use a default config */
5890 net_clients
[nb_net_clients
++] = "nic";
5892 net_clients
[nb_net_clients
++] = "user";
5896 for(i
= 0;i
< nb_net_clients
; i
++) {
5897 if (net_client_parse(net_clients
[i
]) < 0)
5901 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5902 net_set_boot_mask(net_boot
);
5906 /* init the bluetooth world */
5907 for (i
= 0; i
< nb_bt_opts
; i
++)
5908 if (bt_parse(bt_opts
[i
]))
5911 /* init the memory */
5913 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5916 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5917 guest ram allocation. It needs to go away. */
5918 if (kqemu_allowed
) {
5919 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5920 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5921 if (!kqemu_phys_ram_base
) {
5922 fprintf(stderr
, "Could not allocate physical memory\n");
5928 /* init the dynamic translator */
5929 cpu_exec_init_all(tb_size
* 1024 * 1024);
5933 /* we always create the cdrom drive, even if no disk is there */
5935 if (nb_drives_opt
< MAX_DRIVES
)
5936 drive_add(NULL
, CDROM_ALIAS
);
5938 /* we always create at least one floppy */
5940 if (nb_drives_opt
< MAX_DRIVES
)
5941 drive_add(NULL
, FD_ALIAS
, 0);
5943 /* we always create one sd slot, even if no card is in it */
5945 if (nb_drives_opt
< MAX_DRIVES
)
5946 drive_add(NULL
, SD_ALIAS
);
5948 /* open the virtual block devices */
5950 for(i
= 0; i
< nb_drives_opt
; i
++)
5951 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5954 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5955 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5958 /* must be after terminal init, SDL library changes signal handlers */
5962 /* Maintain compatibility with multiple stdio monitors */
5963 if (!strcmp(monitor_device
,"stdio")) {
5964 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5965 const char *devname
= serial_devices
[i
];
5966 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5967 monitor_device
= NULL
;
5969 } else if (devname
&& !strcmp(devname
,"stdio")) {
5970 monitor_device
= NULL
;
5971 serial_devices
[i
] = "mon:stdio";
5977 if (nb_numa_nodes
> 0) {
5980 if (nb_numa_nodes
> smp_cpus
) {
5981 nb_numa_nodes
= smp_cpus
;
5984 /* If no memory size if given for any node, assume the default case
5985 * and distribute the available memory equally across all nodes
5987 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5988 if (node_mem
[i
] != 0)
5991 if (i
== nb_numa_nodes
) {
5992 uint64_t usedmem
= 0;
5994 /* On Linux, the each node's border has to be 8MB aligned,
5995 * the final node gets the rest.
5997 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5998 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5999 usedmem
+= node_mem
[i
];
6001 node_mem
[i
] = ram_size
- usedmem
;
6004 for (i
= 0; i
< nb_numa_nodes
; i
++) {
6005 if (node_cpumask
[i
] != 0)
6008 /* assigning the VCPUs round-robin is easier to implement, guest OSes
6009 * must cope with this anyway, because there are BIOSes out there in
6010 * real machines which also use this scheme.
6012 if (i
== nb_numa_nodes
) {
6013 for (i
= 0; i
< smp_cpus
; i
++) {
6014 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
6019 if (monitor_device
) {
6020 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
6022 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6027 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6028 const char *devname
= serial_devices
[i
];
6029 if (devname
&& strcmp(devname
, "none")) {
6031 snprintf(label
, sizeof(label
), "serial%d", i
);
6032 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6033 if (!serial_hds
[i
]) {
6034 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
6035 devname
, strerror(errno
));
6041 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6042 const char *devname
= parallel_devices
[i
];
6043 if (devname
&& strcmp(devname
, "none")) {
6045 snprintf(label
, sizeof(label
), "parallel%d", i
);
6046 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6047 if (!parallel_hds
[i
]) {
6048 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
6049 devname
, strerror(errno
));
6055 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6056 const char *devname
= virtio_consoles
[i
];
6057 if (devname
&& strcmp(devname
, "none")) {
6059 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6060 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6061 if (!virtcon_hds
[i
]) {
6062 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
6063 devname
, strerror(errno
));
6069 module_call_init(MODULE_INIT_DEVICE
);
6071 if (machine
->compat_props
) {
6072 qdev_prop_register_compat(machine
->compat_props
);
6074 machine
->init(ram_size
, boot_devices
,
6075 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6078 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
6079 for (i
= 0; i
< nb_numa_nodes
; i
++) {
6080 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
6086 current_machine
= machine
;
6088 /* init USB devices */
6090 for(i
= 0; i
< usb_devices_index
; i
++) {
6091 if (usb_device_add(usb_devices
[i
], 0) < 0) {
6092 fprintf(stderr
, "Warning: could not add USB device %s\n",
6099 dumb_display_init();
6100 /* just use the first displaystate for the moment */
6103 if (display_type
== DT_DEFAULT
) {
6104 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6105 display_type
= DT_SDL
;
6107 display_type
= DT_VNC
;
6108 vnc_display
= "localhost:0,to=99";
6114 switch (display_type
) {
6117 #if defined(CONFIG_CURSES)
6119 curses_display_init(ds
, full_screen
);
6122 #if defined(CONFIG_SDL)
6124 sdl_display_init(ds
, full_screen
, no_frame
);
6126 #elif defined(CONFIG_COCOA)
6128 cocoa_display_init(ds
, full_screen
);
6132 vnc_display_init(ds
);
6133 if (vnc_display_open(ds
, vnc_display
) < 0)
6136 if (show_vnc_port
) {
6137 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6145 dcl
= ds
->listeners
;
6146 while (dcl
!= NULL
) {
6147 if (dcl
->dpy_refresh
!= NULL
) {
6148 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6149 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6154 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6155 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6156 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6159 text_consoles_set_display(display_state
);
6160 qemu_chr_initial_reset();
6162 if (monitor_device
&& monitor_hd
)
6163 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6165 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6166 const char *devname
= serial_devices
[i
];
6167 if (devname
&& strcmp(devname
, "none")) {
6168 if (strstart(devname
, "vc", 0))
6169 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6173 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6174 const char *devname
= parallel_devices
[i
];
6175 if (devname
&& strcmp(devname
, "none")) {
6176 if (strstart(devname
, "vc", 0))
6177 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6181 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6182 const char *devname
= virtio_consoles
[i
];
6183 if (virtcon_hds
[i
] && devname
) {
6184 if (strstart(devname
, "vc", 0))
6185 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6189 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6190 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6196 do_loadvm(cur_mon
, loadvm
);
6199 qemu_start_incoming_migration(incoming
);
6200 } else if (autostart
) {
6210 len
= write(fds
[1], &status
, 1);
6211 if (len
== -1 && (errno
== EINTR
))
6218 TFR(fd
= open("/dev/null", O_RDWR
));
6224 pwd
= getpwnam(run_as
);
6226 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6232 if (chroot(chroot_dir
) < 0) {
6233 fprintf(stderr
, "chroot failed\n");
6240 if (setgid(pwd
->pw_gid
) < 0) {
6241 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6244 if (setuid(pwd
->pw_uid
) < 0) {
6245 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6248 if (setuid(0) != -1) {
6249 fprintf(stderr
, "Dropping privileges failed\n");