4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__DragonFly__)
58 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
59 #include <freebsd/stdlib.h>
64 #include <linux/rtc.h>
65 #include <sys/prctl.h>
67 /* For the benefit of older linux systems which don't supply it,
68 we use a local copy of hpet.h. */
69 /* #include <linux/hpet.h> */
72 #include <linux/ppdev.h>
73 #include <linux/parport.h>
77 #include <sys/ethernet.h>
78 #include <sys/sockio.h>
79 #include <netinet/arp.h>
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_icmp.h> // must come after ip.h
84 #include <netinet/udp.h>
85 #include <netinet/tcp.h>
89 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
90 discussion about Solaris header problems */
91 extern int madvise(caddr_t
, size_t, int);
96 #if defined(__OpenBSD__)
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
106 #include <mmsystem.h>
110 #if defined(__APPLE__) || defined(main)
112 int qemu_main(int argc
, char **argv
, char **envp
);
113 int main(int argc
, char **argv
)
115 return qemu_main(argc
, argv
, NULL
);
118 #define main qemu_main
120 #endif /* CONFIG_SDL */
124 #define main qemu_main
125 #endif /* CONFIG_COCOA */
128 #include "hw/boards.h"
130 #include "hw/pcmcia.h"
132 #include "hw/audiodev.h"
136 #include "hw/watchdog.h"
137 #include "hw/smbios.h"
140 #include "hw/loader.h"
147 #include "qemu-timer.h"
148 #include "qemu-char.h"
149 #include "cache-utils.h"
152 #include "audio/audio.h"
153 #include "migration.h"
156 #include "qemu-option.h"
157 #include "qemu-config.h"
158 #include "qemu-kvm.h"
159 #include "hw/device-assignment.h"
163 #include "exec-all.h"
165 #include "qemu_socket.h"
167 #include "slirp/libslirp.h"
169 #include "qemu-queue.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 /* Maximum number of monitor devices */
177 #define MAX_MONITOR_DEVICES 10
179 static const char *data_dir
;
180 const char *bios_name
= NULL
;
181 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
182 to store the VM snapshots */
183 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
184 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
185 DriveInfo
*extboot_drive
= NULL
;
186 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
187 static DisplayState
*display_state
;
188 DisplayType display_type
= DT_DEFAULT
;
189 const char* keyboard_layout
= NULL
;
192 NICInfo nd_table
[MAX_NICS
];
195 static int rtc_utc
= 1;
196 static int rtc_date_offset
= -1; /* -1 means no change */
197 QEMUClock
*rtc_clock
;
198 int vga_interface_type
= VGA_CIRRUS
;
200 int graphic_width
= 1024;
201 int graphic_height
= 768;
202 int graphic_depth
= 8;
204 int graphic_width
= 800;
205 int graphic_height
= 600;
206 int graphic_depth
= 15;
208 static int full_screen
= 0;
210 static int no_frame
= 0;
213 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
214 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
215 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
217 int win2k_install_hack
= 0;
222 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
223 int assigned_devices_index
;
228 const char *vnc_display
;
229 int acpi_enabled
= 1;
237 int graphic_rotate
= 0;
238 uint8_t irq0override
= 1;
242 const char *watchdog
;
243 const char *option_rom
[MAX_OPTION_ROMS
];
245 int semihosting_enabled
= 0;
246 int time_drift_fix
= 0;
247 unsigned int kvm_shadow_memory
= 0;
248 const char *mem_path
= NULL
;
250 int mem_prealloc
= 1; /* force preallocation of physical target memory */
255 const char *qemu_name
;
258 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
259 unsigned int nb_prom_envs
= 0;
260 const char *prom_envs
[MAX_PROM_ENVS
];
262 const char *nvram
= NULL
;
266 uint64_t node_mem
[MAX_NODES
];
267 uint64_t node_cpumask
[MAX_NODES
];
269 static CPUState
*cur_cpu
;
270 static CPUState
*next_cpu
;
271 static int timer_alarm_pending
= 1;
272 /* Conversion factor from emulated instructions to virtual clock ticks. */
273 static int icount_time_shift
;
274 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
275 #define MAX_ICOUNT_SHIFT 10
276 /* Compensate for varying guest execution speed. */
277 static int64_t qemu_icount_bias
;
278 static QEMUTimer
*icount_rt_timer
;
279 static QEMUTimer
*icount_vm_timer
;
280 static QEMUTimer
*nographic_timer
;
282 uint8_t qemu_uuid
[16];
284 static QEMUBootSetHandler
*boot_set_handler
;
285 static void *boot_set_opaque
;
287 /***********************************************************/
288 /* x86 ISA bus support */
290 target_phys_addr_t isa_mem_base
= 0;
293 /***********************************************************/
294 void hw_error(const char *fmt
, ...)
300 fprintf(stderr
, "qemu: hardware error: ");
301 vfprintf(stderr
, fmt
, ap
);
302 fprintf(stderr
, "\n");
303 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
304 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
306 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
308 cpu_dump_state(env
, stderr
, fprintf
, 0);
315 static void set_proc_name(const char *s
)
317 #if defined(__linux__) && defined(PR_SET_NAME)
321 name
[sizeof(name
) - 1] = 0;
322 strncpy(name
, s
, sizeof(name
));
323 /* Could rewrite argv[0] too, but that's a bit more complicated.
324 This simple way is enough for `top'. */
325 prctl(PR_SET_NAME
, name
);
332 static QEMUBalloonEvent
*qemu_balloon_event
;
333 void *qemu_balloon_event_opaque
;
335 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
337 qemu_balloon_event
= func
;
338 qemu_balloon_event_opaque
= opaque
;
341 void qemu_balloon(ram_addr_t target
)
343 if (qemu_balloon_event
)
344 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
347 ram_addr_t
qemu_balloon_status(void)
349 if (qemu_balloon_event
)
350 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
354 /***********************************************************/
357 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
358 static void *qemu_put_kbd_event_opaque
;
359 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
360 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
362 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
364 qemu_put_kbd_event_opaque
= opaque
;
365 qemu_put_kbd_event
= func
;
368 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
369 void *opaque
, int absolute
,
372 QEMUPutMouseEntry
*s
, *cursor
;
374 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
376 s
->qemu_put_mouse_event
= func
;
377 s
->qemu_put_mouse_event_opaque
= opaque
;
378 s
->qemu_put_mouse_event_absolute
= absolute
;
379 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
382 if (!qemu_put_mouse_event_head
) {
383 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
387 cursor
= qemu_put_mouse_event_head
;
388 while (cursor
->next
!= NULL
)
389 cursor
= cursor
->next
;
392 qemu_put_mouse_event_current
= s
;
397 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
399 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
401 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
404 cursor
= qemu_put_mouse_event_head
;
405 while (cursor
!= NULL
&& cursor
!= entry
) {
407 cursor
= cursor
->next
;
410 if (cursor
== NULL
) // does not exist or list empty
412 else if (prev
== NULL
) { // entry is head
413 qemu_put_mouse_event_head
= cursor
->next
;
414 if (qemu_put_mouse_event_current
== entry
)
415 qemu_put_mouse_event_current
= cursor
->next
;
416 qemu_free(entry
->qemu_put_mouse_event_name
);
421 prev
->next
= entry
->next
;
423 if (qemu_put_mouse_event_current
== entry
)
424 qemu_put_mouse_event_current
= prev
;
426 qemu_free(entry
->qemu_put_mouse_event_name
);
430 void kbd_put_keycode(int keycode
)
432 if (qemu_put_kbd_event
) {
433 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
437 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
439 QEMUPutMouseEvent
*mouse_event
;
440 void *mouse_event_opaque
;
443 if (!qemu_put_mouse_event_current
) {
448 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
450 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
453 if (graphic_rotate
) {
454 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
457 width
= graphic_width
- 1;
458 mouse_event(mouse_event_opaque
,
459 width
- dy
, dx
, dz
, buttons_state
);
461 mouse_event(mouse_event_opaque
,
462 dx
, dy
, dz
, buttons_state
);
466 int kbd_mouse_is_absolute(void)
468 if (!qemu_put_mouse_event_current
)
471 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
474 void do_info_mice(Monitor
*mon
)
476 QEMUPutMouseEntry
*cursor
;
479 if (!qemu_put_mouse_event_head
) {
480 monitor_printf(mon
, "No mouse devices connected\n");
484 monitor_printf(mon
, "Mouse devices available:\n");
485 cursor
= qemu_put_mouse_event_head
;
486 while (cursor
!= NULL
) {
487 monitor_printf(mon
, "%c Mouse #%d: %s\n",
488 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
489 index
, cursor
->qemu_put_mouse_event_name
);
491 cursor
= cursor
->next
;
495 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
497 QEMUPutMouseEntry
*cursor
;
499 int index
= qdict_get_int(qdict
, "index");
501 if (!qemu_put_mouse_event_head
) {
502 monitor_printf(mon
, "No mouse devices connected\n");
506 cursor
= qemu_put_mouse_event_head
;
507 while (cursor
!= NULL
&& index
!= i
) {
509 cursor
= cursor
->next
;
513 qemu_put_mouse_event_current
= cursor
;
515 monitor_printf(mon
, "Mouse at given index not found\n");
518 /* compute with 96 bit intermediate result: (a*b)/c */
519 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
524 #ifdef HOST_WORDS_BIGENDIAN
534 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
535 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
538 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
542 /***********************************************************/
543 /* real time host monotonic timer */
545 static int64_t get_clock_realtime(void)
549 gettimeofday(&tv
, NULL
);
550 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
555 static int64_t clock_freq
;
557 static void init_get_clock(void)
561 ret
= QueryPerformanceFrequency(&freq
);
563 fprintf(stderr
, "Could not calibrate ticks\n");
566 clock_freq
= freq
.QuadPart
;
569 static int64_t get_clock(void)
572 QueryPerformanceCounter(&ti
);
573 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
578 static int use_rt_clock
;
580 static void init_get_clock(void)
583 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
584 || defined(__DragonFly__)
587 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
594 static int64_t get_clock(void)
596 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
597 || defined(__DragonFly__)
600 clock_gettime(CLOCK_MONOTONIC
, &ts
);
601 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
605 /* XXX: using gettimeofday leads to problems if the date
606 changes, so it should be avoided. */
607 return get_clock_realtime();
612 /* Return the virtual CPU time, based on the instruction counter. */
613 static int64_t cpu_get_icount(void)
616 CPUState
*env
= cpu_single_env
;;
617 icount
= qemu_icount
;
620 fprintf(stderr
, "Bad clock read\n");
621 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
623 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
626 /***********************************************************/
627 /* guest cycle counter */
629 typedef struct TimersState
{
630 int64_t cpu_ticks_prev
;
631 int64_t cpu_ticks_offset
;
632 int64_t cpu_clock_offset
;
633 int32_t cpu_ticks_enabled
;
637 TimersState timers_state
;
639 /* return the host CPU cycle counter and handle stop/restart */
640 int64_t cpu_get_ticks(void)
643 return cpu_get_icount();
645 if (!timers_state
.cpu_ticks_enabled
) {
646 return timers_state
.cpu_ticks_offset
;
649 ticks
= cpu_get_real_ticks();
650 if (timers_state
.cpu_ticks_prev
> ticks
) {
651 /* Note: non increasing ticks may happen if the host uses
653 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
655 timers_state
.cpu_ticks_prev
= ticks
;
656 return ticks
+ timers_state
.cpu_ticks_offset
;
660 /* return the host CPU monotonic timer and handle stop/restart */
661 static int64_t cpu_get_clock(void)
664 if (!timers_state
.cpu_ticks_enabled
) {
665 return timers_state
.cpu_clock_offset
;
668 return ti
+ timers_state
.cpu_clock_offset
;
672 /* enable cpu_get_ticks() */
673 void cpu_enable_ticks(void)
675 if (!timers_state
.cpu_ticks_enabled
) {
676 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
677 timers_state
.cpu_clock_offset
-= get_clock();
678 timers_state
.cpu_ticks_enabled
= 1;
682 /* disable cpu_get_ticks() : the clock is stopped. You must not call
683 cpu_get_ticks() after that. */
684 void cpu_disable_ticks(void)
686 if (timers_state
.cpu_ticks_enabled
) {
687 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
688 timers_state
.cpu_clock_offset
= cpu_get_clock();
689 timers_state
.cpu_ticks_enabled
= 0;
693 /***********************************************************/
696 #define QEMU_CLOCK_REALTIME 0
697 #define QEMU_CLOCK_VIRTUAL 1
698 #define QEMU_CLOCK_HOST 2
702 /* XXX: add frequency */
710 struct QEMUTimer
*next
;
713 struct qemu_alarm_timer
{
717 int (*start
)(struct qemu_alarm_timer
*t
);
718 void (*stop
)(struct qemu_alarm_timer
*t
);
719 void (*rearm
)(struct qemu_alarm_timer
*t
);
723 #define ALARM_FLAG_DYNTICKS 0x1
724 #define ALARM_FLAG_EXPIRED 0x2
726 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
728 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
731 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
733 if (!alarm_has_dynticks(t
))
739 /* TODO: MIN_TIMER_REARM_US should be optimized */
740 #define MIN_TIMER_REARM_US 250
742 static struct qemu_alarm_timer
*alarm_timer
;
746 struct qemu_alarm_win32
{
749 } alarm_win32_data
= {0, -1};
751 static int win32_start_timer(struct qemu_alarm_timer
*t
);
752 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
753 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
757 static int unix_start_timer(struct qemu_alarm_timer
*t
);
758 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
762 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
763 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
764 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
766 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
767 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
769 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
770 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
772 #endif /* __linux__ */
776 /* Correlation between real and virtual time is always going to be
777 fairly approximate, so ignore small variation.
778 When the guest is idle real and virtual time will be aligned in
780 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
782 static void icount_adjust(void)
787 static int64_t last_delta
;
788 /* If the VM is not running, then do nothing. */
792 cur_time
= cpu_get_clock();
793 cur_icount
= qemu_get_clock(vm_clock
);
794 delta
= cur_icount
- cur_time
;
795 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
797 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
798 && icount_time_shift
> 0) {
799 /* The guest is getting too far ahead. Slow time down. */
803 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
804 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
805 /* The guest is getting too far behind. Speed time up. */
809 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
812 static void icount_adjust_rt(void * opaque
)
814 qemu_mod_timer(icount_rt_timer
,
815 qemu_get_clock(rt_clock
) + 1000);
819 static void icount_adjust_vm(void * opaque
)
821 qemu_mod_timer(icount_vm_timer
,
822 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
826 static void init_icount_adjust(void)
828 /* Have both realtime and virtual time triggers for speed adjustment.
829 The realtime trigger catches emulated time passing too slowly,
830 the virtual time trigger catches emulated time passing too fast.
831 Realtime triggers occur even when idle, so use them less frequently
833 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
834 qemu_mod_timer(icount_rt_timer
,
835 qemu_get_clock(rt_clock
) + 1000);
836 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
837 qemu_mod_timer(icount_vm_timer
,
838 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
841 static struct qemu_alarm_timer alarm_timers
[] = {
844 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
845 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
846 /* HPET - if available - is preferred */
847 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
848 /* ...otherwise try RTC */
849 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
851 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
853 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
854 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
855 {"win32", 0, win32_start_timer
,
856 win32_stop_timer
, NULL
, &alarm_win32_data
},
861 static void show_available_alarms(void)
865 printf("Available alarm timers, in order of precedence:\n");
866 for (i
= 0; alarm_timers
[i
].name
; i
++)
867 printf("%s\n", alarm_timers
[i
].name
);
870 static void configure_alarms(char const *opt
)
874 int count
= ARRAY_SIZE(alarm_timers
) - 1;
877 struct qemu_alarm_timer tmp
;
879 if (!strcmp(opt
, "?")) {
880 show_available_alarms();
884 arg
= qemu_strdup(opt
);
886 /* Reorder the array */
887 name
= strtok(arg
, ",");
889 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
890 if (!strcmp(alarm_timers
[i
].name
, name
))
895 fprintf(stderr
, "Unknown clock %s\n", name
);
904 tmp
= alarm_timers
[i
];
905 alarm_timers
[i
] = alarm_timers
[cur
];
906 alarm_timers
[cur
] = tmp
;
910 name
= strtok(NULL
, ",");
916 /* Disable remaining timers */
917 for (i
= cur
; i
< count
; i
++)
918 alarm_timers
[i
].name
= NULL
;
920 show_available_alarms();
925 #define QEMU_NUM_CLOCKS 3
929 QEMUClock
*host_clock
;
931 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
933 static QEMUClock
*qemu_new_clock(int type
)
936 clock
= qemu_mallocz(sizeof(QEMUClock
));
941 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
945 ts
= qemu_mallocz(sizeof(QEMUTimer
));
952 void qemu_free_timer(QEMUTimer
*ts
)
957 /* stop a timer, but do not dealloc it */
958 void qemu_del_timer(QEMUTimer
*ts
)
962 /* NOTE: this code must be signal safe because
963 qemu_timer_expired() can be called from a signal. */
964 pt
= &active_timers
[ts
->clock
->type
];
977 /* modify the current timer so that it will be fired when current_time
978 >= expire_time. The corresponding callback will be called. */
979 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
985 /* add the timer in the sorted list */
986 /* NOTE: this code must be signal safe because
987 qemu_timer_expired() can be called from a signal. */
988 pt
= &active_timers
[ts
->clock
->type
];
993 if (t
->expire_time
> expire_time
)
997 ts
->expire_time
= expire_time
;
1001 /* Rearm if necessary */
1002 if (pt
== &active_timers
[ts
->clock
->type
]) {
1003 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1004 qemu_rearm_alarm_timer(alarm_timer
);
1006 /* Interrupt execution to force deadline recalculation. */
1008 qemu_notify_event();
1012 int qemu_timer_pending(QEMUTimer
*ts
)
1015 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1022 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1026 return (timer_head
->expire_time
<= current_time
);
1029 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1035 if (!ts
|| ts
->expire_time
> current_time
)
1037 /* remove timer from the list before calling the callback */
1038 *ptimer_head
= ts
->next
;
1041 /* run the callback (the timer list can be modified) */
1046 int64_t qemu_get_clock(QEMUClock
*clock
)
1048 switch(clock
->type
) {
1049 case QEMU_CLOCK_REALTIME
:
1050 return get_clock() / 1000000;
1052 case QEMU_CLOCK_VIRTUAL
:
1054 return cpu_get_icount();
1056 return cpu_get_clock();
1058 case QEMU_CLOCK_HOST
:
1059 return get_clock_realtime();
1063 static void init_clocks(void)
1066 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1067 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1068 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1070 rtc_clock
= host_clock
;
1074 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1076 uint64_t expire_time
;
1078 if (qemu_timer_pending(ts
)) {
1079 expire_time
= ts
->expire_time
;
1083 qemu_put_be64(f
, expire_time
);
1086 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1088 uint64_t expire_time
;
1090 expire_time
= qemu_get_be64(f
);
1091 if (expire_time
!= -1) {
1092 qemu_mod_timer(ts
, expire_time
);
1098 static const VMStateDescription vmstate_timers
= {
1101 .minimum_version_id
= 1,
1102 .minimum_version_id_old
= 1,
1103 .fields
= (VMStateField
[]) {
1104 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1105 VMSTATE_INT64(dummy
, TimersState
),
1106 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1107 VMSTATE_END_OF_LIST()
1111 static void qemu_event_increment(void);
1114 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1115 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1118 static void host_alarm_handler(int host_signum
)
1122 #define DISP_FREQ 1000
1124 static int64_t delta_min
= INT64_MAX
;
1125 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1127 ti
= qemu_get_clock(vm_clock
);
1128 if (last_clock
!= 0) {
1129 delta
= ti
- last_clock
;
1130 if (delta
< delta_min
)
1132 if (delta
> delta_max
)
1135 if (++count
== DISP_FREQ
) {
1136 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1137 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1138 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1139 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1140 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1142 delta_min
= INT64_MAX
;
1150 if (alarm_has_dynticks(alarm_timer
) ||
1152 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1153 qemu_get_clock(vm_clock
))) ||
1154 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1155 qemu_get_clock(rt_clock
)) ||
1156 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1157 qemu_get_clock(host_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 */
1167 timer_alarm_pending
= 1;
1168 qemu_notify_event();
1172 static int64_t qemu_next_deadline(void)
1174 /* To avoid problems with overflow limit this to 2^32. */
1175 int64_t delta
= INT32_MAX
;
1177 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1178 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1179 qemu_get_clock(vm_clock
);
1181 if (active_timers
[QEMU_CLOCK_HOST
]) {
1182 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1183 qemu_get_clock(host_clock
);
1194 #if defined(__linux__)
1195 static uint64_t qemu_next_deadline_dyntick(void)
1203 delta
= (qemu_next_deadline() + 999) / 1000;
1205 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1206 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1207 qemu_get_clock(rt_clock
))*1000;
1208 if (rtdelta
< delta
)
1212 if (delta
< MIN_TIMER_REARM_US
)
1213 delta
= MIN_TIMER_REARM_US
;
1221 /* Sets a specific flag */
1222 static int fcntl_setfl(int fd
, int flag
)
1226 flags
= fcntl(fd
, F_GETFL
);
1230 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1236 #if defined(__linux__)
1238 #define RTC_FREQ 1024
1240 static void enable_sigio_timer(int fd
)
1242 struct sigaction act
;
1245 sigfillset(&act
.sa_mask
);
1247 act
.sa_handler
= host_alarm_handler
;
1249 sigaction(SIGIO
, &act
, NULL
);
1250 fcntl_setfl(fd
, O_ASYNC
);
1251 fcntl(fd
, F_SETOWN
, getpid());
1254 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1256 struct hpet_info info
;
1259 fd
= open("/dev/hpet", O_RDONLY
);
1264 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1266 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1267 "error, but for better emulation accuracy type:\n"
1268 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1272 /* Check capabilities */
1273 r
= ioctl(fd
, HPET_INFO
, &info
);
1277 /* Enable periodic mode */
1278 r
= ioctl(fd
, HPET_EPI
, 0);
1279 if (info
.hi_flags
&& (r
< 0))
1282 /* Enable interrupt */
1283 r
= ioctl(fd
, HPET_IE_ON
, 0);
1287 enable_sigio_timer(fd
);
1288 t
->priv
= (void *)(long)fd
;
1296 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1298 int fd
= (long)t
->priv
;
1303 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1306 unsigned long current_rtc_freq
= 0;
1308 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1311 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1312 if (current_rtc_freq
!= RTC_FREQ
&&
1313 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1314 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1315 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1316 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1319 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1325 enable_sigio_timer(rtc_fd
);
1327 t
->priv
= (void *)(long)rtc_fd
;
1332 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1334 int rtc_fd
= (long)t
->priv
;
1339 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1343 struct sigaction act
;
1345 sigfillset(&act
.sa_mask
);
1347 act
.sa_handler
= host_alarm_handler
;
1349 sigaction(SIGALRM
, &act
, NULL
);
1352 * Initialize ev struct to 0 to avoid valgrind complaining
1353 * about uninitialized data in timer_create call
1355 memset(&ev
, 0, sizeof(ev
));
1356 ev
.sigev_value
.sival_int
= 0;
1357 ev
.sigev_notify
= SIGEV_SIGNAL
;
1358 ev
.sigev_signo
= SIGALRM
;
1360 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1361 perror("timer_create");
1363 /* disable dynticks */
1364 fprintf(stderr
, "Dynamic Ticks disabled\n");
1369 t
->priv
= (void *)(long)host_timer
;
1374 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1376 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1378 timer_delete(host_timer
);
1381 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1383 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1384 struct itimerspec timeout
;
1385 int64_t nearest_delta_us
= INT64_MAX
;
1388 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1389 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1390 !active_timers
[QEMU_CLOCK_HOST
])
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 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1486 timeEndPeriod(data
->period
);
1493 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1495 struct qemu_alarm_win32
*data
= t
->priv
;
1497 timeKillEvent(data
->timerId
);
1498 timeEndPeriod(data
->period
);
1501 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1503 struct qemu_alarm_win32
*data
= t
->priv
;
1505 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1506 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1507 !active_timers
[QEMU_CLOCK_HOST
])
1510 timeKillEvent(data
->timerId
);
1512 data
->timerId
= timeSetEvent(1,
1516 TIME_ONESHOT
| TIME_PERIODIC
);
1518 if (!data
->timerId
) {
1519 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1522 timeEndPeriod(data
->period
);
1529 static int init_timer_alarm(void)
1531 struct qemu_alarm_timer
*t
= NULL
;
1534 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1535 t
= &alarm_timers
[i
];
1555 static void quit_timers(void)
1557 alarm_timer
->stop(alarm_timer
);
1561 /***********************************************************/
1562 /* host time/date access */
1563 void qemu_get_timedate(struct tm
*tm
, int offset
)
1570 if (rtc_date_offset
== -1) {
1574 ret
= localtime(&ti
);
1576 ti
-= rtc_date_offset
;
1580 memcpy(tm
, ret
, sizeof(struct tm
));
1583 int qemu_timedate_diff(struct tm
*tm
)
1587 if (rtc_date_offset
== -1)
1589 seconds
= mktimegm(tm
);
1591 seconds
= mktime(tm
);
1593 seconds
= mktimegm(tm
) + rtc_date_offset
;
1595 return seconds
- time(NULL
);
1598 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1600 time_t rtc_start_date
;
1603 if (!strcmp(startdate
, "now") && legacy
) {
1604 rtc_date_offset
= -1;
1606 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1614 } else if (sscanf(startdate
, "%d-%d-%d",
1617 &tm
.tm_mday
) == 3) {
1626 rtc_start_date
= mktimegm(&tm
);
1627 if (rtc_start_date
== -1) {
1629 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1630 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1633 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1637 static void configure_rtc(QemuOpts
*opts
)
1641 value
= qemu_opt_get(opts
, "base");
1643 if (!strcmp(value
, "utc")) {
1645 } else if (!strcmp(value
, "localtime")) {
1648 configure_rtc_date_offset(value
, 0);
1651 value
= qemu_opt_get(opts
, "clock");
1653 if (!strcmp(value
, "host")) {
1654 rtc_clock
= host_clock
;
1655 } else if (!strcmp(value
, "vm")) {
1656 rtc_clock
= vm_clock
;
1658 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1662 #ifdef CONFIG_TARGET_I386
1663 value
= qemu_opt_get(opts
, "driftfix");
1665 if (!strcmp(buf
, "slew")) {
1667 } else if (!strcmp(buf
, "none")) {
1670 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1678 static void socket_cleanup(void)
1683 static int socket_init(void)
1688 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1690 err
= WSAGetLastError();
1691 fprintf(stderr
, "WSAStartup: %d\n", err
);
1694 atexit(socket_cleanup
);
1699 /***********************************************************/
1700 /* Bluetooth support */
1703 static struct HCIInfo
*hci_table
[MAX_NICS
];
1705 static struct bt_vlan_s
{
1706 struct bt_scatternet_s net
;
1708 struct bt_vlan_s
*next
;
1711 /* find or alloc a new bluetooth "VLAN" */
1712 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1714 struct bt_vlan_s
**pvlan
, *vlan
;
1715 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1719 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1721 pvlan
= &first_bt_vlan
;
1722 while (*pvlan
!= NULL
)
1723 pvlan
= &(*pvlan
)->next
;
1728 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1732 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1737 static struct HCIInfo null_hci
= {
1738 .cmd_send
= null_hci_send
,
1739 .sco_send
= null_hci_send
,
1740 .acl_send
= null_hci_send
,
1741 .bdaddr_set
= null_hci_addr_set
,
1744 struct HCIInfo
*qemu_next_hci(void)
1746 if (cur_hci
== nb_hcis
)
1749 return hci_table
[cur_hci
++];
1752 static struct HCIInfo
*hci_init(const char *str
)
1755 struct bt_scatternet_s
*vlan
= 0;
1757 if (!strcmp(str
, "null"))
1760 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1762 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1763 else if (!strncmp(str
, "hci", 3)) {
1766 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1767 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1772 vlan
= qemu_find_bt_vlan(0);
1774 return bt_new_hci(vlan
);
1777 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1782 static int bt_hci_parse(const char *str
)
1784 struct HCIInfo
*hci
;
1787 if (nb_hcis
>= MAX_NICS
) {
1788 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1792 hci
= hci_init(str
);
1801 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1802 hci
->bdaddr_set(hci
, bdaddr
.b
);
1804 hci_table
[nb_hcis
++] = hci
;
1809 static void bt_vhci_add(int vlan_id
)
1811 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1814 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1815 "an empty scatternet %i\n", vlan_id
);
1817 bt_vhci_init(bt_new_hci(vlan
));
1820 static struct bt_device_s
*bt_device_add(const char *opt
)
1822 struct bt_scatternet_s
*vlan
;
1824 char *endp
= strstr(opt
, ",vlan=");
1825 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1828 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1831 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1833 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1838 vlan
= qemu_find_bt_vlan(vlan_id
);
1841 fprintf(stderr
, "qemu: warning: adding a slave device to "
1842 "an empty scatternet %i\n", vlan_id
);
1844 if (!strcmp(devname
, "keyboard"))
1845 return bt_keyboard_init(vlan
);
1847 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1851 static int bt_parse(const char *opt
)
1853 const char *endp
, *p
;
1856 if (strstart(opt
, "hci", &endp
)) {
1857 if (!*endp
|| *endp
== ',') {
1859 if (!strstart(endp
, ",vlan=", 0))
1862 return bt_hci_parse(opt
);
1864 } else if (strstart(opt
, "vhci", &endp
)) {
1865 if (!*endp
|| *endp
== ',') {
1867 if (strstart(endp
, ",vlan=", &p
)) {
1868 vlan
= strtol(p
, (char **) &endp
, 0);
1870 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1874 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1883 } else if (strstart(opt
, "device:", &endp
))
1884 return !bt_device_add(endp
);
1886 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1890 /***********************************************************/
1891 /* QEMU Block devices */
1893 #define HD_ALIAS "index=%d,media=disk"
1894 #define CDROM_ALIAS "index=2,media=cdrom"
1895 #define FD_ALIAS "index=%d,if=floppy"
1896 #define PFLASH_ALIAS "if=pflash"
1897 #define MTD_ALIAS "if=mtd"
1898 #define SD_ALIAS "index=0,if=sd"
1900 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1907 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1910 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1912 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1913 __FUNCTION__
, optstr
);
1917 qemu_opt_set(opts
, "file", file
);
1921 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1925 /* seek interface, bus and unit */
1927 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1928 if (dinfo
->type
== type
&&
1929 dinfo
->bus
== bus
&&
1930 dinfo
->unit
== unit
)
1937 DriveInfo
*drive_get_by_id(const char *id
)
1941 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1942 if (strcmp(id
, dinfo
->id
))
1949 int drive_get_max_bus(BlockInterfaceType type
)
1955 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1956 if(dinfo
->type
== type
&&
1957 dinfo
->bus
> max_bus
)
1958 max_bus
= dinfo
->bus
;
1963 const char *drive_get_serial(BlockDriverState
*bdrv
)
1967 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1968 if (dinfo
->bdrv
== bdrv
)
1969 return dinfo
->serial
;
1975 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1979 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1980 if (dinfo
->bdrv
== bdrv
)
1981 return dinfo
->onerror
;
1984 return BLOCK_ERR_STOP_ENOSPC
;
1987 static void bdrv_format_print(void *opaque
, const char *name
)
1989 fprintf(stderr
, " %s", name
);
1992 void drive_uninit(DriveInfo
*dinfo
)
1994 qemu_opts_del(dinfo
->opts
);
1995 bdrv_delete(dinfo
->bdrv
);
1996 QTAILQ_REMOVE(&drives
, dinfo
, next
);
2000 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
2004 const char *file
= NULL
;
2007 const char *mediastr
= "";
2008 BlockInterfaceType type
;
2009 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2010 int bus_id
, unit_id
;
2011 int cyls
, heads
, secs
, translation
;
2012 BlockDriver
*drv
= NULL
;
2013 QEMUMachine
*machine
= opaque
;
2018 int bdrv_flags
, onerror
;
2019 const char *devaddr
;
2026 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2029 if (machine
&& machine
->use_scsi
) {
2031 max_devs
= MAX_SCSI_DEVS
;
2032 pstrcpy(devname
, sizeof(devname
), "scsi");
2035 max_devs
= MAX_IDE_DEVS
;
2036 pstrcpy(devname
, sizeof(devname
), "ide");
2040 /* extract parameters */
2041 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2042 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2043 index
= qemu_opt_get_number(opts
, "index", -1);
2045 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2046 heads
= qemu_opt_get_number(opts
, "heads", 0);
2047 secs
= qemu_opt_get_number(opts
, "secs", 0);
2049 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2051 file
= qemu_opt_get(opts
, "file");
2052 serial
= qemu_opt_get(opts
, "serial");
2054 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2055 pstrcpy(devname
, sizeof(devname
), buf
);
2056 if (!strcmp(buf
, "ide")) {
2058 max_devs
= MAX_IDE_DEVS
;
2059 } else if (!strcmp(buf
, "scsi")) {
2061 max_devs
= MAX_SCSI_DEVS
;
2062 } else if (!strcmp(buf
, "floppy")) {
2065 } else if (!strcmp(buf
, "pflash")) {
2068 } else if (!strcmp(buf
, "mtd")) {
2071 } else if (!strcmp(buf
, "sd")) {
2074 } else if (!strcmp(buf
, "virtio")) {
2077 } else if (!strcmp(buf
, "xen")) {
2080 } else if (!strcmp(buf
, "none")) {
2084 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2089 if (cyls
|| heads
|| secs
) {
2090 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2091 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2094 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2095 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2098 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2099 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2104 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2107 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2111 if (!strcmp(buf
, "none"))
2112 translation
= BIOS_ATA_TRANSLATION_NONE
;
2113 else if (!strcmp(buf
, "lba"))
2114 translation
= BIOS_ATA_TRANSLATION_LBA
;
2115 else if (!strcmp(buf
, "auto"))
2116 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2118 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2123 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2124 if (!strcmp(buf
, "disk")) {
2126 } else if (!strcmp(buf
, "cdrom")) {
2127 if (cyls
|| secs
|| heads
) {
2129 "qemu: '%s' invalid physical CHS format\n", buf
);
2132 media
= MEDIA_CDROM
;
2134 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2139 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2140 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2142 else if (!strcmp(buf
, "writethrough"))
2144 else if (!strcmp(buf
, "writeback"))
2147 fprintf(stderr
, "qemu: invalid cache option\n");
2152 #ifdef CONFIG_LINUX_AIO
2153 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2154 if (!strcmp(buf
, "threads"))
2156 else if (!strcmp(buf
, "native"))
2159 fprintf(stderr
, "qemu: invalid aio option\n");
2165 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2166 if (strcmp(buf
, "?") == 0) {
2167 fprintf(stderr
, "qemu: Supported formats:");
2168 bdrv_iterate_format(bdrv_format_print
, NULL
);
2169 fprintf(stderr
, "\n");
2172 drv
= bdrv_find_format(buf
);
2174 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2179 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
2180 if (is_extboot
&& extboot_drive
) {
2181 fprintf(stderr
, "qemu: two bootable drives specified\n");
2185 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2186 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2187 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2188 fprintf(stderr
, "werror is no supported by this format\n");
2191 if (!strcmp(buf
, "ignore"))
2192 onerror
= BLOCK_ERR_IGNORE
;
2193 else if (!strcmp(buf
, "enospc"))
2194 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2195 else if (!strcmp(buf
, "stop"))
2196 onerror
= BLOCK_ERR_STOP_ANY
;
2197 else if (!strcmp(buf
, "report"))
2198 onerror
= BLOCK_ERR_REPORT
;
2200 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2205 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2206 if (type
!= IF_VIRTIO
) {
2207 fprintf(stderr
, "addr is not supported\n");
2212 /* compute bus and unit according index */
2215 if (bus_id
!= 0 || unit_id
!= -1) {
2217 "qemu: index cannot be used with bus and unit\n");
2225 unit_id
= index
% max_devs
;
2226 bus_id
= index
/ max_devs
;
2230 /* if user doesn't specify a unit_id,
2231 * try to find the first free
2234 if (unit_id
== -1) {
2236 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2238 if (max_devs
&& unit_id
>= max_devs
) {
2239 unit_id
-= max_devs
;
2247 if (max_devs
&& unit_id
>= max_devs
) {
2248 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2249 unit_id
, max_devs
- 1);
2254 * ignore multiple definitions
2257 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2264 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2265 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2266 dinfo
->id
= qemu_strdup(buf
);
2268 /* no id supplied -> create one */
2269 dinfo
->id
= qemu_mallocz(32);
2270 if (type
== IF_IDE
|| type
== IF_SCSI
)
2271 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2273 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2274 devname
, bus_id
, mediastr
, unit_id
);
2276 snprintf(dinfo
->id
, 32, "%s%s%i",
2277 devname
, mediastr
, unit_id
);
2279 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2280 dinfo
->devaddr
= devaddr
;
2282 dinfo
->bus
= bus_id
;
2283 dinfo
->unit
= unit_id
;
2284 dinfo
->onerror
= onerror
;
2287 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2288 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2290 extboot_drive
= dinfo
;
2301 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2302 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2306 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2311 /* FIXME: This isn't really a floppy, but it's a reasonable
2314 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2320 /* add virtio block device */
2321 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2322 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2323 qemu_opt_set(opts
, "drive", dinfo
->id
);
2325 qemu_opt_set(opts
, "addr", devaddr
);
2336 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2337 cache
= 2; /* always use write-back with snapshot */
2339 if (cache
== 0) /* no caching */
2340 bdrv_flags
|= BDRV_O_NOCACHE
;
2341 else if (cache
== 2) /* write-back */
2342 bdrv_flags
|= BDRV_O_CACHE_WB
;
2345 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2347 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2350 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2351 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2352 file
, strerror(errno
));
2356 if (bdrv_key_required(dinfo
->bdrv
))
2362 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2364 QEMUMachine
*machine
= opaque
;
2365 int fatal_error
= 0;
2367 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2374 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2376 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2377 qemu_opt_set(opts
, "snapshot", "on");
2382 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2384 boot_set_handler
= func
;
2385 boot_set_opaque
= opaque
;
2388 int qemu_boot_set(const char *boot_devices
)
2390 if (!boot_set_handler
) {
2393 return boot_set_handler(boot_set_opaque
, boot_devices
);
2396 static int parse_bootdevices(char *devices
)
2398 /* We just do some generic consistency checks */
2402 for (p
= devices
; *p
!= '\0'; p
++) {
2403 /* Allowed boot devices are:
2404 * a-b: floppy disk drives
2405 * c-f: IDE disk drives
2406 * g-m: machine implementation dependant drives
2407 * n-p: network devices
2408 * It's up to each machine implementation to check if the given boot
2409 * devices match the actual hardware implementation and firmware
2412 if (*p
< 'a' || *p
> 'p') {
2413 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2416 if (bitmap
& (1 << (*p
- 'a'))) {
2417 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2420 bitmap
|= 1 << (*p
- 'a');
2425 static void restore_boot_devices(void *opaque
)
2427 char *standard_boot_devices
= opaque
;
2429 qemu_boot_set(standard_boot_devices
);
2431 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2432 qemu_free(standard_boot_devices
);
2435 static void numa_add(const char *optarg
)
2439 unsigned long long value
, endvalue
;
2442 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2443 if (!strcmp(option
, "node")) {
2444 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2445 nodenr
= nb_numa_nodes
;
2447 nodenr
= strtoull(option
, NULL
, 10);
2450 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2451 node_mem
[nodenr
] = 0;
2453 value
= strtoull(option
, &endptr
, 0);
2455 case 0: case 'M': case 'm':
2462 node_mem
[nodenr
] = value
;
2464 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2465 node_cpumask
[nodenr
] = 0;
2467 value
= strtoull(option
, &endptr
, 10);
2470 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2472 if (*endptr
== '-') {
2473 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2474 if (endvalue
>= 63) {
2477 "only 63 CPUs in NUMA mode supported.\n");
2479 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2484 node_cpumask
[nodenr
] = value
;
2491 static void smp_parse(const char *optarg
)
2493 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2497 smp
= strtoul(optarg
, &endptr
, 10);
2498 if (endptr
!= optarg
) {
2499 if (*endptr
== ',') {
2503 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2504 sockets
= strtoull(option
, NULL
, 10);
2505 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2506 cores
= strtoull(option
, NULL
, 10);
2507 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2508 threads
= strtoull(option
, NULL
, 10);
2509 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2510 max_cpus
= strtoull(option
, NULL
, 10);
2512 /* compute missing values, prefer sockets over cores over threads */
2513 if (smp
== 0 || sockets
== 0) {
2514 sockets
= sockets
> 0 ? sockets
: 1;
2515 cores
= cores
> 0 ? cores
: 1;
2516 threads
= threads
> 0 ? threads
: 1;
2518 smp
= cores
* threads
* sockets
;
2520 sockets
= smp
/ (cores
* threads
);
2524 threads
= threads
> 0 ? threads
: 1;
2525 cores
= smp
/ (sockets
* threads
);
2528 sockets
= smp
/ (cores
* threads
);
2530 threads
= smp
/ (cores
* sockets
);
2535 smp_cores
= cores
> 0 ? cores
: 1;
2536 smp_threads
= threads
> 0 ? threads
: 1;
2538 max_cpus
= smp_cpus
;
2541 /***********************************************************/
2544 static void usb_msd_password_cb(void *opaque
, int err
)
2546 USBDevice
*dev
= opaque
;
2549 usb_device_attach(dev
);
2551 dev
->info
->handle_destroy(dev
);
2560 .qdev
= "QEMU USB Mouse",
2563 .qdev
= "QEMU USB Tablet",
2566 .qdev
= "QEMU USB Keyboard",
2568 .name
= "wacom-tablet",
2569 .qdev
= "QEMU PenPartner Tablet",
2573 static int usb_device_add(const char *devname
, int is_hotplug
)
2576 USBBus
*bus
= usb_bus_find(-1 /* any */);
2577 USBDevice
*dev
= NULL
;
2583 /* simple devices which don't need extra care */
2584 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2585 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2587 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2591 /* the other ones */
2592 if (strstart(devname
, "host:", &p
)) {
2593 dev
= usb_host_device_open(p
);
2594 } else if (strstart(devname
, "disk:", &p
)) {
2595 BlockDriverState
*bs
;
2597 dev
= usb_msd_init(p
);
2600 bs
= usb_msd_get_bdrv(dev
);
2601 if (bdrv_key_required(bs
)) {
2604 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2609 } else if (strstart(devname
, "serial:", &p
)) {
2610 dev
= usb_serial_init(p
);
2611 #ifdef CONFIG_BRLAPI
2612 } else if (!strcmp(devname
, "braille")) {
2613 dev
= usb_baum_init();
2615 } else if (strstart(devname
, "net:", &p
)) {
2619 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2624 qemu_opt_set(opts
, "type", "nic");
2625 qemu_opt_set(opts
, "model", "usb");
2627 idx
= net_client_init(NULL
, opts
, 0);
2632 dev
= usb_net_init(&nd_table
[idx
]);
2633 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2634 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2635 bt_new_hci(qemu_find_bt_vlan(0)));
2646 static int usb_device_del(const char *devname
)
2651 if (strstart(devname
, "host:", &p
))
2652 return usb_host_device_close(p
);
2657 p
= strchr(devname
, '.');
2660 bus_num
= strtoul(devname
, NULL
, 0);
2661 addr
= strtoul(p
+ 1, NULL
, 0);
2663 return usb_device_delete_addr(bus_num
, addr
);
2666 static int usb_parse(const char *cmdline
)
2668 return usb_device_add(cmdline
, 0);
2671 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2673 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2676 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2678 usb_device_del(qdict_get_str(qdict
, "devname"));
2681 /***********************************************************/
2682 /* PCMCIA/Cardbus */
2684 static struct pcmcia_socket_entry_s
{
2685 PCMCIASocket
*socket
;
2686 struct pcmcia_socket_entry_s
*next
;
2687 } *pcmcia_sockets
= 0;
2689 void pcmcia_socket_register(PCMCIASocket
*socket
)
2691 struct pcmcia_socket_entry_s
*entry
;
2693 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2694 entry
->socket
= socket
;
2695 entry
->next
= pcmcia_sockets
;
2696 pcmcia_sockets
= entry
;
2699 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2701 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2703 ptr
= &pcmcia_sockets
;
2704 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2705 if (entry
->socket
== socket
) {
2711 void pcmcia_info(Monitor
*mon
)
2713 struct pcmcia_socket_entry_s
*iter
;
2715 if (!pcmcia_sockets
)
2716 monitor_printf(mon
, "No PCMCIA sockets\n");
2718 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2719 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2720 iter
->socket
->attached
? iter
->socket
->card_string
:
2724 /***********************************************************/
2725 /* register display */
2727 struct DisplayAllocator default_allocator
= {
2728 defaultallocator_create_displaysurface
,
2729 defaultallocator_resize_displaysurface
,
2730 defaultallocator_free_displaysurface
2733 void register_displaystate(DisplayState
*ds
)
2743 DisplayState
*get_displaystate(void)
2745 return display_state
;
2748 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2750 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2751 return ds
->allocator
;
2756 static void dumb_display_init(void)
2758 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2759 ds
->allocator
= &default_allocator
;
2760 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2761 register_displaystate(ds
);
2764 /***********************************************************/
2767 typedef struct IOHandlerRecord
{
2769 IOCanRWHandler
*fd_read_poll
;
2771 IOHandler
*fd_write
;
2774 /* temporary data */
2776 struct IOHandlerRecord
*next
;
2779 static IOHandlerRecord
*first_io_handler
;
2781 /* XXX: fd_read_poll should be suppressed, but an API change is
2782 necessary in the character devices to suppress fd_can_read(). */
2783 int qemu_set_fd_handler2(int fd
,
2784 IOCanRWHandler
*fd_read_poll
,
2786 IOHandler
*fd_write
,
2789 IOHandlerRecord
**pioh
, *ioh
;
2791 if (!fd_read
&& !fd_write
) {
2792 pioh
= &first_io_handler
;
2797 if (ioh
->fd
== fd
) {
2804 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2808 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2809 ioh
->next
= first_io_handler
;
2810 first_io_handler
= ioh
;
2813 ioh
->fd_read_poll
= fd_read_poll
;
2814 ioh
->fd_read
= fd_read
;
2815 ioh
->fd_write
= fd_write
;
2816 ioh
->opaque
= opaque
;
2819 qemu_notify_event();
2823 int qemu_set_fd_handler(int fd
,
2825 IOHandler
*fd_write
,
2828 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2832 /***********************************************************/
2833 /* Polling handling */
2835 typedef struct PollingEntry
{
2838 struct PollingEntry
*next
;
2841 static PollingEntry
*first_polling_entry
;
2843 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2845 PollingEntry
**ppe
, *pe
;
2846 pe
= qemu_mallocz(sizeof(PollingEntry
));
2848 pe
->opaque
= opaque
;
2849 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2854 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2856 PollingEntry
**ppe
, *pe
;
2857 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2859 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2867 /***********************************************************/
2868 /* Wait objects support */
2869 typedef struct WaitObjects
{
2871 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2872 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2873 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2876 static WaitObjects wait_objects
= {0};
2878 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2880 WaitObjects
*w
= &wait_objects
;
2882 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2884 w
->events
[w
->num
] = handle
;
2885 w
->func
[w
->num
] = func
;
2886 w
->opaque
[w
->num
] = opaque
;
2891 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2894 WaitObjects
*w
= &wait_objects
;
2897 for (i
= 0; i
< w
->num
; i
++) {
2898 if (w
->events
[i
] == handle
)
2901 w
->events
[i
] = w
->events
[i
+ 1];
2902 w
->func
[i
] = w
->func
[i
+ 1];
2903 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2911 /***********************************************************/
2912 /* ram save/restore */
2914 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2915 #define RAM_SAVE_FLAG_COMPRESS 0x02
2916 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2917 #define RAM_SAVE_FLAG_PAGE 0x08
2918 #define RAM_SAVE_FLAG_EOS 0x10
2920 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2922 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2923 uint32_t *array
= (uint32_t *)page
;
2926 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2927 if (array
[i
] != val
)
2934 static int ram_save_block(QEMUFile
*f
)
2936 static ram_addr_t current_addr
= 0;
2937 ram_addr_t saved_addr
= current_addr
;
2938 ram_addr_t addr
= 0;
2941 while (addr
< last_ram_offset
) {
2942 if (kvm_enabled() && current_addr
== 0) {
2944 r
= kvm_update_dirty_pages_log();
2946 fprintf(stderr
, "%s: update dirty pages log failed %d\n", __FUNCTION__
, r
);
2947 qemu_file_set_error(f
);
2951 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2954 cpu_physical_memory_reset_dirty(current_addr
,
2955 current_addr
+ TARGET_PAGE_SIZE
,
2956 MIGRATION_DIRTY_FLAG
);
2958 p
= qemu_get_ram_ptr(current_addr
);
2960 if (is_dup_page(p
, *p
)) {
2961 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2962 qemu_put_byte(f
, *p
);
2964 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2965 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2971 addr
+= TARGET_PAGE_SIZE
;
2972 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2978 static uint64_t bytes_transferred
= 0;
2980 static ram_addr_t
ram_save_remaining(void)
2983 ram_addr_t count
= 0;
2985 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2986 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2993 uint64_t ram_bytes_remaining(void)
2995 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2998 uint64_t ram_bytes_transferred(void)
3000 return bytes_transferred
;
3003 uint64_t ram_bytes_total(void)
3005 return last_ram_offset
;
3008 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3011 uint64_t bytes_transferred_last
;
3013 uint64_t expected_time
= 0;
3015 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3016 qemu_file_set_error(f
);
3021 /* Make sure all dirty bits are set */
3022 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3023 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3024 cpu_physical_memory_set_dirty(addr
);
3027 /* Enable dirty memory tracking */
3028 cpu_physical_memory_set_dirty_tracking(1);
3030 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3033 bytes_transferred_last
= bytes_transferred
;
3034 bwidth
= get_clock();
3036 while (!qemu_file_rate_limit(f
)) {
3039 ret
= ram_save_block(f
);
3040 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3041 if (ret
== 0) /* no more blocks */
3045 bwidth
= get_clock() - bwidth
;
3046 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3048 /* if we haven't transferred anything this round, force expected_time to a
3049 * a very high value, but without crashing */
3053 /* try transferring iterative blocks of memory */
3057 /* flush all remaining blocks regardless of rate limiting */
3058 while (ram_save_block(f
) != 0) {
3059 bytes_transferred
+= TARGET_PAGE_SIZE
;
3061 cpu_physical_memory_set_dirty_tracking(0);
3064 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3066 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3068 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3071 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3076 if (version_id
!= 3)
3080 addr
= qemu_get_be64(f
);
3082 flags
= addr
& ~TARGET_PAGE_MASK
;
3083 addr
&= TARGET_PAGE_MASK
;
3085 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3086 if (addr
!= last_ram_offset
)
3090 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3091 uint8_t ch
= qemu_get_byte(f
);
3092 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3095 (!kvm_enabled() || kvm_has_sync_mmu())) {
3096 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3099 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3100 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3101 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3106 void qemu_service_io(void)
3108 qemu_notify_event();
3111 /***********************************************************/
3112 /* machine registration */
3114 static QEMUMachine
*first_machine
= NULL
;
3115 QEMUMachine
*current_machine
= NULL
;
3117 int qemu_register_machine(QEMUMachine
*m
)
3120 pm
= &first_machine
;
3128 static QEMUMachine
*find_machine(const char *name
)
3132 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3133 if (!strcmp(m
->name
, name
))
3135 if (m
->alias
&& !strcmp(m
->alias
, name
))
3141 static QEMUMachine
*find_default_machine(void)
3145 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3146 if (m
->is_default
) {
3153 /***********************************************************/
3154 /* main execution loop */
3156 static void gui_update(void *opaque
)
3158 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3159 DisplayState
*ds
= opaque
;
3160 DisplayChangeListener
*dcl
= ds
->listeners
;
3164 while (dcl
!= NULL
) {
3165 if (dcl
->gui_timer_interval
&&
3166 dcl
->gui_timer_interval
< interval
)
3167 interval
= dcl
->gui_timer_interval
;
3170 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3173 static void nographic_update(void *opaque
)
3175 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3177 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3180 struct vm_change_state_entry
{
3181 VMChangeStateHandler
*cb
;
3183 QLIST_ENTRY (vm_change_state_entry
) entries
;
3186 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3188 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3191 VMChangeStateEntry
*e
;
3193 e
= qemu_mallocz(sizeof (*e
));
3197 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3201 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3203 QLIST_REMOVE (e
, entries
);
3207 static void vm_state_notify(int running
, int reason
)
3209 VMChangeStateEntry
*e
;
3211 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3212 e
->cb(e
->opaque
, running
, reason
);
3216 static void resume_all_vcpus(void);
3217 static void pause_all_vcpus(void);
3224 vm_state_notify(1, 0);
3225 qemu_rearm_alarm_timer(alarm_timer
);
3230 /* reset/shutdown handler */
3232 typedef struct QEMUResetEntry
{
3233 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3234 QEMUResetHandler
*func
;
3238 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3239 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3240 static int reset_requested
;
3241 static int shutdown_requested
;
3242 static int powerdown_requested
;
3243 static int debug_requested
;
3244 static int vmstop_requested
;
3246 int qemu_no_shutdown(void)
3248 int r
= no_shutdown
;
3253 int qemu_shutdown_requested(void)
3255 int r
= shutdown_requested
;
3256 shutdown_requested
= 0;
3260 int qemu_reset_requested(void)
3262 int r
= reset_requested
;
3263 reset_requested
= 0;
3267 int qemu_powerdown_requested(void)
3269 int r
= powerdown_requested
;
3270 powerdown_requested
= 0;
3274 static int qemu_debug_requested(void)
3276 int r
= debug_requested
;
3277 debug_requested
= 0;
3281 static int qemu_vmstop_requested(void)
3283 int r
= vmstop_requested
;
3284 vmstop_requested
= 0;
3288 static void do_vm_stop(int reason
)
3291 cpu_disable_ticks();
3294 vm_state_notify(0, reason
);
3298 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3300 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3303 re
->opaque
= opaque
;
3304 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3307 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3311 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3312 if (re
->func
== func
&& re
->opaque
== opaque
) {
3313 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3320 void qemu_system_reset(void)
3322 QEMUResetEntry
*re
, *nre
;
3324 /* reset all devices */
3325 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3326 re
->func(re
->opaque
);
3330 void qemu_system_reset_request(void)
3333 shutdown_requested
= 1;
3335 reset_requested
= 1;
3337 if (cpu_single_env
) {
3338 cpu_single_env
->stopped
= 1;
3340 qemu_notify_event();
3343 void qemu_system_shutdown_request(void)
3345 shutdown_requested
= 1;
3346 qemu_notify_event();
3349 void qemu_system_powerdown_request(void)
3351 powerdown_requested
= 1;
3352 qemu_notify_event();
3355 #ifdef CONFIG_IOTHREAD
3356 static void qemu_system_vmstop_request(int reason
)
3358 vmstop_requested
= reason
;
3359 qemu_notify_event();
3364 static int io_thread_fd
= -1;
3366 static void qemu_event_increment(void)
3368 static const char byte
= 0;
3370 if (io_thread_fd
== -1)
3373 write(io_thread_fd
, &byte
, sizeof(byte
));
3376 static void qemu_event_read(void *opaque
)
3378 int fd
= (unsigned long)opaque
;
3381 /* Drain the notify pipe */
3384 len
= read(fd
, buffer
, sizeof(buffer
));
3385 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3388 static int qemu_event_init(void)
3397 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3401 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3405 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3406 (void *)(unsigned long)fds
[0]);
3408 io_thread_fd
= fds
[1];
3417 HANDLE qemu_event_handle
;
3419 static void dummy_event_handler(void *opaque
)
3423 static int qemu_event_init(void)
3425 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3426 if (!qemu_event_handle
) {
3427 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3430 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3434 static void qemu_event_increment(void)
3436 if (!SetEvent(qemu_event_handle
)) {
3437 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3444 static int cpu_can_run(CPUState
*env
)
3453 #ifndef CONFIG_IOTHREAD
3454 static int qemu_init_main_loop(void)
3456 return qemu_event_init();
3459 void qemu_init_vcpu(void *_env
)
3461 CPUState
*env
= _env
;
3465 env
->nr_cores
= smp_cores
;
3466 env
->nr_threads
= smp_threads
;
3470 int qemu_cpu_self(void *env
)
3475 static void resume_all_vcpus(void)
3479 static void pause_all_vcpus(void)
3483 void qemu_cpu_kick(void *env
)
3488 void qemu_notify_event(void)
3490 CPUState
*env
= cpu_single_env
;
3492 if (kvm_enabled()) {
3493 qemu_kvm_notify_work();
3502 void qemu_mutex_lock_iothread(void) {}
3503 void qemu_mutex_unlock_iothread(void) {}
3506 void vm_stop(int reason
)
3511 #else /* CONFIG_IOTHREAD */
3513 #include "qemu-thread.h"
3515 QemuMutex qemu_global_mutex
;
3516 static QemuMutex qemu_fair_mutex
;
3518 static QemuThread io_thread
;
3520 static QemuThread
*tcg_cpu_thread
;
3521 static QemuCond
*tcg_halt_cond
;
3523 static int qemu_system_ready
;
3525 static QemuCond qemu_cpu_cond
;
3527 static QemuCond qemu_system_cond
;
3528 static QemuCond qemu_pause_cond
;
3530 static void block_io_signals(void);
3531 static void unblock_io_signals(void);
3532 static int tcg_has_work(void);
3534 static int qemu_init_main_loop(void)
3538 ret
= qemu_event_init();
3542 qemu_cond_init(&qemu_pause_cond
);
3543 qemu_mutex_init(&qemu_fair_mutex
);
3544 qemu_mutex_init(&qemu_global_mutex
);
3545 qemu_mutex_lock(&qemu_global_mutex
);
3547 unblock_io_signals();
3548 qemu_thread_self(&io_thread
);
3553 static void qemu_wait_io_event(CPUState
*env
)
3555 while (!tcg_has_work())
3556 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3558 qemu_mutex_unlock(&qemu_global_mutex
);
3561 * Users of qemu_global_mutex can be starved, having no chance
3562 * to acquire it since this path will get to it first.
3563 * So use another lock to provide fairness.
3565 qemu_mutex_lock(&qemu_fair_mutex
);
3566 qemu_mutex_unlock(&qemu_fair_mutex
);
3568 qemu_mutex_lock(&qemu_global_mutex
);
3572 qemu_cond_signal(&qemu_pause_cond
);
3576 static int qemu_cpu_exec(CPUState
*env
);
3578 static void *kvm_cpu_thread_fn(void *arg
)
3580 CPUState
*env
= arg
;
3583 qemu_thread_self(env
->thread
);
3587 /* signal CPU creation */
3588 qemu_mutex_lock(&qemu_global_mutex
);
3590 qemu_cond_signal(&qemu_cpu_cond
);
3592 /* and wait for machine initialization */
3593 while (!qemu_system_ready
)
3594 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3597 if (cpu_can_run(env
))
3599 qemu_wait_io_event(env
);
3605 static void tcg_cpu_exec(void);
3607 static void *tcg_cpu_thread_fn(void *arg
)
3609 CPUState
*env
= arg
;
3612 qemu_thread_self(env
->thread
);
3614 /* signal CPU creation */
3615 qemu_mutex_lock(&qemu_global_mutex
);
3616 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3618 qemu_cond_signal(&qemu_cpu_cond
);
3620 /* and wait for machine initialization */
3621 while (!qemu_system_ready
)
3622 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3626 qemu_wait_io_event(cur_cpu
);
3632 void qemu_cpu_kick(void *_env
)
3634 CPUState
*env
= _env
;
3635 qemu_cond_broadcast(env
->halt_cond
);
3637 qemu_thread_signal(env
->thread
, SIGUSR1
);
3640 int qemu_cpu_self(void *_env
)
3642 CPUState
*env
= _env
;
3645 qemu_thread_self(&this);
3647 return qemu_thread_equal(&this, env
->thread
);
3650 static void cpu_signal(int sig
)
3653 cpu_exit(cpu_single_env
);
3656 static void block_io_signals(void)
3659 struct sigaction sigact
;
3662 sigaddset(&set
, SIGUSR2
);
3663 sigaddset(&set
, SIGIO
);
3664 sigaddset(&set
, SIGALRM
);
3665 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3668 sigaddset(&set
, SIGUSR1
);
3669 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3671 memset(&sigact
, 0, sizeof(sigact
));
3672 sigact
.sa_handler
= cpu_signal
;
3673 sigaction(SIGUSR1
, &sigact
, NULL
);
3676 static void unblock_io_signals(void)
3681 sigaddset(&set
, SIGUSR2
);
3682 sigaddset(&set
, SIGIO
);
3683 sigaddset(&set
, SIGALRM
);
3684 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3687 sigaddset(&set
, SIGUSR1
);
3688 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3691 static void qemu_signal_lock(unsigned int msecs
)
3693 qemu_mutex_lock(&qemu_fair_mutex
);
3695 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3696 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3697 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3700 qemu_mutex_unlock(&qemu_fair_mutex
);
3703 void qemu_mutex_lock_iothread(void)
3705 if (kvm_enabled()) {
3706 qemu_mutex_lock(&qemu_fair_mutex
);
3707 qemu_mutex_lock(&qemu_global_mutex
);
3708 qemu_mutex_unlock(&qemu_fair_mutex
);
3710 qemu_signal_lock(100);
3713 void qemu_mutex_unlock_iothread(void)
3715 qemu_mutex_unlock(&qemu_global_mutex
);
3718 static int all_vcpus_paused(void)
3720 CPUState
*penv
= first_cpu
;
3725 penv
= (CPUState
*)penv
->next_cpu
;
3731 static void pause_all_vcpus(void)
3733 CPUState
*penv
= first_cpu
;
3737 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3738 qemu_cpu_kick(penv
);
3739 penv
= (CPUState
*)penv
->next_cpu
;
3742 while (!all_vcpus_paused()) {
3743 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3746 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3747 penv
= (CPUState
*)penv
->next_cpu
;
3752 static void resume_all_vcpus(void)
3754 CPUState
*penv
= first_cpu
;
3759 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3760 qemu_cpu_kick(penv
);
3761 penv
= (CPUState
*)penv
->next_cpu
;
3765 static void tcg_init_vcpu(void *_env
)
3767 CPUState
*env
= _env
;
3768 /* share a single thread for all cpus with TCG */
3769 if (!tcg_cpu_thread
) {
3770 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3771 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3772 qemu_cond_init(env
->halt_cond
);
3773 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3774 while (env
->created
== 0)
3775 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3776 tcg_cpu_thread
= env
->thread
;
3777 tcg_halt_cond
= env
->halt_cond
;
3779 env
->thread
= tcg_cpu_thread
;
3780 env
->halt_cond
= tcg_halt_cond
;
3784 static void kvm_start_vcpu(CPUState
*env
)
3786 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3787 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3788 qemu_cond_init(env
->halt_cond
);
3789 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3790 while (env
->created
== 0)
3791 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3794 void qemu_init_vcpu(void *_env
)
3796 CPUState
*env
= _env
;
3799 kvm_start_vcpu(env
);
3802 env
->nr_cores
= smp_cores
;
3803 env
->nr_threads
= smp_threads
;
3806 void qemu_notify_event(void)
3808 qemu_event_increment();
3811 void vm_stop(int reason
)
3814 qemu_thread_self(&me
);
3816 if (!qemu_thread_equal(&me
, &io_thread
)) {
3817 qemu_system_vmstop_request(reason
);
3819 * FIXME: should not return to device code in case
3820 * vm_stop() has been requested.
3822 if (cpu_single_env
) {
3823 cpu_exit(cpu_single_env
);
3824 cpu_single_env
->stop
= 1;
3835 static void host_main_loop_wait(int *timeout
)
3841 /* XXX: need to suppress polling by better using win32 events */
3843 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3844 ret
|= pe
->func(pe
->opaque
);
3848 WaitObjects
*w
= &wait_objects
;
3850 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3851 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3852 if (w
->func
[ret
- WAIT_OBJECT_0
])
3853 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3855 /* Check for additional signaled events */
3856 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3858 /* Check if event is signaled */
3859 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3860 if(ret2
== WAIT_OBJECT_0
) {
3862 w
->func
[i
](w
->opaque
[i
]);
3863 } else if (ret2
== WAIT_TIMEOUT
) {
3865 err
= GetLastError();
3866 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3869 } else if (ret
== WAIT_TIMEOUT
) {
3871 err
= GetLastError();
3872 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3879 static void host_main_loop_wait(int *timeout
)
3884 void main_loop_wait(int timeout
)
3886 IOHandlerRecord
*ioh
;
3887 fd_set rfds
, wfds
, xfds
;
3891 qemu_bh_update_timeout(&timeout
);
3893 host_main_loop_wait(&timeout
);
3895 /* poll any events */
3896 /* XXX: separate device handlers from system ones */
3901 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3905 (!ioh
->fd_read_poll
||
3906 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3907 FD_SET(ioh
->fd
, &rfds
);
3911 if (ioh
->fd_write
) {
3912 FD_SET(ioh
->fd
, &wfds
);
3918 tv
.tv_sec
= timeout
/ 1000;
3919 tv
.tv_usec
= (timeout
% 1000) * 1000;
3921 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3923 qemu_mutex_unlock_iothread();
3924 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3925 qemu_mutex_lock_iothread();
3927 IOHandlerRecord
**pioh
;
3929 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3930 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3931 ioh
->fd_read(ioh
->opaque
);
3932 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3933 FD_CLR(ioh
->fd
, &rfds
);
3935 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3936 ioh
->fd_write(ioh
->opaque
);
3940 /* remove deleted IO handlers */
3941 pioh
= &first_io_handler
;
3952 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3954 /* rearm timer, if not periodic */
3955 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3956 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3957 qemu_rearm_alarm_timer(alarm_timer
);
3960 /* vm time timers */
3962 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3963 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3964 qemu_get_clock(vm_clock
));
3967 /* real time timers */
3968 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3969 qemu_get_clock(rt_clock
));
3971 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3972 qemu_get_clock(host_clock
));
3974 /* Check bottom-halves last in case any of the earlier events triggered
3980 static int qemu_cpu_exec(CPUState
*env
)
3983 #ifdef CONFIG_PROFILER
3987 #ifdef CONFIG_PROFILER
3988 ti
= profile_getclock();
3993 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3994 env
->icount_decr
.u16
.low
= 0;
3995 env
->icount_extra
= 0;
3996 count
= qemu_next_deadline();
3997 count
= (count
+ (1 << icount_time_shift
) - 1)
3998 >> icount_time_shift
;
3999 qemu_icount
+= count
;
4000 decr
= (count
> 0xffff) ? 0xffff : count
;
4002 env
->icount_decr
.u16
.low
= decr
;
4003 env
->icount_extra
= count
;
4005 ret
= cpu_exec(env
);
4006 #ifdef CONFIG_PROFILER
4007 qemu_time
+= profile_getclock() - ti
;
4010 /* Fold pending instructions back into the
4011 instruction counter, and clear the interrupt flag. */
4012 qemu_icount
-= (env
->icount_decr
.u16
.low
4013 + env
->icount_extra
);
4014 env
->icount_decr
.u32
= 0;
4015 env
->icount_extra
= 0;
4020 static void tcg_cpu_exec(void)
4024 if (next_cpu
== NULL
)
4025 next_cpu
= first_cpu
;
4026 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4027 CPUState
*env
= cur_cpu
= next_cpu
;
4031 if (timer_alarm_pending
) {
4032 timer_alarm_pending
= 0;
4035 if (cpu_can_run(env
))
4036 ret
= qemu_cpu_exec(env
);
4037 if (ret
== EXCP_DEBUG
) {
4038 gdb_set_stop_cpu(env
);
4039 debug_requested
= 1;
4045 static int cpu_has_work(CPUState
*env
)
4053 if (qemu_cpu_has_work(env
))
4058 static int tcg_has_work(void)
4062 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4063 if (cpu_has_work(env
))
4068 static int qemu_calculate_timeout(void)
4070 #ifndef CONFIG_IOTHREAD
4075 else if (tcg_has_work())
4077 else if (!use_icount
)
4080 /* XXX: use timeout computed from timers */
4083 /* Advance virtual time to the next event. */
4084 if (use_icount
== 1) {
4085 /* When not using an adaptive execution frequency
4086 we tend to get badly out of sync with real time,
4087 so just delay for a reasonable amount of time. */
4090 delta
= cpu_get_icount() - cpu_get_clock();
4093 /* If virtual time is ahead of real time then just
4095 timeout
= (delta
/ 1000000) + 1;
4097 /* Wait for either IO to occur or the next
4099 add
= qemu_next_deadline();
4100 /* We advance the timer before checking for IO.
4101 Limit the amount we advance so that early IO
4102 activity won't get the guest too far ahead. */
4106 add
= (add
+ (1 << icount_time_shift
) - 1)
4107 >> icount_time_shift
;
4109 timeout
= delta
/ 1000000;
4116 #else /* CONFIG_IOTHREAD */
4121 static int vm_can_run(void)
4123 if (powerdown_requested
)
4125 if (reset_requested
)
4127 if (shutdown_requested
)
4129 if (debug_requested
)
4134 qemu_irq qemu_system_powerdown
;
4136 static void main_loop(void)
4140 if (kvm_enabled()) {
4142 cpu_disable_ticks();
4146 #ifdef CONFIG_IOTHREAD
4147 qemu_system_ready
= 1;
4148 qemu_cond_broadcast(&qemu_system_cond
);
4153 #ifdef CONFIG_PROFILER
4156 #ifndef CONFIG_IOTHREAD
4159 #ifdef CONFIG_PROFILER
4160 ti
= profile_getclock();
4162 main_loop_wait(qemu_calculate_timeout());
4163 #ifdef CONFIG_PROFILER
4164 dev_time
+= profile_getclock() - ti
;
4166 } while (vm_can_run());
4168 if (qemu_debug_requested())
4169 vm_stop(EXCP_DEBUG
);
4170 if (qemu_shutdown_requested()) {
4177 if (qemu_reset_requested()) {
4179 qemu_system_reset();
4182 if (qemu_powerdown_requested()) {
4183 qemu_irq_raise(qemu_system_powerdown
);
4185 if ((r
= qemu_vmstop_requested()))
4191 static void version(void)
4193 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4196 static void help(int exitcode
)
4199 printf("usage: %s [options] [disk_image]\n"
4201 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4203 #define DEF(option, opt_arg, opt_enum, opt_help) \
4205 #define DEFHEADING(text) stringify(text) "\n"
4206 #include "qemu-options.h"
4211 "During emulation, the following keys are useful:\n"
4212 "ctrl-alt-f toggle full screen\n"
4213 "ctrl-alt-n switch to virtual console 'n'\n"
4214 "ctrl-alt toggle mouse and keyboard grab\n"
4216 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4221 DEFAULT_NETWORK_SCRIPT
,
4222 DEFAULT_NETWORK_DOWN_SCRIPT
,
4224 DEFAULT_GDBSTUB_PORT
,
4229 #define HAS_ARG 0x0001
4232 #define DEF(option, opt_arg, opt_enum, opt_help) \
4234 #define DEFHEADING(text)
4235 #include "qemu-options.h"
4241 typedef struct QEMUOption
{
4247 static const QEMUOption qemu_options
[] = {
4248 { "h", 0, QEMU_OPTION_h
},
4249 #define DEF(option, opt_arg, opt_enum, opt_help) \
4250 { option, opt_arg, opt_enum },
4251 #define DEFHEADING(text)
4252 #include "qemu-options.h"
4260 struct soundhw soundhw
[] = {
4261 #ifdef HAS_AUDIO_CHOICE
4262 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4268 { .init_isa
= pcspk_audio_init
}
4275 "Creative Sound Blaster 16",
4278 { .init_isa
= SB16_init
}
4282 #ifdef CONFIG_CS4231A
4288 { .init_isa
= cs4231a_init
}
4296 "Yamaha YMF262 (OPL3)",
4298 "Yamaha YM3812 (OPL2)",
4302 { .init_isa
= Adlib_init
}
4309 "Gravis Ultrasound GF1",
4312 { .init_isa
= GUS_init
}
4319 "Intel 82801AA AC97 Audio",
4322 { .init_pci
= ac97_init
}
4326 #ifdef CONFIG_ES1370
4329 "ENSONIQ AudioPCI ES1370",
4332 { .init_pci
= es1370_init
}
4336 #endif /* HAS_AUDIO_CHOICE */
4338 { NULL
, NULL
, 0, 0, { NULL
} }
4341 static void select_soundhw (const char *optarg
)
4345 if (*optarg
== '?') {
4348 printf ("Valid sound card names (comma separated):\n");
4349 for (c
= soundhw
; c
->name
; ++c
) {
4350 printf ("%-11s %s\n", c
->name
, c
->descr
);
4352 printf ("\n-soundhw all will enable all of the above\n");
4353 exit (*optarg
!= '?');
4361 if (!strcmp (optarg
, "all")) {
4362 for (c
= soundhw
; c
->name
; ++c
) {
4370 e
= strchr (p
, ',');
4371 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4373 for (c
= soundhw
; c
->name
; ++c
) {
4374 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4383 "Unknown sound card name (too big to show)\n");
4386 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4391 p
+= l
+ (e
!= NULL
);
4395 goto show_valid_cards
;
4400 static void select_vgahw (const char *p
)
4404 vga_interface_type
= VGA_NONE
;
4405 if (strstart(p
, "std", &opts
)) {
4406 vga_interface_type
= VGA_STD
;
4407 } else if (strstart(p
, "cirrus", &opts
)) {
4408 vga_interface_type
= VGA_CIRRUS
;
4409 } else if (strstart(p
, "vmware", &opts
)) {
4410 vga_interface_type
= VGA_VMWARE
;
4411 } else if (strstart(p
, "xenfb", &opts
)) {
4412 vga_interface_type
= VGA_XENFB
;
4413 } else if (!strstart(p
, "none", &opts
)) {
4415 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4419 const char *nextopt
;
4421 if (strstart(opts
, ",retrace=", &nextopt
)) {
4423 if (strstart(opts
, "dumb", &nextopt
))
4424 vga_retrace_method
= VGA_RETRACE_DUMB
;
4425 else if (strstart(opts
, "precise", &nextopt
))
4426 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4427 else goto invalid_vga
;
4428 } else goto invalid_vga
;
4434 static int balloon_parse(const char *arg
)
4438 if (strcmp(arg
, "none") == 0) {
4442 if (!strncmp(arg
, "virtio", 6)) {
4443 if (arg
[6] == ',') {
4444 /* have params -> parse them */
4445 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4449 /* create empty opts */
4450 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4452 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4461 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4463 exit(STATUS_CONTROL_C_EXIT
);
4468 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4472 if(strlen(str
) != 36)
4475 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4476 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4477 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4483 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4491 static void termsig_handler(int signal
)
4493 qemu_system_shutdown_request();
4496 static void sigchld_handler(int signal
)
4498 waitpid(-1, NULL
, WNOHANG
);
4501 static void sighandler_setup(void)
4503 struct sigaction act
;
4505 memset(&act
, 0, sizeof(act
));
4506 act
.sa_handler
= termsig_handler
;
4507 sigaction(SIGINT
, &act
, NULL
);
4508 sigaction(SIGHUP
, &act
, NULL
);
4509 sigaction(SIGTERM
, &act
, NULL
);
4511 act
.sa_handler
= sigchld_handler
;
4512 act
.sa_flags
= SA_NOCLDSTOP
;
4513 sigaction(SIGCHLD
, &act
, NULL
);
4519 /* Look for support files in the same directory as the executable. */
4520 static char *find_datadir(const char *argv0
)
4526 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4533 while (p
!= buf
&& *p
!= '\\')
4536 if (access(buf
, R_OK
) == 0) {
4537 return qemu_strdup(buf
);
4543 /* Find a likely location for support files using the location of the binary.
4544 For installed binaries this will be "$bindir/../share/qemu". When
4545 running from the build tree this will be "$bindir/../pc-bios". */
4546 #define SHARE_SUFFIX "/share/qemu"
4547 #define BUILD_SUFFIX "/pc-bios"
4548 static char *find_datadir(const char *argv0
)
4556 #if defined(__linux__)
4559 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4565 #elif defined(__FreeBSD__)
4568 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4575 /* If we don't have any way of figuring out the actual executable
4576 location then try argv[0]. */
4578 p
= realpath(argv0
, buf
);
4586 max_len
= strlen(dir
) +
4587 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4588 res
= qemu_mallocz(max_len
);
4589 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4590 if (access(res
, R_OK
)) {
4591 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4592 if (access(res
, R_OK
)) {
4604 char *qemu_find_file(int type
, const char *name
)
4610 /* If name contains path separators then try it as a straight path. */
4611 if ((strchr(name
, '/') || strchr(name
, '\\'))
4612 && access(name
, R_OK
) == 0) {
4613 return qemu_strdup(name
);
4616 case QEMU_FILE_TYPE_BIOS
:
4619 case QEMU_FILE_TYPE_KEYMAP
:
4620 subdir
= "keymaps/";
4625 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4626 buf
= qemu_mallocz(len
);
4627 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4628 if (access(buf
, R_OK
)) {
4635 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4639 dev
= qdev_device_add(opts
);
4645 struct device_config
{
4647 DEV_USB
, /* -usbdevice */
4650 const char *cmdline
;
4651 QTAILQ_ENTRY(device_config
) next
;
4653 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4655 static void add_device_config(int type
, const char *cmdline
)
4657 struct device_config
*conf
;
4659 conf
= qemu_mallocz(sizeof(*conf
));
4661 conf
->cmdline
= cmdline
;
4662 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4665 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4667 struct device_config
*conf
;
4670 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4671 if (conf
->type
!= type
)
4673 rc
= func(conf
->cmdline
);
4680 int main(int argc
, char **argv
, char **envp
)
4682 const char *gdbstub_dev
= NULL
;
4683 uint32_t boot_devices_bitmap
= 0;
4685 int snapshot
, linux_boot
, net_boot
;
4686 const char *initrd_filename
;
4687 const char *kernel_filename
, *kernel_cmdline
;
4688 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4690 DisplayChangeListener
*dcl
;
4691 int cyls
, heads
, secs
, translation
;
4692 QemuOpts
*hda_opts
= NULL
, *opts
;
4694 const char *r
, *optarg
;
4695 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4696 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4697 int monitor_device_index
;
4698 const char *serial_devices
[MAX_SERIAL_PORTS
];
4699 int serial_device_index
;
4700 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4701 int parallel_device_index
;
4702 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4703 int virtio_console_index
;
4704 const char *loadvm
= NULL
;
4705 QEMUMachine
*machine
;
4706 const char *cpu_model
;
4711 const char *pid_file
= NULL
;
4712 const char *incoming
= NULL
;
4715 struct passwd
*pwd
= NULL
;
4716 const char *chroot_dir
= NULL
;
4717 const char *run_as
= NULL
;
4720 int show_vnc_port
= 0;
4724 qemu_errors_to_file(stderr
);
4725 qemu_cache_utils_init(envp
);
4727 QLIST_INIT (&vm_change_state_head
);
4730 struct sigaction act
;
4731 sigfillset(&act
.sa_mask
);
4733 act
.sa_handler
= SIG_IGN
;
4734 sigaction(SIGPIPE
, &act
, NULL
);
4737 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4738 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4739 QEMU to run on a single CPU */
4744 h
= GetCurrentProcess();
4745 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4746 for(i
= 0; i
< 32; i
++) {
4747 if (mask
& (1 << i
))
4752 SetProcessAffinityMask(h
, mask
);
4758 module_call_init(MODULE_INIT_MACHINE
);
4759 machine
= find_default_machine();
4761 initrd_filename
= NULL
;
4764 kernel_filename
= NULL
;
4765 kernel_cmdline
= "";
4766 cyls
= heads
= secs
= 0;
4767 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4769 serial_devices
[0] = "vc:80Cx24C";
4770 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4771 serial_devices
[i
] = NULL
;
4772 serial_device_index
= 0;
4774 parallel_devices
[0] = "vc:80Cx24C";
4775 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4776 parallel_devices
[i
] = NULL
;
4777 parallel_device_index
= 0;
4779 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4780 virtio_consoles
[i
] = NULL
;
4781 virtio_console_index
= 0;
4783 monitor_devices
[0] = "vc:80Cx24C";
4784 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4785 monitor_devices
[i
] = NULL
;
4787 monitor_device_index
= 0;
4789 for (i
= 0; i
< MAX_NODES
; i
++) {
4791 node_cpumask
[i
] = 0;
4794 assigned_devices_index
= 0;
4808 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4810 const QEMUOption
*popt
;
4813 /* Treat --foo the same as -foo. */
4816 popt
= qemu_options
;
4819 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4823 if (!strcmp(popt
->name
, r
+ 1))
4827 if (popt
->flags
& HAS_ARG
) {
4828 if (optind
>= argc
) {
4829 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4833 optarg
= argv
[optind
++];
4838 switch(popt
->index
) {
4840 machine
= find_machine(optarg
);
4843 printf("Supported machines are:\n");
4844 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4846 printf("%-10s %s (alias of %s)\n",
4847 m
->alias
, m
->desc
, m
->name
);
4848 printf("%-10s %s%s\n",
4850 m
->is_default
? " (default)" : "");
4852 exit(*optarg
!= '?');
4855 case QEMU_OPTION_cpu
:
4856 /* hw initialization will check this */
4857 if (*optarg
== '?') {
4858 /* XXX: implement xxx_cpu_list for targets that still miss it */
4859 #if defined(cpu_list)
4860 cpu_list(stdout
, &fprintf
);
4867 case QEMU_OPTION_initrd
:
4868 initrd_filename
= optarg
;
4870 case QEMU_OPTION_hda
:
4872 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4874 hda_opts
= drive_add(optarg
, HD_ALIAS
4875 ",cyls=%d,heads=%d,secs=%d%s",
4876 0, cyls
, heads
, secs
,
4877 translation
== BIOS_ATA_TRANSLATION_LBA
?
4879 translation
== BIOS_ATA_TRANSLATION_NONE
?
4880 ",trans=none" : "");
4882 case QEMU_OPTION_hdb
:
4883 case QEMU_OPTION_hdc
:
4884 case QEMU_OPTION_hdd
:
4885 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4887 case QEMU_OPTION_drive
:
4888 drive_add(NULL
, "%s", optarg
);
4890 case QEMU_OPTION_set
:
4891 if (qemu_set_option(optarg
) != 0)
4894 case QEMU_OPTION_mtdblock
:
4895 drive_add(optarg
, MTD_ALIAS
);
4897 case QEMU_OPTION_sd
:
4898 drive_add(optarg
, SD_ALIAS
);
4900 case QEMU_OPTION_pflash
:
4901 drive_add(optarg
, PFLASH_ALIAS
);
4903 case QEMU_OPTION_snapshot
:
4906 case QEMU_OPTION_hdachs
:
4910 cyls
= strtol(p
, (char **)&p
, 0);
4911 if (cyls
< 1 || cyls
> 16383)
4916 heads
= strtol(p
, (char **)&p
, 0);
4917 if (heads
< 1 || heads
> 16)
4922 secs
= strtol(p
, (char **)&p
, 0);
4923 if (secs
< 1 || secs
> 63)
4927 if (!strcmp(p
, "none"))
4928 translation
= BIOS_ATA_TRANSLATION_NONE
;
4929 else if (!strcmp(p
, "lba"))
4930 translation
= BIOS_ATA_TRANSLATION_LBA
;
4931 else if (!strcmp(p
, "auto"))
4932 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4935 } else if (*p
!= '\0') {
4937 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4940 if (hda_opts
!= NULL
) {
4942 snprintf(num
, sizeof(num
), "%d", cyls
);
4943 qemu_opt_set(hda_opts
, "cyls", num
);
4944 snprintf(num
, sizeof(num
), "%d", heads
);
4945 qemu_opt_set(hda_opts
, "heads", num
);
4946 snprintf(num
, sizeof(num
), "%d", secs
);
4947 qemu_opt_set(hda_opts
, "secs", num
);
4948 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4949 qemu_opt_set(hda_opts
, "trans", "lba");
4950 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4951 qemu_opt_set(hda_opts
, "trans", "none");
4955 case QEMU_OPTION_numa
:
4956 if (nb_numa_nodes
>= MAX_NODES
) {
4957 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4962 case QEMU_OPTION_nographic
:
4963 display_type
= DT_NOGRAPHIC
;
4965 #ifdef CONFIG_CURSES
4966 case QEMU_OPTION_curses
:
4967 display_type
= DT_CURSES
;
4970 case QEMU_OPTION_portrait
:
4973 case QEMU_OPTION_kernel
:
4974 kernel_filename
= optarg
;
4976 case QEMU_OPTION_append
:
4977 kernel_cmdline
= optarg
;
4979 case QEMU_OPTION_cdrom
:
4980 drive_add(optarg
, CDROM_ALIAS
);
4982 case QEMU_OPTION_boot
:
4984 static const char * const params
[] = {
4985 "order", "once", "menu", NULL
4987 char buf
[sizeof(boot_devices
)];
4988 char *standard_boot_devices
;
4991 if (!strchr(optarg
, '=')) {
4993 pstrcpy(buf
, sizeof(buf
), optarg
);
4994 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4996 "qemu: unknown boot parameter '%s' in '%s'\n",
5002 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5003 boot_devices_bitmap
= parse_bootdevices(buf
);
5004 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5007 if (get_param_value(buf
, sizeof(buf
),
5009 boot_devices_bitmap
|= parse_bootdevices(buf
);
5010 standard_boot_devices
= qemu_strdup(boot_devices
);
5011 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5012 qemu_register_reset(restore_boot_devices
,
5013 standard_boot_devices
);
5015 if (get_param_value(buf
, sizeof(buf
),
5017 if (!strcmp(buf
, "on")) {
5019 } else if (!strcmp(buf
, "off")) {
5023 "qemu: invalid option value '%s'\n",
5031 case QEMU_OPTION_fda
:
5032 case QEMU_OPTION_fdb
:
5033 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5036 case QEMU_OPTION_no_fd_bootchk
:
5040 case QEMU_OPTION_netdev
:
5041 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5045 case QEMU_OPTION_net
:
5046 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5051 case QEMU_OPTION_tftp
:
5052 legacy_tftp_prefix
= optarg
;
5054 case QEMU_OPTION_bootp
:
5055 legacy_bootp_filename
= optarg
;
5058 case QEMU_OPTION_smb
:
5059 if (net_slirp_smb(optarg
) < 0)
5063 case QEMU_OPTION_redir
:
5064 if (net_slirp_redir(optarg
) < 0)
5068 case QEMU_OPTION_bt
:
5069 add_device_config(DEV_BT
, optarg
);
5072 case QEMU_OPTION_audio_help
:
5076 case QEMU_OPTION_soundhw
:
5077 select_soundhw (optarg
);
5083 case QEMU_OPTION_version
:
5087 case QEMU_OPTION_m
: {
5091 value
= strtoul(optarg
, &ptr
, 10);
5093 case 0: case 'M': case 'm':
5100 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5104 /* On 32-bit hosts, QEMU is limited by virtual address space */
5105 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5106 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5109 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5110 fprintf(stderr
, "qemu: ram size too large\n");
5119 const CPULogItem
*item
;
5121 mask
= cpu_str_to_log_mask(optarg
);
5123 printf("Log items (comma separated):\n");
5124 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5125 printf("%-10s %s\n", item
->name
, item
->help
);
5133 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5135 case QEMU_OPTION_gdb
:
5136 gdbstub_dev
= optarg
;
5141 case QEMU_OPTION_bios
:
5144 case QEMU_OPTION_singlestep
:
5152 keyboard_layout
= optarg
;
5155 case QEMU_OPTION_localtime
:
5158 case QEMU_OPTION_vga
:
5159 select_vgahw (optarg
);
5161 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5167 w
= strtol(p
, (char **)&p
, 10);
5170 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5176 h
= strtol(p
, (char **)&p
, 10);
5181 depth
= strtol(p
, (char **)&p
, 10);
5182 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5183 depth
!= 24 && depth
!= 32)
5185 } else if (*p
== '\0') {
5186 depth
= graphic_depth
;
5193 graphic_depth
= depth
;
5197 case QEMU_OPTION_echr
:
5200 term_escape_char
= strtol(optarg
, &r
, 0);
5202 printf("Bad argument to echr\n");
5205 case QEMU_OPTION_monitor
:
5206 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5207 fprintf(stderr
, "qemu: too many monitor devices\n");
5210 monitor_devices
[monitor_device_index
] = optarg
;
5211 monitor_device_index
++;
5213 case QEMU_OPTION_chardev
:
5214 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5216 fprintf(stderr
, "parse error: %s\n", optarg
);
5219 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5223 case QEMU_OPTION_serial
:
5224 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5225 fprintf(stderr
, "qemu: too many serial ports\n");
5228 serial_devices
[serial_device_index
] = optarg
;
5229 serial_device_index
++;
5231 case QEMU_OPTION_watchdog
:
5234 "qemu: only one watchdog option may be given\n");
5239 case QEMU_OPTION_watchdog_action
:
5240 if (select_watchdog_action(optarg
) == -1) {
5241 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5245 case QEMU_OPTION_virtiocon
:
5246 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5247 fprintf(stderr
, "qemu: too many virtio consoles\n");
5250 virtio_consoles
[virtio_console_index
] = optarg
;
5251 virtio_console_index
++;
5253 case QEMU_OPTION_parallel
:
5254 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5255 fprintf(stderr
, "qemu: too many parallel ports\n");
5258 parallel_devices
[parallel_device_index
] = optarg
;
5259 parallel_device_index
++;
5261 case QEMU_OPTION_loadvm
:
5264 case QEMU_OPTION_full_screen
:
5268 case QEMU_OPTION_no_frame
:
5271 case QEMU_OPTION_alt_grab
:
5274 case QEMU_OPTION_ctrl_grab
:
5277 case QEMU_OPTION_no_quit
:
5280 case QEMU_OPTION_sdl
:
5281 display_type
= DT_SDL
;
5284 case QEMU_OPTION_pidfile
:
5288 case QEMU_OPTION_win2k_hack
:
5289 win2k_install_hack
= 1;
5291 case QEMU_OPTION_rtc_td_hack
:
5294 case QEMU_OPTION_acpitable
:
5295 if(acpi_table_add(optarg
) < 0) {
5296 fprintf(stderr
, "Wrong acpi table provided\n");
5300 case QEMU_OPTION_smbios
:
5301 if(smbios_entry_add(optarg
) < 0) {
5302 fprintf(stderr
, "Wrong smbios provided\n");
5309 case QEMU_OPTION_enable_kvm
:
5313 case QEMU_OPTION_no_kvm
:
5316 case QEMU_OPTION_no_kvm_irqchip
: {
5321 case QEMU_OPTION_no_kvm_pit
: {
5325 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5326 kvm_pit_reinject
= 0;
5329 case QEMU_OPTION_enable_nesting
: {
5333 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5334 case QEMU_OPTION_pcidevice
:
5335 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5336 fprintf(stderr
, "Too many assigned devices\n");
5339 assigned_devices
[assigned_devices_index
] = optarg
;
5340 assigned_devices_index
++;
5344 case QEMU_OPTION_usb
:
5347 case QEMU_OPTION_usbdevice
:
5349 add_device_config(DEV_USB
, optarg
);
5351 case QEMU_OPTION_device
:
5352 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5356 case QEMU_OPTION_smp
:
5359 fprintf(stderr
, "Invalid number of CPUs\n");
5362 if (max_cpus
< smp_cpus
) {
5363 fprintf(stderr
, "maxcpus must be equal to or greater than "
5367 if (max_cpus
> 255) {
5368 fprintf(stderr
, "Unsupported number of maxcpus\n");
5372 case QEMU_OPTION_vnc
:
5373 display_type
= DT_VNC
;
5374 vnc_display
= optarg
;
5377 case QEMU_OPTION_no_acpi
:
5380 case QEMU_OPTION_no_hpet
:
5383 case QEMU_OPTION_balloon
:
5384 if (balloon_parse(optarg
) < 0) {
5385 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5390 case QEMU_OPTION_no_reboot
:
5393 case QEMU_OPTION_no_shutdown
:
5396 case QEMU_OPTION_show_cursor
:
5399 case QEMU_OPTION_uuid
:
5400 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5401 fprintf(stderr
, "Fail to parse UUID string."
5402 " Wrong format.\n");
5407 case QEMU_OPTION_daemonize
:
5411 case QEMU_OPTION_option_rom
:
5412 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5413 fprintf(stderr
, "Too many option ROMs\n");
5416 option_rom
[nb_option_roms
] = optarg
;
5419 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5420 case QEMU_OPTION_semihosting
:
5421 semihosting_enabled
= 1;
5424 case QEMU_OPTION_tdf
:
5427 case QEMU_OPTION_kvm_shadow_memory
:
5428 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5430 case QEMU_OPTION_mempath
:
5434 case QEMU_OPTION_mem_prealloc
:
5435 mem_prealloc
= !mem_prealloc
;
5438 case QEMU_OPTION_name
:
5439 qemu_name
= qemu_strdup(optarg
);
5441 char *p
= strchr(qemu_name
, ',');
5444 if (strncmp(p
, "process=", 8)) {
5445 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5453 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5454 case QEMU_OPTION_prom_env
:
5455 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5456 fprintf(stderr
, "Too many prom variables\n");
5459 prom_envs
[nb_prom_envs
] = optarg
;
5464 case QEMU_OPTION_old_param
:
5468 case QEMU_OPTION_clock
:
5469 configure_alarms(optarg
);
5471 case QEMU_OPTION_startdate
:
5472 configure_rtc_date_offset(optarg
, 1);
5474 case QEMU_OPTION_rtc
:
5475 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5477 fprintf(stderr
, "parse error: %s\n", optarg
);
5480 configure_rtc(opts
);
5482 case QEMU_OPTION_tb_size
:
5483 tb_size
= strtol(optarg
, NULL
, 0);
5487 case QEMU_OPTION_icount
:
5489 if (strcmp(optarg
, "auto") == 0) {
5490 icount_time_shift
= -1;
5492 icount_time_shift
= strtol(optarg
, NULL
, 0);
5495 case QEMU_OPTION_incoming
:
5499 case QEMU_OPTION_chroot
:
5500 chroot_dir
= optarg
;
5502 case QEMU_OPTION_runas
:
5505 case QEMU_OPTION_nvram
:
5510 case QEMU_OPTION_xen_domid
:
5511 xen_domid
= atoi(optarg
);
5513 case QEMU_OPTION_xen_create
:
5514 xen_mode
= XEN_CREATE
;
5516 case QEMU_OPTION_xen_attach
:
5517 xen_mode
= XEN_ATTACH
;
5524 /* If no data_dir is specified then try to find it relative to the
5527 data_dir
= find_datadir(argv
[0]);
5529 /* If all else fails use the install patch specified when building. */
5531 data_dir
= CONFIG_QEMU_SHAREDIR
;
5535 * Default to max_cpus = smp_cpus, in case the user doesn't
5536 * specify a max_cpus value.
5539 max_cpus
= smp_cpus
;
5541 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5542 if (smp_cpus
> machine
->max_cpus
) {
5543 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5544 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5549 if (display_type
== DT_NOGRAPHIC
) {
5550 if (serial_device_index
== 0)
5551 serial_devices
[0] = "stdio";
5552 if (parallel_device_index
== 0)
5553 parallel_devices
[0] = "null";
5554 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5555 monitor_devices
[0] = "stdio";
5563 if (pipe(fds
) == -1)
5574 len
= read(fds
[0], &status
, 1);
5575 if (len
== -1 && (errno
== EINTR
))
5580 else if (status
== 1) {
5581 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5598 signal(SIGTSTP
, SIG_IGN
);
5599 signal(SIGTTOU
, SIG_IGN
);
5600 signal(SIGTTIN
, SIG_IGN
);
5603 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5606 write(fds
[1], &status
, 1);
5608 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5613 if (kvm_enabled()) {
5616 ret
= kvm_init(smp_cpus
);
5618 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
5619 fprintf(stderr
, "failed to initialize KVM\n");
5622 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5627 if (qemu_init_main_loop()) {
5628 fprintf(stderr
, "qemu_init_main_loop failed\n");
5631 linux_boot
= (kernel_filename
!= NULL
);
5633 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5634 fprintf(stderr
, "-append only allowed with -kernel option\n");
5638 if (!linux_boot
&& initrd_filename
!= NULL
) {
5639 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5644 /* Win32 doesn't support line-buffering and requires size >= 2 */
5645 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5648 if (init_timer_alarm() < 0) {
5649 fprintf(stderr
, "could not initialize alarm timer\n");
5652 if (use_icount
&& icount_time_shift
< 0) {
5654 /* 125MIPS seems a reasonable initial guess at the guest speed.
5655 It will be corrected fairly quickly anyway. */
5656 icount_time_shift
= 3;
5657 init_icount_adjust();
5664 if (net_init_clients() < 0) {
5668 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5669 net_set_boot_mask(net_boot
);
5671 /* init the bluetooth world */
5672 if (foreach_device_config(DEV_BT
, bt_parse
))
5675 /* init the memory */
5677 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5679 /* init the dynamic translator */
5680 cpu_exec_init_all(tb_size
* 1024 * 1024);
5684 /* we always create the cdrom drive, even if no disk is there */
5685 drive_add(NULL
, CDROM_ALIAS
);
5687 /* we always create at least one floppy */
5688 drive_add(NULL
, FD_ALIAS
, 0);
5690 /* we always create one sd slot, even if no card is in it */
5691 drive_add(NULL
, SD_ALIAS
);
5693 /* open the virtual block devices */
5695 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5696 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5699 vmstate_register(0, &vmstate_timers
,&timers_state
);
5700 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5702 /* Maintain compatibility with multiple stdio monitors */
5703 if (!strcmp(monitor_devices
[0],"stdio")) {
5704 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5705 const char *devname
= serial_devices
[i
];
5706 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5707 monitor_devices
[0] = NULL
;
5709 } else if (devname
&& !strcmp(devname
,"stdio")) {
5710 monitor_devices
[0] = NULL
;
5711 serial_devices
[i
] = "mon:stdio";
5717 if (nb_numa_nodes
> 0) {
5720 if (nb_numa_nodes
> smp_cpus
) {
5721 nb_numa_nodes
= smp_cpus
;
5724 /* If no memory size if given for any node, assume the default case
5725 * and distribute the available memory equally across all nodes
5727 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5728 if (node_mem
[i
] != 0)
5731 if (i
== nb_numa_nodes
) {
5732 uint64_t usedmem
= 0;
5734 /* On Linux, the each node's border has to be 8MB aligned,
5735 * the final node gets the rest.
5737 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5738 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5739 usedmem
+= node_mem
[i
];
5741 node_mem
[i
] = ram_size
- usedmem
;
5744 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5745 if (node_cpumask
[i
] != 0)
5748 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5749 * must cope with this anyway, because there are BIOSes out there in
5750 * real machines which also use this scheme.
5752 if (i
== nb_numa_nodes
) {
5753 for (i
= 0; i
< smp_cpus
; i
++) {
5754 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5759 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5760 const char *devname
= monitor_devices
[i
];
5761 if (devname
&& strcmp(devname
, "none")) {
5764 snprintf(label
, sizeof(label
), "monitor");
5766 snprintf(label
, sizeof(label
), "monitor%d", i
);
5768 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5769 if (!monitor_hds
[i
]) {
5770 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5777 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5778 const char *devname
= serial_devices
[i
];
5779 if (devname
&& strcmp(devname
, "none")) {
5781 snprintf(label
, sizeof(label
), "serial%d", i
);
5782 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5783 if (!serial_hds
[i
]) {
5784 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
5785 devname
, strerror(errno
));
5791 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5792 const char *devname
= parallel_devices
[i
];
5793 if (devname
&& strcmp(devname
, "none")) {
5795 snprintf(label
, sizeof(label
), "parallel%d", i
);
5796 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5797 if (!parallel_hds
[i
]) {
5798 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5799 devname
, strerror(errno
));
5805 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5806 const char *devname
= virtio_consoles
[i
];
5807 if (devname
&& strcmp(devname
, "none")) {
5809 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5810 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5811 if (!virtcon_hds
[i
]) {
5812 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5813 devname
, strerror(errno
));
5819 module_call_init(MODULE_INIT_DEVICE
);
5822 i
= select_watchdog(watchdog
);
5824 exit (i
== 1 ? 1 : 0);
5827 if (machine
->compat_props
) {
5828 qdev_prop_register_compat(machine
->compat_props
);
5830 machine
->init(ram_size
, boot_devices
,
5831 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5835 /* must be after terminal init, SDL library changes signal handlers */
5839 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5840 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5841 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5847 current_machine
= machine
;
5849 /* init USB devices */
5851 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5855 /* init generic devices */
5856 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5860 dumb_display_init();
5861 /* just use the first displaystate for the moment */
5864 if (display_type
== DT_DEFAULT
) {
5865 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5866 display_type
= DT_SDL
;
5868 display_type
= DT_VNC
;
5869 vnc_display
= "localhost:0,to=99";
5875 switch (display_type
) {
5878 #if defined(CONFIG_CURSES)
5880 curses_display_init(ds
, full_screen
);
5883 #if defined(CONFIG_SDL)
5885 sdl_display_init(ds
, full_screen
, no_frame
);
5887 #elif defined(CONFIG_COCOA)
5889 cocoa_display_init(ds
, full_screen
);
5893 vnc_display_init(ds
);
5894 if (vnc_display_open(ds
, vnc_display
) < 0)
5897 if (show_vnc_port
) {
5898 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5906 dcl
= ds
->listeners
;
5907 while (dcl
!= NULL
) {
5908 if (dcl
->dpy_refresh
!= NULL
) {
5909 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5910 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5915 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5916 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5917 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5920 text_consoles_set_display(display_state
);
5921 qemu_chr_initial_reset();
5923 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5924 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5925 monitor_init(monitor_hds
[i
],
5926 MONITOR_USE_READLINE
|
5927 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5931 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5932 const char *devname
= serial_devices
[i
];
5933 if (devname
&& strcmp(devname
, "none")) {
5934 if (strstart(devname
, "vc", 0))
5935 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5939 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5940 const char *devname
= parallel_devices
[i
];
5941 if (devname
&& strcmp(devname
, "none")) {
5942 if (strstart(devname
, "vc", 0))
5943 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5947 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5948 const char *devname
= virtio_consoles
[i
];
5949 if (virtcon_hds
[i
] && devname
) {
5950 if (strstart(devname
, "vc", 0))
5951 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5955 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5956 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5961 qdev_machine_creation_done();
5966 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5972 qemu_start_incoming_migration(incoming
);
5973 } else if (autostart
) {
5983 len
= write(fds
[1], &status
, 1);
5984 if (len
== -1 && (errno
== EINTR
))
5991 TFR(fd
= open("/dev/null", O_RDWR
));
5997 pwd
= getpwnam(run_as
);
5999 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6005 if (chroot(chroot_dir
) < 0) {
6006 fprintf(stderr
, "chroot failed\n");
6013 if (setgid(pwd
->pw_gid
) < 0) {
6014 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6017 if (setuid(pwd
->pw_uid
) < 0) {
6018 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6021 if (setuid(0) != -1) {
6022 fprintf(stderr
, "Dropping privileges failed\n");