4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
112 #include <sys/timeb.h>
113 #include <mmsystem.h>
114 #define getopt_long_only getopt_long
115 #define memalign(align, size) malloc(size)
119 #if defined(__APPLE__) || defined(main)
121 int qemu_main(int argc
, char **argv
, char **envp
);
122 int main(int argc
, char **argv
)
124 return qemu_main(argc
, argv
, NULL
);
127 #define main qemu_main
129 #endif /* CONFIG_SDL */
133 #define main qemu_main
134 #endif /* CONFIG_COCOA */
137 #include "hw/boards.h"
139 #include "hw/pcmcia.h"
141 #include "hw/audiodev.h"
145 #include "hw/watchdog.h"
146 #include "hw/smbios.h"
154 #include "qemu-timer.h"
155 #include "qemu-char.h"
156 #include "cache-utils.h"
159 #include "audio/audio.h"
160 #include "migration.h"
163 #include "qemu-option.h"
167 #include "exec-all.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 /* Max number of USB devices that can be specified on the commandline. */
179 #define MAX_USB_CMDLINE 8
181 /* Max number of bluetooth switches on the commandline. */
182 #define MAX_BT_CMDLINE 10
184 static const char *data_dir
;
185 const char *bios_name
= NULL
;
186 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
187 to store the VM snapshots */
188 DriveInfo drives_table
[MAX_DRIVES
+1];
190 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
191 static DisplayState
*display_state
;
192 DisplayType display_type
= DT_DEFAULT
;
193 const char* keyboard_layout
= NULL
;
194 int64_t ticks_per_sec
;
197 NICInfo nd_table
[MAX_NICS
];
200 static int rtc_utc
= 1;
201 static int rtc_date_offset
= -1; /* -1 means no change */
202 int vga_interface_type
= VGA_CIRRUS
;
204 int graphic_width
= 1024;
205 int graphic_height
= 768;
206 int graphic_depth
= 8;
208 int graphic_width
= 800;
209 int graphic_height
= 600;
210 int graphic_depth
= 15;
212 static int full_screen
= 0;
214 static int no_frame
= 0;
217 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
218 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
219 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
221 int win2k_install_hack
= 0;
227 const char *vnc_display
;
228 int acpi_enabled
= 1;
230 int virtio_balloon
= 1;
231 const char *virtio_balloon_devaddr
;
236 int graphic_rotate
= 0;
240 WatchdogTimerModel
*watchdog
= NULL
;
241 int watchdog_action
= WDT_RESET
;
242 const char *option_rom
[MAX_OPTION_ROMS
];
244 int semihosting_enabled
= 0;
248 const char *qemu_name
;
250 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
251 unsigned int nb_prom_envs
= 0;
252 const char *prom_envs
[MAX_PROM_ENVS
];
255 struct drive_opt drives_opt
[MAX_DRIVES
];
259 uint64_t node_mem
[MAX_NODES
];
260 uint64_t node_cpumask
[MAX_NODES
];
262 static CPUState
*cur_cpu
;
263 static CPUState
*next_cpu
;
264 static int timer_alarm_pending
= 1;
265 /* Conversion factor from emulated instructions to virtual clock ticks. */
266 static int icount_time_shift
;
267 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
268 #define MAX_ICOUNT_SHIFT 10
269 /* Compensate for varying guest execution speed. */
270 static int64_t qemu_icount_bias
;
271 static QEMUTimer
*icount_rt_timer
;
272 static QEMUTimer
*icount_vm_timer
;
273 static QEMUTimer
*nographic_timer
;
275 uint8_t qemu_uuid
[16];
277 static QEMUBootSetHandler
*boot_set_handler
;
278 static void *boot_set_opaque
;
280 /***********************************************************/
281 /* x86 ISA bus support */
283 target_phys_addr_t isa_mem_base
= 0;
286 /***********************************************************/
287 void hw_error(const char *fmt
, ...)
293 fprintf(stderr
, "qemu: hardware error: ");
294 vfprintf(stderr
, fmt
, ap
);
295 fprintf(stderr
, "\n");
296 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
297 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
299 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
301 cpu_dump_state(env
, stderr
, fprintf
, 0);
308 static void set_proc_name(const char *s
)
310 #if defined(__linux__) && defined(PR_SET_NAME)
314 name
[sizeof(name
) - 1] = 0;
315 strncpy(name
, s
, sizeof(name
));
316 /* Could rewrite argv[0] too, but that's a bit more complicated.
317 This simple way is enough for `top'. */
318 prctl(PR_SET_NAME
, name
);
325 static QEMUBalloonEvent
*qemu_balloon_event
;
326 void *qemu_balloon_event_opaque
;
328 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
330 qemu_balloon_event
= func
;
331 qemu_balloon_event_opaque
= opaque
;
334 void qemu_balloon(ram_addr_t target
)
336 if (qemu_balloon_event
)
337 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
340 ram_addr_t
qemu_balloon_status(void)
342 if (qemu_balloon_event
)
343 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
347 /***********************************************************/
350 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
351 static void *qemu_put_kbd_event_opaque
;
352 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
353 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
355 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
357 qemu_put_kbd_event_opaque
= opaque
;
358 qemu_put_kbd_event
= func
;
361 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
362 void *opaque
, int absolute
,
365 QEMUPutMouseEntry
*s
, *cursor
;
367 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
369 s
->qemu_put_mouse_event
= func
;
370 s
->qemu_put_mouse_event_opaque
= opaque
;
371 s
->qemu_put_mouse_event_absolute
= absolute
;
372 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
375 if (!qemu_put_mouse_event_head
) {
376 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
380 cursor
= qemu_put_mouse_event_head
;
381 while (cursor
->next
!= NULL
)
382 cursor
= cursor
->next
;
385 qemu_put_mouse_event_current
= s
;
390 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
392 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
394 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
397 cursor
= qemu_put_mouse_event_head
;
398 while (cursor
!= NULL
&& cursor
!= entry
) {
400 cursor
= cursor
->next
;
403 if (cursor
== NULL
) // does not exist or list empty
405 else if (prev
== NULL
) { // entry is head
406 qemu_put_mouse_event_head
= cursor
->next
;
407 if (qemu_put_mouse_event_current
== entry
)
408 qemu_put_mouse_event_current
= cursor
->next
;
409 qemu_free(entry
->qemu_put_mouse_event_name
);
414 prev
->next
= entry
->next
;
416 if (qemu_put_mouse_event_current
== entry
)
417 qemu_put_mouse_event_current
= prev
;
419 qemu_free(entry
->qemu_put_mouse_event_name
);
423 void kbd_put_keycode(int keycode
)
425 if (qemu_put_kbd_event
) {
426 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
430 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
432 QEMUPutMouseEvent
*mouse_event
;
433 void *mouse_event_opaque
;
436 if (!qemu_put_mouse_event_current
) {
441 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
443 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
446 if (graphic_rotate
) {
447 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
450 width
= graphic_width
- 1;
451 mouse_event(mouse_event_opaque
,
452 width
- dy
, dx
, dz
, buttons_state
);
454 mouse_event(mouse_event_opaque
,
455 dx
, dy
, dz
, buttons_state
);
459 int kbd_mouse_is_absolute(void)
461 if (!qemu_put_mouse_event_current
)
464 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
467 void do_info_mice(Monitor
*mon
)
469 QEMUPutMouseEntry
*cursor
;
472 if (!qemu_put_mouse_event_head
) {
473 monitor_printf(mon
, "No mouse devices connected\n");
477 monitor_printf(mon
, "Mouse devices available:\n");
478 cursor
= qemu_put_mouse_event_head
;
479 while (cursor
!= NULL
) {
480 monitor_printf(mon
, "%c Mouse #%d: %s\n",
481 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
482 index
, cursor
->qemu_put_mouse_event_name
);
484 cursor
= cursor
->next
;
488 void do_mouse_set(Monitor
*mon
, int index
)
490 QEMUPutMouseEntry
*cursor
;
493 if (!qemu_put_mouse_event_head
) {
494 monitor_printf(mon
, "No mouse devices connected\n");
498 cursor
= qemu_put_mouse_event_head
;
499 while (cursor
!= NULL
&& index
!= i
) {
501 cursor
= cursor
->next
;
505 qemu_put_mouse_event_current
= cursor
;
507 monitor_printf(mon
, "Mouse at given index not found\n");
510 /* compute with 96 bit intermediate result: (a*b)/c */
511 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
516 #ifdef WORDS_BIGENDIAN
526 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
527 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
530 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
534 /***********************************************************/
535 /* real time host monotonic timer */
537 #define QEMU_TIMER_BASE 1000000000LL
541 static int64_t clock_freq
;
543 static void init_get_clock(void)
547 ret
= QueryPerformanceFrequency(&freq
);
549 fprintf(stderr
, "Could not calibrate ticks\n");
552 clock_freq
= freq
.QuadPart
;
555 static int64_t get_clock(void)
558 QueryPerformanceCounter(&ti
);
559 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
564 static int use_rt_clock
;
566 static void init_get_clock(void)
569 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
570 || defined(__DragonFly__)
573 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
580 static int64_t get_clock(void)
582 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
583 || defined(__DragonFly__)
586 clock_gettime(CLOCK_MONOTONIC
, &ts
);
587 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
591 /* XXX: using gettimeofday leads to problems if the date
592 changes, so it should be avoided. */
594 gettimeofday(&tv
, NULL
);
595 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
600 /* Return the virtual CPU time, based on the instruction counter. */
601 static int64_t cpu_get_icount(void)
604 CPUState
*env
= cpu_single_env
;;
605 icount
= qemu_icount
;
608 fprintf(stderr
, "Bad clock read\n");
609 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
611 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
614 /***********************************************************/
615 /* guest cycle counter */
617 static int64_t cpu_ticks_prev
;
618 static int64_t cpu_ticks_offset
;
619 static int64_t cpu_clock_offset
;
620 static int cpu_ticks_enabled
;
622 /* return the host CPU cycle counter and handle stop/restart */
623 int64_t cpu_get_ticks(void)
626 return cpu_get_icount();
628 if (!cpu_ticks_enabled
) {
629 return cpu_ticks_offset
;
632 ticks
= cpu_get_real_ticks();
633 if (cpu_ticks_prev
> ticks
) {
634 /* Note: non increasing ticks may happen if the host uses
636 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
638 cpu_ticks_prev
= ticks
;
639 return ticks
+ cpu_ticks_offset
;
643 /* return the host CPU monotonic timer and handle stop/restart */
644 static int64_t cpu_get_clock(void)
647 if (!cpu_ticks_enabled
) {
648 return cpu_clock_offset
;
651 return ti
+ cpu_clock_offset
;
655 /* enable cpu_get_ticks() */
656 void cpu_enable_ticks(void)
658 if (!cpu_ticks_enabled
) {
659 cpu_ticks_offset
-= cpu_get_real_ticks();
660 cpu_clock_offset
-= get_clock();
661 cpu_ticks_enabled
= 1;
665 /* disable cpu_get_ticks() : the clock is stopped. You must not call
666 cpu_get_ticks() after that. */
667 void cpu_disable_ticks(void)
669 if (cpu_ticks_enabled
) {
670 cpu_ticks_offset
= cpu_get_ticks();
671 cpu_clock_offset
= cpu_get_clock();
672 cpu_ticks_enabled
= 0;
676 /***********************************************************/
679 #define QEMU_TIMER_REALTIME 0
680 #define QEMU_TIMER_VIRTUAL 1
684 /* XXX: add frequency */
692 struct QEMUTimer
*next
;
695 struct qemu_alarm_timer
{
699 int (*start
)(struct qemu_alarm_timer
*t
);
700 void (*stop
)(struct qemu_alarm_timer
*t
);
701 void (*rearm
)(struct qemu_alarm_timer
*t
);
705 #define ALARM_FLAG_DYNTICKS 0x1
706 #define ALARM_FLAG_EXPIRED 0x2
708 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
710 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
713 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
715 if (!alarm_has_dynticks(t
))
721 /* TODO: MIN_TIMER_REARM_US should be optimized */
722 #define MIN_TIMER_REARM_US 250
724 static struct qemu_alarm_timer
*alarm_timer
;
728 struct qemu_alarm_win32
{
731 } alarm_win32_data
= {0, -1};
733 static int win32_start_timer(struct qemu_alarm_timer
*t
);
734 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
735 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
739 static int unix_start_timer(struct qemu_alarm_timer
*t
);
740 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
744 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
745 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
746 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
748 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
749 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
751 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
752 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
754 #endif /* __linux__ */
758 /* Correlation between real and virtual time is always going to be
759 fairly approximate, so ignore small variation.
760 When the guest is idle real and virtual time will be aligned in
762 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
764 static void icount_adjust(void)
769 static int64_t last_delta
;
770 /* If the VM is not running, then do nothing. */
774 cur_time
= cpu_get_clock();
775 cur_icount
= qemu_get_clock(vm_clock
);
776 delta
= cur_icount
- cur_time
;
777 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
779 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
780 && icount_time_shift
> 0) {
781 /* The guest is getting too far ahead. Slow time down. */
785 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
786 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
787 /* The guest is getting too far behind. Speed time up. */
791 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
794 static void icount_adjust_rt(void * opaque
)
796 qemu_mod_timer(icount_rt_timer
,
797 qemu_get_clock(rt_clock
) + 1000);
801 static void icount_adjust_vm(void * opaque
)
803 qemu_mod_timer(icount_vm_timer
,
804 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
808 static void init_icount_adjust(void)
810 /* Have both realtime and virtual time triggers for speed adjustment.
811 The realtime trigger catches emulated time passing too slowly,
812 the virtual time trigger catches emulated time passing too fast.
813 Realtime triggers occur even when idle, so use them less frequently
815 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
816 qemu_mod_timer(icount_rt_timer
,
817 qemu_get_clock(rt_clock
) + 1000);
818 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
819 qemu_mod_timer(icount_vm_timer
,
820 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
823 static struct qemu_alarm_timer alarm_timers
[] = {
826 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
827 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
828 /* HPET - if available - is preferred */
829 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
830 /* ...otherwise try RTC */
831 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
833 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
835 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
836 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
837 {"win32", 0, win32_start_timer
,
838 win32_stop_timer
, NULL
, &alarm_win32_data
},
843 static void show_available_alarms(void)
847 printf("Available alarm timers, in order of precedence:\n");
848 for (i
= 0; alarm_timers
[i
].name
; i
++)
849 printf("%s\n", alarm_timers
[i
].name
);
852 static void configure_alarms(char const *opt
)
856 int count
= ARRAY_SIZE(alarm_timers
) - 1;
859 struct qemu_alarm_timer tmp
;
861 if (!strcmp(opt
, "?")) {
862 show_available_alarms();
868 /* Reorder the array */
869 name
= strtok(arg
, ",");
871 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
872 if (!strcmp(alarm_timers
[i
].name
, name
))
877 fprintf(stderr
, "Unknown clock %s\n", name
);
886 tmp
= alarm_timers
[i
];
887 alarm_timers
[i
] = alarm_timers
[cur
];
888 alarm_timers
[cur
] = tmp
;
892 name
= strtok(NULL
, ",");
898 /* Disable remaining timers */
899 for (i
= cur
; i
< count
; i
++)
900 alarm_timers
[i
].name
= NULL
;
902 show_available_alarms();
910 static QEMUTimer
*active_timers
[2];
912 static QEMUClock
*qemu_new_clock(int type
)
915 clock
= qemu_mallocz(sizeof(QEMUClock
));
920 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
924 ts
= qemu_mallocz(sizeof(QEMUTimer
));
931 void qemu_free_timer(QEMUTimer
*ts
)
936 /* stop a timer, but do not dealloc it */
937 void qemu_del_timer(QEMUTimer
*ts
)
941 /* NOTE: this code must be signal safe because
942 qemu_timer_expired() can be called from a signal. */
943 pt
= &active_timers
[ts
->clock
->type
];
956 /* modify the current timer so that it will be fired when current_time
957 >= expire_time. The corresponding callback will be called. */
958 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
964 /* add the timer in the sorted list */
965 /* NOTE: this code must be signal safe because
966 qemu_timer_expired() can be called from a signal. */
967 pt
= &active_timers
[ts
->clock
->type
];
972 if (t
->expire_time
> expire_time
)
976 ts
->expire_time
= expire_time
;
980 /* Rearm if necessary */
981 if (pt
== &active_timers
[ts
->clock
->type
]) {
982 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
983 qemu_rearm_alarm_timer(alarm_timer
);
985 /* Interrupt execution to force deadline recalculation. */
991 int qemu_timer_pending(QEMUTimer
*ts
)
994 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1001 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1005 return (timer_head
->expire_time
<= current_time
);
1008 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1014 if (!ts
|| ts
->expire_time
> current_time
)
1016 /* remove timer from the list before calling the callback */
1017 *ptimer_head
= ts
->next
;
1020 /* run the callback (the timer list can be modified) */
1025 int64_t qemu_get_clock(QEMUClock
*clock
)
1027 switch(clock
->type
) {
1028 case QEMU_TIMER_REALTIME
:
1029 return get_clock() / 1000000;
1031 case QEMU_TIMER_VIRTUAL
:
1033 return cpu_get_icount();
1035 return cpu_get_clock();
1040 static void init_timers(void)
1043 ticks_per_sec
= QEMU_TIMER_BASE
;
1044 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1045 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1049 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1051 uint64_t expire_time
;
1053 if (qemu_timer_pending(ts
)) {
1054 expire_time
= ts
->expire_time
;
1058 qemu_put_be64(f
, expire_time
);
1061 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1063 uint64_t expire_time
;
1065 expire_time
= qemu_get_be64(f
);
1066 if (expire_time
!= -1) {
1067 qemu_mod_timer(ts
, expire_time
);
1073 static void timer_save(QEMUFile
*f
, void *opaque
)
1075 if (cpu_ticks_enabled
) {
1076 hw_error("cannot save state if virtual timers are running");
1078 qemu_put_be64(f
, cpu_ticks_offset
);
1079 qemu_put_be64(f
, ticks_per_sec
);
1080 qemu_put_be64(f
, cpu_clock_offset
);
1083 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1085 if (version_id
!= 1 && version_id
!= 2)
1087 if (cpu_ticks_enabled
) {
1090 cpu_ticks_offset
=qemu_get_be64(f
);
1091 ticks_per_sec
=qemu_get_be64(f
);
1092 if (version_id
== 2) {
1093 cpu_clock_offset
=qemu_get_be64(f
);
1098 static void qemu_event_increment(void);
1101 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1102 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1105 static void host_alarm_handler(int host_signum
)
1109 #define DISP_FREQ 1000
1111 static int64_t delta_min
= INT64_MAX
;
1112 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1114 ti
= qemu_get_clock(vm_clock
);
1115 if (last_clock
!= 0) {
1116 delta
= ti
- last_clock
;
1117 if (delta
< delta_min
)
1119 if (delta
> delta_max
)
1122 if (++count
== DISP_FREQ
) {
1123 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1124 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1125 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1126 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1127 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1129 delta_min
= INT64_MAX
;
1137 if (alarm_has_dynticks(alarm_timer
) ||
1139 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1140 qemu_get_clock(vm_clock
))) ||
1141 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1142 qemu_get_clock(rt_clock
))) {
1143 qemu_event_increment();
1144 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1146 #ifndef CONFIG_IOTHREAD
1148 /* stop the currently executing cpu because a timer occured */
1151 if (next_cpu
->kqemu_enabled
) {
1152 kqemu_cpu_interrupt(next_cpu
);
1157 timer_alarm_pending
= 1;
1158 qemu_notify_event();
1162 static int64_t qemu_next_deadline(void)
1166 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1167 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1168 qemu_get_clock(vm_clock
);
1170 /* To avoid problems with overflow limit this to 2^32. */
1180 #if defined(__linux__) || defined(_WIN32)
1181 static uint64_t qemu_next_deadline_dyntick(void)
1189 delta
= (qemu_next_deadline() + 999) / 1000;
1191 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1192 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1193 qemu_get_clock(rt_clock
))*1000;
1194 if (rtdelta
< delta
)
1198 if (delta
< MIN_TIMER_REARM_US
)
1199 delta
= MIN_TIMER_REARM_US
;
1207 /* Sets a specific flag */
1208 static int fcntl_setfl(int fd
, int flag
)
1212 flags
= fcntl(fd
, F_GETFL
);
1216 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1222 #if defined(__linux__)
1224 #define RTC_FREQ 1024
1226 static void enable_sigio_timer(int fd
)
1228 struct sigaction act
;
1231 sigfillset(&act
.sa_mask
);
1233 act
.sa_handler
= host_alarm_handler
;
1235 sigaction(SIGIO
, &act
, NULL
);
1236 fcntl_setfl(fd
, O_ASYNC
);
1237 fcntl(fd
, F_SETOWN
, getpid());
1240 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1242 struct hpet_info info
;
1245 fd
= open("/dev/hpet", O_RDONLY
);
1250 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1252 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1253 "error, but for better emulation accuracy type:\n"
1254 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1258 /* Check capabilities */
1259 r
= ioctl(fd
, HPET_INFO
, &info
);
1263 /* Enable periodic mode */
1264 r
= ioctl(fd
, HPET_EPI
, 0);
1265 if (info
.hi_flags
&& (r
< 0))
1268 /* Enable interrupt */
1269 r
= ioctl(fd
, HPET_IE_ON
, 0);
1273 enable_sigio_timer(fd
);
1274 t
->priv
= (void *)(long)fd
;
1282 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1284 int fd
= (long)t
->priv
;
1289 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1292 unsigned long current_rtc_freq
= 0;
1294 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1297 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1298 if (current_rtc_freq
!= RTC_FREQ
&&
1299 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1300 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1301 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1302 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1305 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1311 enable_sigio_timer(rtc_fd
);
1313 t
->priv
= (void *)(long)rtc_fd
;
1318 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1320 int rtc_fd
= (long)t
->priv
;
1325 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1329 struct sigaction act
;
1331 sigfillset(&act
.sa_mask
);
1333 act
.sa_handler
= host_alarm_handler
;
1335 sigaction(SIGALRM
, &act
, NULL
);
1338 * Initialize ev struct to 0 to avoid valgrind complaining
1339 * about uninitialized data in timer_create call
1341 memset(&ev
, 0, sizeof(ev
));
1342 ev
.sigev_value
.sival_int
= 0;
1343 ev
.sigev_notify
= SIGEV_SIGNAL
;
1344 ev
.sigev_signo
= SIGALRM
;
1346 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1347 perror("timer_create");
1349 /* disable dynticks */
1350 fprintf(stderr
, "Dynamic Ticks disabled\n");
1355 t
->priv
= (void *)(long)host_timer
;
1360 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1362 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1364 timer_delete(host_timer
);
1367 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1369 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1370 struct itimerspec timeout
;
1371 int64_t nearest_delta_us
= INT64_MAX
;
1374 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1375 !active_timers
[QEMU_TIMER_VIRTUAL
])
1378 nearest_delta_us
= qemu_next_deadline_dyntick();
1380 /* check whether a timer is already running */
1381 if (timer_gettime(host_timer
, &timeout
)) {
1383 fprintf(stderr
, "Internal timer error: aborting\n");
1386 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1387 if (current_us
&& current_us
<= nearest_delta_us
)
1390 timeout
.it_interval
.tv_sec
= 0;
1391 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1392 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1393 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1394 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1396 fprintf(stderr
, "Internal timer error: aborting\n");
1401 #endif /* defined(__linux__) */
1403 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1405 struct sigaction act
;
1406 struct itimerval itv
;
1410 sigfillset(&act
.sa_mask
);
1412 act
.sa_handler
= host_alarm_handler
;
1414 sigaction(SIGALRM
, &act
, NULL
);
1416 itv
.it_interval
.tv_sec
= 0;
1417 /* for i386 kernel 2.6 to get 1 ms */
1418 itv
.it_interval
.tv_usec
= 999;
1419 itv
.it_value
.tv_sec
= 0;
1420 itv
.it_value
.tv_usec
= 10 * 1000;
1422 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1429 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1431 struct itimerval itv
;
1433 memset(&itv
, 0, sizeof(itv
));
1434 setitimer(ITIMER_REAL
, &itv
, NULL
);
1437 #endif /* !defined(_WIN32) */
1442 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1445 struct qemu_alarm_win32
*data
= t
->priv
;
1448 memset(&tc
, 0, sizeof(tc
));
1449 timeGetDevCaps(&tc
, sizeof(tc
));
1451 if (data
->period
< tc
.wPeriodMin
)
1452 data
->period
= tc
.wPeriodMin
;
1454 timeBeginPeriod(data
->period
);
1456 flags
= TIME_CALLBACK_FUNCTION
;
1457 if (alarm_has_dynticks(t
))
1458 flags
|= TIME_ONESHOT
;
1460 flags
|= TIME_PERIODIC
;
1462 data
->timerId
= timeSetEvent(1, // interval (ms)
1463 data
->period
, // resolution
1464 host_alarm_handler
, // function
1465 (DWORD
)t
, // parameter
1468 if (!data
->timerId
) {
1469 perror("Failed to initialize win32 alarm timer");
1470 timeEndPeriod(data
->period
);
1477 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1479 struct qemu_alarm_win32
*data
= t
->priv
;
1481 timeKillEvent(data
->timerId
);
1482 timeEndPeriod(data
->period
);
1485 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1487 struct qemu_alarm_win32
*data
= t
->priv
;
1488 uint64_t nearest_delta_us
;
1490 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1491 !active_timers
[QEMU_TIMER_VIRTUAL
])
1494 nearest_delta_us
= qemu_next_deadline_dyntick();
1495 nearest_delta_us
/= 1000;
1497 timeKillEvent(data
->timerId
);
1499 data
->timerId
= timeSetEvent(1,
1503 TIME_ONESHOT
| TIME_PERIODIC
);
1505 if (!data
->timerId
) {
1506 perror("Failed to re-arm win32 alarm timer");
1508 timeEndPeriod(data
->period
);
1515 static int init_timer_alarm(void)
1517 struct qemu_alarm_timer
*t
= NULL
;
1520 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1521 t
= &alarm_timers
[i
];
1541 static void quit_timers(void)
1543 alarm_timer
->stop(alarm_timer
);
1547 /***********************************************************/
1548 /* host time/date access */
1549 void qemu_get_timedate(struct tm
*tm
, int offset
)
1556 if (rtc_date_offset
== -1) {
1560 ret
= localtime(&ti
);
1562 ti
-= rtc_date_offset
;
1566 memcpy(tm
, ret
, sizeof(struct tm
));
1569 int qemu_timedate_diff(struct tm
*tm
)
1573 if (rtc_date_offset
== -1)
1575 seconds
= mktimegm(tm
);
1577 seconds
= mktime(tm
);
1579 seconds
= mktimegm(tm
) + rtc_date_offset
;
1581 return seconds
- time(NULL
);
1585 static void socket_cleanup(void)
1590 static int socket_init(void)
1595 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1597 err
= WSAGetLastError();
1598 fprintf(stderr
, "WSAStartup: %d\n", err
);
1601 atexit(socket_cleanup
);
1606 int get_next_param_value(char *buf
, int buf_size
,
1607 const char *tag
, const char **pstr
)
1614 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1618 if (!strcmp(tag
, option
)) {
1619 *pstr
= get_opt_value(buf
, buf_size
, p
);
1620 if (**pstr
== ',') {
1625 p
= get_opt_value(NULL
, 0, p
);
1634 int get_param_value(char *buf
, int buf_size
,
1635 const char *tag
, const char *str
)
1637 return get_next_param_value(buf
, buf_size
, tag
, &str
);
1640 int check_params(char *buf
, int buf_size
,
1641 const char * const *params
, const char *str
)
1647 while (*p
!= '\0') {
1648 p
= get_opt_name(buf
, buf_size
, p
, '=');
1653 for (i
= 0; params
[i
] != NULL
; i
++) {
1654 if (!strcmp(params
[i
], buf
)) {
1658 if (params
[i
] == NULL
) {
1661 p
= get_opt_value(NULL
, 0, p
);
1670 /***********************************************************/
1671 /* Bluetooth support */
1674 static struct HCIInfo
*hci_table
[MAX_NICS
];
1676 static struct bt_vlan_s
{
1677 struct bt_scatternet_s net
;
1679 struct bt_vlan_s
*next
;
1682 /* find or alloc a new bluetooth "VLAN" */
1683 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1685 struct bt_vlan_s
**pvlan
, *vlan
;
1686 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1690 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1692 pvlan
= &first_bt_vlan
;
1693 while (*pvlan
!= NULL
)
1694 pvlan
= &(*pvlan
)->next
;
1699 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1703 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1708 static struct HCIInfo null_hci
= {
1709 .cmd_send
= null_hci_send
,
1710 .sco_send
= null_hci_send
,
1711 .acl_send
= null_hci_send
,
1712 .bdaddr_set
= null_hci_addr_set
,
1715 struct HCIInfo
*qemu_next_hci(void)
1717 if (cur_hci
== nb_hcis
)
1720 return hci_table
[cur_hci
++];
1723 static struct HCIInfo
*hci_init(const char *str
)
1726 struct bt_scatternet_s
*vlan
= 0;
1728 if (!strcmp(str
, "null"))
1731 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1733 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1734 else if (!strncmp(str
, "hci", 3)) {
1737 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1738 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1743 vlan
= qemu_find_bt_vlan(0);
1745 return bt_new_hci(vlan
);
1748 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1753 static int bt_hci_parse(const char *str
)
1755 struct HCIInfo
*hci
;
1758 if (nb_hcis
>= MAX_NICS
) {
1759 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1763 hci
= hci_init(str
);
1772 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1773 hci
->bdaddr_set(hci
, bdaddr
.b
);
1775 hci_table
[nb_hcis
++] = hci
;
1780 static void bt_vhci_add(int vlan_id
)
1782 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1785 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1786 "an empty scatternet %i\n", vlan_id
);
1788 bt_vhci_init(bt_new_hci(vlan
));
1791 static struct bt_device_s
*bt_device_add(const char *opt
)
1793 struct bt_scatternet_s
*vlan
;
1795 char *endp
= strstr(opt
, ",vlan=");
1796 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1799 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1802 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1804 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1809 vlan
= qemu_find_bt_vlan(vlan_id
);
1812 fprintf(stderr
, "qemu: warning: adding a slave device to "
1813 "an empty scatternet %i\n", vlan_id
);
1815 if (!strcmp(devname
, "keyboard"))
1816 return bt_keyboard_init(vlan
);
1818 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1822 static int bt_parse(const char *opt
)
1824 const char *endp
, *p
;
1827 if (strstart(opt
, "hci", &endp
)) {
1828 if (!*endp
|| *endp
== ',') {
1830 if (!strstart(endp
, ",vlan=", 0))
1833 return bt_hci_parse(opt
);
1835 } else if (strstart(opt
, "vhci", &endp
)) {
1836 if (!*endp
|| *endp
== ',') {
1838 if (strstart(endp
, ",vlan=", &p
)) {
1839 vlan
= strtol(p
, (char **) &endp
, 0);
1841 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1845 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1854 } else if (strstart(opt
, "device:", &endp
))
1855 return !bt_device_add(endp
);
1857 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1861 /***********************************************************/
1862 /* QEMU Block devices */
1864 #define HD_ALIAS "index=%d,media=disk"
1865 #define CDROM_ALIAS "index=2,media=cdrom"
1866 #define FD_ALIAS "index=%d,if=floppy"
1867 #define PFLASH_ALIAS "if=pflash"
1868 #define MTD_ALIAS "if=mtd"
1869 #define SD_ALIAS "index=0,if=sd"
1871 static int drive_opt_get_free_idx(void)
1875 for (index
= 0; index
< MAX_DRIVES
; index
++)
1876 if (!drives_opt
[index
].used
) {
1877 drives_opt
[index
].used
= 1;
1884 static int drive_get_free_idx(void)
1888 for (index
= 0; index
< MAX_DRIVES
; index
++)
1889 if (!drives_table
[index
].used
) {
1890 drives_table
[index
].used
= 1;
1897 int drive_add(const char *file
, const char *fmt
, ...)
1900 int index
= drive_opt_get_free_idx();
1902 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
1903 fprintf(stderr
, "qemu: too many drives\n");
1907 drives_opt
[index
].file
= file
;
1909 vsnprintf(drives_opt
[index
].opt
,
1910 sizeof(drives_opt
[0].opt
), fmt
, ap
);
1917 void drive_remove(int index
)
1919 drives_opt
[index
].used
= 0;
1923 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
1927 /* seek interface, bus and unit */
1929 for (index
= 0; index
< MAX_DRIVES
; index
++)
1930 if (drives_table
[index
].type
== type
&&
1931 drives_table
[index
].bus
== bus
&&
1932 drives_table
[index
].unit
== unit
&&
1933 drives_table
[index
].used
)
1939 int drive_get_max_bus(BlockInterfaceType type
)
1945 for (index
= 0; index
< nb_drives
; index
++) {
1946 if(drives_table
[index
].type
== type
&&
1947 drives_table
[index
].bus
> max_bus
)
1948 max_bus
= drives_table
[index
].bus
;
1953 const char *drive_get_serial(BlockDriverState
*bdrv
)
1957 for (index
= 0; index
< nb_drives
; index
++)
1958 if (drives_table
[index
].bdrv
== bdrv
)
1959 return drives_table
[index
].serial
;
1964 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1968 for (index
= 0; index
< nb_drives
; index
++)
1969 if (drives_table
[index
].bdrv
== bdrv
)
1970 return drives_table
[index
].onerror
;
1972 return BLOCK_ERR_STOP_ENOSPC
;
1975 static void bdrv_format_print(void *opaque
, const char *name
)
1977 fprintf(stderr
, " %s", name
);
1980 void drive_uninit(BlockDriverState
*bdrv
)
1984 for (i
= 0; i
< MAX_DRIVES
; i
++)
1985 if (drives_table
[i
].bdrv
== bdrv
) {
1986 drives_table
[i
].bdrv
= NULL
;
1987 drives_table
[i
].used
= 0;
1988 drive_remove(drives_table
[i
].drive_opt_idx
);
1994 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2000 const char *mediastr
= "";
2001 BlockInterfaceType type
;
2002 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2003 int bus_id
, unit_id
;
2004 int cyls
, heads
, secs
, translation
;
2005 BlockDriverState
*bdrv
;
2006 BlockDriver
*drv
= NULL
;
2007 QEMUMachine
*machine
= opaque
;
2011 int bdrv_flags
, onerror
;
2012 const char *devaddr
;
2013 int drives_table_idx
;
2014 char *str
= arg
->opt
;
2015 static const char * const params
[] = { "bus", "unit", "if", "index",
2016 "cyls", "heads", "secs", "trans",
2017 "media", "snapshot", "file",
2018 "cache", "format", "serial",
2022 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2023 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2029 cyls
= heads
= secs
= 0;
2032 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2036 if (machine
->use_scsi
) {
2038 max_devs
= MAX_SCSI_DEVS
;
2039 pstrcpy(devname
, sizeof(devname
), "scsi");
2042 max_devs
= MAX_IDE_DEVS
;
2043 pstrcpy(devname
, sizeof(devname
), "ide");
2047 /* extract parameters */
2049 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2050 bus_id
= strtol(buf
, NULL
, 0);
2052 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2057 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2058 unit_id
= strtol(buf
, NULL
, 0);
2060 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2065 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2066 pstrcpy(devname
, sizeof(devname
), buf
);
2067 if (!strcmp(buf
, "ide")) {
2069 max_devs
= MAX_IDE_DEVS
;
2070 } else if (!strcmp(buf
, "scsi")) {
2072 max_devs
= MAX_SCSI_DEVS
;
2073 } else if (!strcmp(buf
, "floppy")) {
2076 } else if (!strcmp(buf
, "pflash")) {
2079 } else if (!strcmp(buf
, "mtd")) {
2082 } else if (!strcmp(buf
, "sd")) {
2085 } else if (!strcmp(buf
, "virtio")) {
2088 } else if (!strcmp(buf
, "xen")) {
2092 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2097 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2098 index
= strtol(buf
, NULL
, 0);
2100 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2105 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2106 cyls
= strtol(buf
, NULL
, 0);
2109 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2110 heads
= strtol(buf
, NULL
, 0);
2113 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2114 secs
= strtol(buf
, NULL
, 0);
2117 if (cyls
|| heads
|| secs
) {
2118 if (cyls
< 1 || cyls
> 16383) {
2119 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2122 if (heads
< 1 || heads
> 16) {
2123 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2126 if (secs
< 1 || secs
> 63) {
2127 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2132 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2135 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2139 if (!strcmp(buf
, "none"))
2140 translation
= BIOS_ATA_TRANSLATION_NONE
;
2141 else if (!strcmp(buf
, "lba"))
2142 translation
= BIOS_ATA_TRANSLATION_LBA
;
2143 else if (!strcmp(buf
, "auto"))
2144 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2146 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2151 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2152 if (!strcmp(buf
, "disk")) {
2154 } else if (!strcmp(buf
, "cdrom")) {
2155 if (cyls
|| secs
|| heads
) {
2157 "qemu: '%s' invalid physical CHS format\n", str
);
2160 media
= MEDIA_CDROM
;
2162 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2167 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2168 if (!strcmp(buf
, "on"))
2170 else if (!strcmp(buf
, "off"))
2173 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2178 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2179 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2181 else if (!strcmp(buf
, "writethrough"))
2183 else if (!strcmp(buf
, "writeback"))
2186 fprintf(stderr
, "qemu: invalid cache option\n");
2191 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2192 if (strcmp(buf
, "?") == 0) {
2193 fprintf(stderr
, "qemu: Supported formats:");
2194 bdrv_iterate_format(bdrv_format_print
, NULL
);
2195 fprintf(stderr
, "\n");
2198 drv
= bdrv_find_format(buf
);
2200 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2205 if (arg
->file
== NULL
)
2206 get_param_value(file
, sizeof(file
), "file", str
);
2208 pstrcpy(file
, sizeof(file
), arg
->file
);
2210 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2211 memset(serial
, 0, sizeof(serial
));
2213 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2214 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2215 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2216 fprintf(stderr
, "werror is no supported by this format\n");
2219 if (!strcmp(buf
, "ignore"))
2220 onerror
= BLOCK_ERR_IGNORE
;
2221 else if (!strcmp(buf
, "enospc"))
2222 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2223 else if (!strcmp(buf
, "stop"))
2224 onerror
= BLOCK_ERR_STOP_ANY
;
2225 else if (!strcmp(buf
, "report"))
2226 onerror
= BLOCK_ERR_REPORT
;
2228 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2234 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2235 if (type
!= IF_VIRTIO
) {
2236 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2239 devaddr
= strdup(buf
);
2242 /* compute bus and unit according index */
2245 if (bus_id
!= 0 || unit_id
!= -1) {
2247 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2255 unit_id
= index
% max_devs
;
2256 bus_id
= index
/ max_devs
;
2260 /* if user doesn't specify a unit_id,
2261 * try to find the first free
2264 if (unit_id
== -1) {
2266 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2268 if (max_devs
&& unit_id
>= max_devs
) {
2269 unit_id
-= max_devs
;
2277 if (max_devs
&& unit_id
>= max_devs
) {
2278 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2279 str
, unit_id
, max_devs
- 1);
2284 * ignore multiple definitions
2287 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2292 if (type
== IF_IDE
|| type
== IF_SCSI
)
2293 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2295 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2296 devname
, bus_id
, mediastr
, unit_id
);
2298 snprintf(buf
, sizeof(buf
), "%s%s%i",
2299 devname
, mediastr
, unit_id
);
2300 bdrv
= bdrv_new(buf
);
2301 drives_table_idx
= drive_get_free_idx();
2302 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2303 drives_table
[drives_table_idx
].devaddr
= devaddr
;
2304 drives_table
[drives_table_idx
].type
= type
;
2305 drives_table
[drives_table_idx
].bus
= bus_id
;
2306 drives_table
[drives_table_idx
].unit
= unit_id
;
2307 drives_table
[drives_table_idx
].onerror
= onerror
;
2308 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2309 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2319 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2320 bdrv_set_translation_hint(bdrv
, translation
);
2324 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2329 /* FIXME: This isn't really a floppy, but it's a reasonable
2332 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2345 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2346 cache
= 2; /* always use write-back with snapshot */
2348 if (cache
== 0) /* no caching */
2349 bdrv_flags
|= BDRV_O_NOCACHE
;
2350 else if (cache
== 2) /* write-back */
2351 bdrv_flags
|= BDRV_O_CACHE_WB
;
2352 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2353 fprintf(stderr
, "qemu: could not open disk image %s\n",
2357 if (bdrv_key_required(bdrv
))
2359 return drives_table_idx
;
2362 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2364 boot_set_handler
= func
;
2365 boot_set_opaque
= opaque
;
2368 int qemu_boot_set(const char *boot_devices
)
2370 if (!boot_set_handler
) {
2373 return boot_set_handler(boot_set_opaque
, boot_devices
);
2376 static int parse_bootdevices(char *devices
)
2378 /* We just do some generic consistency checks */
2382 for (p
= devices
; *p
!= '\0'; p
++) {
2383 /* Allowed boot devices are:
2384 * a-b: floppy disk drives
2385 * c-f: IDE disk drives
2386 * g-m: machine implementation dependant drives
2387 * n-p: network devices
2388 * It's up to each machine implementation to check if the given boot
2389 * devices match the actual hardware implementation and firmware
2392 if (*p
< 'a' || *p
> 'p') {
2393 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2396 if (bitmap
& (1 << (*p
- 'a'))) {
2397 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2400 bitmap
|= 1 << (*p
- 'a');
2405 static void restore_boot_devices(void *opaque
)
2407 char *standard_boot_devices
= opaque
;
2409 qemu_boot_set(standard_boot_devices
);
2411 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2412 qemu_free(standard_boot_devices
);
2415 static void numa_add(const char *optarg
)
2419 unsigned long long value
, endvalue
;
2422 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2423 if (!strcmp(option
, "node")) {
2424 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2425 nodenr
= nb_numa_nodes
;
2427 nodenr
= strtoull(option
, NULL
, 10);
2430 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2431 node_mem
[nodenr
] = 0;
2433 value
= strtoull(option
, &endptr
, 0);
2435 case 0: case 'M': case 'm':
2442 node_mem
[nodenr
] = value
;
2444 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2445 node_cpumask
[nodenr
] = 0;
2447 value
= strtoull(option
, &endptr
, 10);
2450 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2452 if (*endptr
== '-') {
2453 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2454 if (endvalue
>= 63) {
2457 "only 63 CPUs in NUMA mode supported.\n");
2459 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2464 node_cpumask
[nodenr
] = value
;
2471 /***********************************************************/
2474 static USBPort
*used_usb_ports
;
2475 static USBPort
*free_usb_ports
;
2477 /* ??? Maybe change this to register a hub to keep track of the topology. */
2478 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2479 usb_attachfn attach
)
2481 port
->opaque
= opaque
;
2482 port
->index
= index
;
2483 port
->attach
= attach
;
2484 port
->next
= free_usb_ports
;
2485 free_usb_ports
= port
;
2488 int usb_device_add_dev(USBDevice
*dev
)
2492 /* Find a USB port to add the device to. */
2493 port
= free_usb_ports
;
2497 /* Create a new hub and chain it on. */
2498 free_usb_ports
= NULL
;
2499 port
->next
= used_usb_ports
;
2500 used_usb_ports
= port
;
2502 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2503 usb_attach(port
, hub
);
2504 port
= free_usb_ports
;
2507 free_usb_ports
= port
->next
;
2508 port
->next
= used_usb_ports
;
2509 used_usb_ports
= port
;
2510 usb_attach(port
, dev
);
2514 static void usb_msd_password_cb(void *opaque
, int err
)
2516 USBDevice
*dev
= opaque
;
2519 usb_device_add_dev(dev
);
2521 dev
->handle_destroy(dev
);
2524 static int usb_device_add(const char *devname
, int is_hotplug
)
2529 if (!free_usb_ports
)
2532 if (strstart(devname
, "host:", &p
)) {
2533 dev
= usb_host_device_open(p
);
2534 } else if (!strcmp(devname
, "mouse")) {
2535 dev
= usb_mouse_init();
2536 } else if (!strcmp(devname
, "tablet")) {
2537 dev
= usb_tablet_init();
2538 } else if (!strcmp(devname
, "keyboard")) {
2539 dev
= usb_keyboard_init();
2540 } else if (strstart(devname
, "disk:", &p
)) {
2541 BlockDriverState
*bs
;
2543 dev
= usb_msd_init(p
);
2546 bs
= usb_msd_get_bdrv(dev
);
2547 if (bdrv_key_required(bs
)) {
2550 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2555 } else if (!strcmp(devname
, "wacom-tablet")) {
2556 dev
= usb_wacom_init();
2557 } else if (strstart(devname
, "serial:", &p
)) {
2558 dev
= usb_serial_init(p
);
2559 #ifdef CONFIG_BRLAPI
2560 } else if (!strcmp(devname
, "braille")) {
2561 dev
= usb_baum_init();
2563 } else if (strstart(devname
, "net:", &p
)) {
2566 if (net_client_init(NULL
, "nic", p
) < 0)
2568 nd_table
[nic
].model
= "usb";
2569 dev
= usb_net_init(&nd_table
[nic
]);
2570 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2571 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2572 bt_new_hci(qemu_find_bt_vlan(0)));
2579 return usb_device_add_dev(dev
);
2582 int usb_device_del_addr(int bus_num
, int addr
)
2588 if (!used_usb_ports
)
2594 lastp
= &used_usb_ports
;
2595 port
= used_usb_ports
;
2596 while (port
&& port
->dev
->addr
!= addr
) {
2597 lastp
= &port
->next
;
2605 *lastp
= port
->next
;
2606 usb_attach(port
, NULL
);
2607 dev
->handle_destroy(dev
);
2608 port
->next
= free_usb_ports
;
2609 free_usb_ports
= port
;
2613 static int usb_device_del(const char *devname
)
2618 if (strstart(devname
, "host:", &p
))
2619 return usb_host_device_close(p
);
2621 if (!used_usb_ports
)
2624 p
= strchr(devname
, '.');
2627 bus_num
= strtoul(devname
, NULL
, 0);
2628 addr
= strtoul(p
+ 1, NULL
, 0);
2630 return usb_device_del_addr(bus_num
, addr
);
2633 void do_usb_add(Monitor
*mon
, const char *devname
)
2635 usb_device_add(devname
, 1);
2638 void do_usb_del(Monitor
*mon
, const char *devname
)
2640 usb_device_del(devname
);
2643 void usb_info(Monitor
*mon
)
2647 const char *speed_str
;
2650 monitor_printf(mon
, "USB support not enabled\n");
2654 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2658 switch(dev
->speed
) {
2662 case USB_SPEED_FULL
:
2665 case USB_SPEED_HIGH
:
2672 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2673 0, dev
->addr
, speed_str
, dev
->devname
);
2677 /***********************************************************/
2678 /* PCMCIA/Cardbus */
2680 static struct pcmcia_socket_entry_s
{
2681 PCMCIASocket
*socket
;
2682 struct pcmcia_socket_entry_s
*next
;
2683 } *pcmcia_sockets
= 0;
2685 void pcmcia_socket_register(PCMCIASocket
*socket
)
2687 struct pcmcia_socket_entry_s
*entry
;
2689 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2690 entry
->socket
= socket
;
2691 entry
->next
= pcmcia_sockets
;
2692 pcmcia_sockets
= entry
;
2695 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2697 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2699 ptr
= &pcmcia_sockets
;
2700 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2701 if (entry
->socket
== socket
) {
2707 void pcmcia_info(Monitor
*mon
)
2709 struct pcmcia_socket_entry_s
*iter
;
2711 if (!pcmcia_sockets
)
2712 monitor_printf(mon
, "No PCMCIA sockets\n");
2714 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2715 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2716 iter
->socket
->attached
? iter
->socket
->card_string
:
2720 /***********************************************************/
2721 /* register display */
2723 struct DisplayAllocator default_allocator
= {
2724 defaultallocator_create_displaysurface
,
2725 defaultallocator_resize_displaysurface
,
2726 defaultallocator_free_displaysurface
2729 void register_displaystate(DisplayState
*ds
)
2739 DisplayState
*get_displaystate(void)
2741 return display_state
;
2744 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2746 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2747 return ds
->allocator
;
2752 static void dumb_display_init(void)
2754 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2755 ds
->allocator
= &default_allocator
;
2756 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2757 register_displaystate(ds
);
2760 /***********************************************************/
2763 typedef struct IOHandlerRecord
{
2765 IOCanRWHandler
*fd_read_poll
;
2767 IOHandler
*fd_write
;
2770 /* temporary data */
2772 struct IOHandlerRecord
*next
;
2775 static IOHandlerRecord
*first_io_handler
;
2777 /* XXX: fd_read_poll should be suppressed, but an API change is
2778 necessary in the character devices to suppress fd_can_read(). */
2779 int qemu_set_fd_handler2(int fd
,
2780 IOCanRWHandler
*fd_read_poll
,
2782 IOHandler
*fd_write
,
2785 IOHandlerRecord
**pioh
, *ioh
;
2787 if (!fd_read
&& !fd_write
) {
2788 pioh
= &first_io_handler
;
2793 if (ioh
->fd
== fd
) {
2800 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2804 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2805 ioh
->next
= first_io_handler
;
2806 first_io_handler
= ioh
;
2809 ioh
->fd_read_poll
= fd_read_poll
;
2810 ioh
->fd_read
= fd_read
;
2811 ioh
->fd_write
= fd_write
;
2812 ioh
->opaque
= opaque
;
2818 int qemu_set_fd_handler(int fd
,
2820 IOHandler
*fd_write
,
2823 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2827 /***********************************************************/
2828 /* Polling handling */
2830 typedef struct PollingEntry
{
2833 struct PollingEntry
*next
;
2836 static PollingEntry
*first_polling_entry
;
2838 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2840 PollingEntry
**ppe
, *pe
;
2841 pe
= qemu_mallocz(sizeof(PollingEntry
));
2843 pe
->opaque
= opaque
;
2844 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2849 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2851 PollingEntry
**ppe
, *pe
;
2852 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2854 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2862 /***********************************************************/
2863 /* Wait objects support */
2864 typedef struct WaitObjects
{
2866 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2867 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2868 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2871 static WaitObjects wait_objects
= {0};
2873 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2875 WaitObjects
*w
= &wait_objects
;
2877 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2879 w
->events
[w
->num
] = handle
;
2880 w
->func
[w
->num
] = func
;
2881 w
->opaque
[w
->num
] = opaque
;
2886 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2889 WaitObjects
*w
= &wait_objects
;
2892 for (i
= 0; i
< w
->num
; i
++) {
2893 if (w
->events
[i
] == handle
)
2896 w
->events
[i
] = w
->events
[i
+ 1];
2897 w
->func
[i
] = w
->func
[i
+ 1];
2898 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2906 /***********************************************************/
2907 /* ram save/restore */
2909 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2913 v
= qemu_get_byte(f
);
2916 if (qemu_get_buffer(f
, buf
, len
) != len
)
2920 v
= qemu_get_byte(f
);
2921 memset(buf
, v
, len
);
2927 if (qemu_file_has_error(f
))
2933 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2938 if (qemu_get_be32(f
) != last_ram_offset
)
2940 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2941 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2948 #define BDRV_HASH_BLOCK_SIZE 1024
2949 #define IOBUF_SIZE 4096
2950 #define RAM_CBLOCK_MAGIC 0xfabe
2952 typedef struct RamDecompressState
{
2955 uint8_t buf
[IOBUF_SIZE
];
2956 } RamDecompressState
;
2958 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2961 memset(s
, 0, sizeof(*s
));
2963 ret
= inflateInit(&s
->zstream
);
2969 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2973 s
->zstream
.avail_out
= len
;
2974 s
->zstream
.next_out
= buf
;
2975 while (s
->zstream
.avail_out
> 0) {
2976 if (s
->zstream
.avail_in
== 0) {
2977 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2979 clen
= qemu_get_be16(s
->f
);
2980 if (clen
> IOBUF_SIZE
)
2982 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2983 s
->zstream
.avail_in
= clen
;
2984 s
->zstream
.next_in
= s
->buf
;
2986 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2987 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2994 static void ram_decompress_close(RamDecompressState
*s
)
2996 inflateEnd(&s
->zstream
);
2999 #define RAM_SAVE_FLAG_FULL 0x01
3000 #define RAM_SAVE_FLAG_COMPRESS 0x02
3001 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3002 #define RAM_SAVE_FLAG_PAGE 0x08
3003 #define RAM_SAVE_FLAG_EOS 0x10
3005 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3007 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3008 uint32_t *array
= (uint32_t *)page
;
3011 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3012 if (array
[i
] != val
)
3019 static int ram_save_block(QEMUFile
*f
)
3021 static ram_addr_t current_addr
= 0;
3022 ram_addr_t saved_addr
= current_addr
;
3023 ram_addr_t addr
= 0;
3026 while (addr
< last_ram_offset
) {
3027 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3030 cpu_physical_memory_reset_dirty(current_addr
,
3031 current_addr
+ TARGET_PAGE_SIZE
,
3032 MIGRATION_DIRTY_FLAG
);
3034 p
= qemu_get_ram_ptr(current_addr
);
3036 if (is_dup_page(p
, *p
)) {
3037 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3038 qemu_put_byte(f
, *p
);
3040 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3041 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3047 addr
+= TARGET_PAGE_SIZE
;
3048 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3054 static uint64_t bytes_transferred
= 0;
3056 static ram_addr_t
ram_save_remaining(void)
3059 ram_addr_t count
= 0;
3061 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3062 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3069 uint64_t ram_bytes_remaining(void)
3071 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3074 uint64_t ram_bytes_transferred(void)
3076 return bytes_transferred
;
3079 uint64_t ram_bytes_total(void)
3081 return last_ram_offset
;
3084 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3087 uint64_t bytes_transferred_last
;
3089 uint64_t expected_time
= 0;
3091 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3092 qemu_file_set_error(f
);
3097 /* Make sure all dirty bits are set */
3098 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3099 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3100 cpu_physical_memory_set_dirty(addr
);
3103 /* Enable dirty memory tracking */
3104 cpu_physical_memory_set_dirty_tracking(1);
3106 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3109 bytes_transferred_last
= bytes_transferred
;
3110 bwidth
= get_clock();
3112 while (!qemu_file_rate_limit(f
)) {
3115 ret
= ram_save_block(f
);
3116 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3117 if (ret
== 0) /* no more blocks */
3121 bwidth
= get_clock() - bwidth
;
3122 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3124 /* if we haven't transferred anything this round, force expected_time to a
3125 * a very high value, but without crashing */
3129 /* try transferring iterative blocks of memory */
3133 /* flush all remaining blocks regardless of rate limiting */
3134 while (ram_save_block(f
) != 0) {
3135 bytes_transferred
+= TARGET_PAGE_SIZE
;
3137 cpu_physical_memory_set_dirty_tracking(0);
3140 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3142 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3144 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3147 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3149 RamDecompressState s1
, *s
= &s1
;
3153 if (ram_decompress_open(s
, f
) < 0)
3155 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3156 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3157 fprintf(stderr
, "Error while reading ram block header\n");
3161 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3162 BDRV_HASH_BLOCK_SIZE
) < 0) {
3163 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3168 printf("Error block header\n");
3172 ram_decompress_close(s
);
3177 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3182 if (version_id
== 1)
3183 return ram_load_v1(f
, opaque
);
3185 if (version_id
== 2) {
3186 if (qemu_get_be32(f
) != last_ram_offset
)
3188 return ram_load_dead(f
, opaque
);
3191 if (version_id
!= 3)
3195 addr
= qemu_get_be64(f
);
3197 flags
= addr
& ~TARGET_PAGE_MASK
;
3198 addr
&= TARGET_PAGE_MASK
;
3200 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3201 if (addr
!= last_ram_offset
)
3205 if (flags
& RAM_SAVE_FLAG_FULL
) {
3206 if (ram_load_dead(f
, opaque
) < 0)
3210 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3211 uint8_t ch
= qemu_get_byte(f
);
3212 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3215 (!kvm_enabled() || kvm_has_sync_mmu())) {
3216 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3219 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3220 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3221 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3226 void qemu_service_io(void)
3228 qemu_notify_event();
3231 /***********************************************************/
3232 /* bottom halves (can be seen as timers which expire ASAP) */
3243 static QEMUBH
*first_bh
= NULL
;
3245 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3248 bh
= qemu_mallocz(sizeof(QEMUBH
));
3250 bh
->opaque
= opaque
;
3251 bh
->next
= first_bh
;
3256 int qemu_bh_poll(void)
3262 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3263 if (!bh
->deleted
&& bh
->scheduled
) {
3272 /* remove deleted bhs */
3286 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3294 void qemu_bh_schedule(QEMUBH
*bh
)
3300 /* stop the currently executing CPU to execute the BH ASAP */
3301 qemu_notify_event();
3304 void qemu_bh_cancel(QEMUBH
*bh
)
3309 void qemu_bh_delete(QEMUBH
*bh
)
3315 static void qemu_bh_update_timeout(int *timeout
)
3319 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3320 if (!bh
->deleted
&& bh
->scheduled
) {
3322 /* idle bottom halves will be polled at least
3324 *timeout
= MIN(10, *timeout
);
3326 /* non-idle bottom halves will be executed
3335 /***********************************************************/
3336 /* machine registration */
3338 static QEMUMachine
*first_machine
= NULL
;
3339 QEMUMachine
*current_machine
= NULL
;
3341 int qemu_register_machine(QEMUMachine
*m
)
3344 pm
= &first_machine
;
3352 static QEMUMachine
*find_machine(const char *name
)
3356 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3357 if (!strcmp(m
->name
, name
))
3359 if (m
->alias
&& !strcmp(m
->alias
, name
))
3365 static QEMUMachine
*find_default_machine(void)
3369 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3370 if (m
->is_default
) {
3377 /***********************************************************/
3378 /* main execution loop */
3380 static void gui_update(void *opaque
)
3382 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3383 DisplayState
*ds
= opaque
;
3384 DisplayChangeListener
*dcl
= ds
->listeners
;
3388 while (dcl
!= NULL
) {
3389 if (dcl
->gui_timer_interval
&&
3390 dcl
->gui_timer_interval
< interval
)
3391 interval
= dcl
->gui_timer_interval
;
3394 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3397 static void nographic_update(void *opaque
)
3399 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3401 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3404 struct vm_change_state_entry
{
3405 VMChangeStateHandler
*cb
;
3407 LIST_ENTRY (vm_change_state_entry
) entries
;
3410 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3412 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3415 VMChangeStateEntry
*e
;
3417 e
= qemu_mallocz(sizeof (*e
));
3421 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3425 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3427 LIST_REMOVE (e
, entries
);
3431 static void vm_state_notify(int running
, int reason
)
3433 VMChangeStateEntry
*e
;
3435 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3436 e
->cb(e
->opaque
, running
, reason
);
3440 static void resume_all_vcpus(void);
3441 static void pause_all_vcpus(void);
3448 vm_state_notify(1, 0);
3449 qemu_rearm_alarm_timer(alarm_timer
);
3454 /* reset/shutdown handler */
3456 typedef struct QEMUResetEntry
{
3457 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3458 QEMUResetHandler
*func
;
3462 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3463 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3464 static int reset_requested
;
3465 static int shutdown_requested
;
3466 static int powerdown_requested
;
3467 static int debug_requested
;
3468 static int vmstop_requested
;
3470 int qemu_shutdown_requested(void)
3472 int r
= shutdown_requested
;
3473 shutdown_requested
= 0;
3477 int qemu_reset_requested(void)
3479 int r
= reset_requested
;
3480 reset_requested
= 0;
3484 int qemu_powerdown_requested(void)
3486 int r
= powerdown_requested
;
3487 powerdown_requested
= 0;
3491 static int qemu_debug_requested(void)
3493 int r
= debug_requested
;
3494 debug_requested
= 0;
3498 static int qemu_vmstop_requested(void)
3500 int r
= vmstop_requested
;
3501 vmstop_requested
= 0;
3505 static void do_vm_stop(int reason
)
3508 cpu_disable_ticks();
3511 vm_state_notify(0, reason
);
3515 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3517 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3520 re
->opaque
= opaque
;
3521 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3524 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3528 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3529 if (re
->func
== func
&& re
->opaque
== opaque
) {
3530 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3537 void qemu_system_reset(void)
3539 QEMUResetEntry
*re
, *nre
;
3541 /* reset all devices */
3542 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3543 re
->func(re
->opaque
);
3547 void qemu_system_reset_request(void)
3550 shutdown_requested
= 1;
3552 reset_requested
= 1;
3554 qemu_notify_event();
3557 void qemu_system_shutdown_request(void)
3559 shutdown_requested
= 1;
3560 qemu_notify_event();
3563 void qemu_system_powerdown_request(void)
3565 powerdown_requested
= 1;
3566 qemu_notify_event();
3569 #ifdef CONFIG_IOTHREAD
3570 static void qemu_system_vmstop_request(int reason
)
3572 vmstop_requested
= reason
;
3573 qemu_notify_event();
3578 static int io_thread_fd
= -1;
3580 static void qemu_event_increment(void)
3582 static const char byte
= 0;
3584 if (io_thread_fd
== -1)
3587 write(io_thread_fd
, &byte
, sizeof(byte
));
3590 static void qemu_event_read(void *opaque
)
3592 int fd
= (unsigned long)opaque
;
3595 /* Drain the notify pipe */
3598 len
= read(fd
, buffer
, sizeof(buffer
));
3599 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3602 static int qemu_event_init(void)
3611 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3615 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3619 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3620 (void *)(unsigned long)fds
[0]);
3622 io_thread_fd
= fds
[1];
3631 HANDLE qemu_event_handle
;
3633 static void dummy_event_handler(void *opaque
)
3637 static int qemu_event_init(void)
3639 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3640 if (!qemu_event_handle
) {
3641 perror("Failed CreateEvent");
3644 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3648 static void qemu_event_increment(void)
3650 SetEvent(qemu_event_handle
);
3654 static int cpu_can_run(CPUState
*env
)
3663 #ifndef CONFIG_IOTHREAD
3664 static int qemu_init_main_loop(void)
3666 return qemu_event_init();
3669 void qemu_init_vcpu(void *_env
)
3671 CPUState
*env
= _env
;
3678 int qemu_cpu_self(void *env
)
3683 static void resume_all_vcpus(void)
3687 static void pause_all_vcpus(void)
3691 void qemu_cpu_kick(void *env
)
3696 void qemu_notify_event(void)
3698 CPUState
*env
= cpu_single_env
;
3703 if (env
->kqemu_enabled
)
3704 kqemu_cpu_interrupt(env
);
3709 #define qemu_mutex_lock_iothread() do { } while (0)
3710 #define qemu_mutex_unlock_iothread() do { } while (0)
3712 void vm_stop(int reason
)
3717 #else /* CONFIG_IOTHREAD */
3719 #include "qemu-thread.h"
3721 QemuMutex qemu_global_mutex
;
3722 static QemuMutex qemu_fair_mutex
;
3724 static QemuThread io_thread
;
3726 static QemuThread
*tcg_cpu_thread
;
3727 static QemuCond
*tcg_halt_cond
;
3729 static int qemu_system_ready
;
3731 static QemuCond qemu_cpu_cond
;
3733 static QemuCond qemu_system_cond
;
3734 static QemuCond qemu_pause_cond
;
3736 static void block_io_signals(void);
3737 static void unblock_io_signals(void);
3738 static int tcg_has_work(void);
3740 static int qemu_init_main_loop(void)
3744 ret
= qemu_event_init();
3748 qemu_cond_init(&qemu_pause_cond
);
3749 qemu_mutex_init(&qemu_fair_mutex
);
3750 qemu_mutex_init(&qemu_global_mutex
);
3751 qemu_mutex_lock(&qemu_global_mutex
);
3753 unblock_io_signals();
3754 qemu_thread_self(&io_thread
);
3759 static void qemu_wait_io_event(CPUState
*env
)
3761 while (!tcg_has_work())
3762 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3764 qemu_mutex_unlock(&qemu_global_mutex
);
3767 * Users of qemu_global_mutex can be starved, having no chance
3768 * to acquire it since this path will get to it first.
3769 * So use another lock to provide fairness.
3771 qemu_mutex_lock(&qemu_fair_mutex
);
3772 qemu_mutex_unlock(&qemu_fair_mutex
);
3774 qemu_mutex_lock(&qemu_global_mutex
);
3778 qemu_cond_signal(&qemu_pause_cond
);
3782 static int qemu_cpu_exec(CPUState
*env
);
3784 static void *kvm_cpu_thread_fn(void *arg
)
3786 CPUState
*env
= arg
;
3789 qemu_thread_self(env
->thread
);
3791 /* signal CPU creation */
3792 qemu_mutex_lock(&qemu_global_mutex
);
3794 qemu_cond_signal(&qemu_cpu_cond
);
3796 /* and wait for machine initialization */
3797 while (!qemu_system_ready
)
3798 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3801 if (cpu_can_run(env
))
3803 qemu_wait_io_event(env
);
3809 static void tcg_cpu_exec(void);
3811 static void *tcg_cpu_thread_fn(void *arg
)
3813 CPUState
*env
= arg
;
3816 qemu_thread_self(env
->thread
);
3818 /* signal CPU creation */
3819 qemu_mutex_lock(&qemu_global_mutex
);
3820 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3822 qemu_cond_signal(&qemu_cpu_cond
);
3824 /* and wait for machine initialization */
3825 while (!qemu_system_ready
)
3826 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3830 qemu_wait_io_event(cur_cpu
);
3836 void qemu_cpu_kick(void *_env
)
3838 CPUState
*env
= _env
;
3839 qemu_cond_broadcast(env
->halt_cond
);
3841 qemu_thread_signal(env
->thread
, SIGUSR1
);
3844 int qemu_cpu_self(void *env
)
3846 return (cpu_single_env
!= NULL
);
3849 static void cpu_signal(int sig
)
3852 cpu_exit(cpu_single_env
);
3855 static void block_io_signals(void)
3858 struct sigaction sigact
;
3861 sigaddset(&set
, SIGUSR2
);
3862 sigaddset(&set
, SIGIO
);
3863 sigaddset(&set
, SIGALRM
);
3864 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3867 sigaddset(&set
, SIGUSR1
);
3868 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3870 memset(&sigact
, 0, sizeof(sigact
));
3871 sigact
.sa_handler
= cpu_signal
;
3872 sigaction(SIGUSR1
, &sigact
, NULL
);
3875 static void unblock_io_signals(void)
3880 sigaddset(&set
, SIGUSR2
);
3881 sigaddset(&set
, SIGIO
);
3882 sigaddset(&set
, SIGALRM
);
3883 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3886 sigaddset(&set
, SIGUSR1
);
3887 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3890 static void qemu_signal_lock(unsigned int msecs
)
3892 qemu_mutex_lock(&qemu_fair_mutex
);
3894 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3895 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3896 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3899 qemu_mutex_unlock(&qemu_fair_mutex
);
3902 static void qemu_mutex_lock_iothread(void)
3904 if (kvm_enabled()) {
3905 qemu_mutex_lock(&qemu_fair_mutex
);
3906 qemu_mutex_lock(&qemu_global_mutex
);
3907 qemu_mutex_unlock(&qemu_fair_mutex
);
3909 qemu_signal_lock(100);
3912 static void qemu_mutex_unlock_iothread(void)
3914 qemu_mutex_unlock(&qemu_global_mutex
);
3917 static int all_vcpus_paused(void)
3919 CPUState
*penv
= first_cpu
;
3924 penv
= (CPUState
*)penv
->next_cpu
;
3930 static void pause_all_vcpus(void)
3932 CPUState
*penv
= first_cpu
;
3936 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3937 qemu_cpu_kick(penv
);
3938 penv
= (CPUState
*)penv
->next_cpu
;
3941 while (!all_vcpus_paused()) {
3942 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3945 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3946 penv
= (CPUState
*)penv
->next_cpu
;
3951 static void resume_all_vcpus(void)
3953 CPUState
*penv
= first_cpu
;
3958 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3959 qemu_cpu_kick(penv
);
3960 penv
= (CPUState
*)penv
->next_cpu
;
3964 static void tcg_init_vcpu(void *_env
)
3966 CPUState
*env
= _env
;
3967 /* share a single thread for all cpus with TCG */
3968 if (!tcg_cpu_thread
) {
3969 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3970 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3971 qemu_cond_init(env
->halt_cond
);
3972 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3973 while (env
->created
== 0)
3974 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3975 tcg_cpu_thread
= env
->thread
;
3976 tcg_halt_cond
= env
->halt_cond
;
3978 env
->thread
= tcg_cpu_thread
;
3979 env
->halt_cond
= tcg_halt_cond
;
3983 static void kvm_start_vcpu(CPUState
*env
)
3986 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3987 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3988 qemu_cond_init(env
->halt_cond
);
3989 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3990 while (env
->created
== 0)
3991 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3994 void qemu_init_vcpu(void *_env
)
3996 CPUState
*env
= _env
;
3999 kvm_start_vcpu(env
);
4004 void qemu_notify_event(void)
4006 qemu_event_increment();
4009 void vm_stop(int reason
)
4012 qemu_thread_self(&me
);
4014 if (!qemu_thread_equal(&me
, &io_thread
)) {
4015 qemu_system_vmstop_request(reason
);
4017 * FIXME: should not return to device code in case
4018 * vm_stop() has been requested.
4020 if (cpu_single_env
) {
4021 cpu_exit(cpu_single_env
);
4022 cpu_single_env
->stop
= 1;
4033 static void host_main_loop_wait(int *timeout
)
4039 /* XXX: need to suppress polling by better using win32 events */
4041 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4042 ret
|= pe
->func(pe
->opaque
);
4046 WaitObjects
*w
= &wait_objects
;
4048 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4049 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4050 if (w
->func
[ret
- WAIT_OBJECT_0
])
4051 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4053 /* Check for additional signaled events */
4054 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4056 /* Check if event is signaled */
4057 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4058 if(ret2
== WAIT_OBJECT_0
) {
4060 w
->func
[i
](w
->opaque
[i
]);
4061 } else if (ret2
== WAIT_TIMEOUT
) {
4063 err
= GetLastError();
4064 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4067 } else if (ret
== WAIT_TIMEOUT
) {
4069 err
= GetLastError();
4070 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4077 static void host_main_loop_wait(int *timeout
)
4082 void main_loop_wait(int timeout
)
4084 IOHandlerRecord
*ioh
;
4085 fd_set rfds
, wfds
, xfds
;
4089 qemu_bh_update_timeout(&timeout
);
4091 host_main_loop_wait(&timeout
);
4093 /* poll any events */
4094 /* XXX: separate device handlers from system ones */
4099 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4103 (!ioh
->fd_read_poll
||
4104 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4105 FD_SET(ioh
->fd
, &rfds
);
4109 if (ioh
->fd_write
) {
4110 FD_SET(ioh
->fd
, &wfds
);
4116 tv
.tv_sec
= timeout
/ 1000;
4117 tv
.tv_usec
= (timeout
% 1000) * 1000;
4119 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4121 qemu_mutex_unlock_iothread();
4122 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4123 qemu_mutex_lock_iothread();
4125 IOHandlerRecord
**pioh
;
4127 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4128 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4129 ioh
->fd_read(ioh
->opaque
);
4131 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4132 ioh
->fd_write(ioh
->opaque
);
4136 /* remove deleted IO handlers */
4137 pioh
= &first_io_handler
;
4148 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4150 /* rearm timer, if not periodic */
4151 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4152 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4153 qemu_rearm_alarm_timer(alarm_timer
);
4156 /* vm time timers */
4158 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4159 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4160 qemu_get_clock(vm_clock
));
4163 /* real time timers */
4164 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4165 qemu_get_clock(rt_clock
));
4167 /* Check bottom-halves last in case any of the earlier events triggered
4173 static int qemu_cpu_exec(CPUState
*env
)
4176 #ifdef CONFIG_PROFILER
4180 #ifdef CONFIG_PROFILER
4181 ti
= profile_getclock();
4186 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4187 env
->icount_decr
.u16
.low
= 0;
4188 env
->icount_extra
= 0;
4189 count
= qemu_next_deadline();
4190 count
= (count
+ (1 << icount_time_shift
) - 1)
4191 >> icount_time_shift
;
4192 qemu_icount
+= count
;
4193 decr
= (count
> 0xffff) ? 0xffff : count
;
4195 env
->icount_decr
.u16
.low
= decr
;
4196 env
->icount_extra
= count
;
4198 ret
= cpu_exec(env
);
4199 #ifdef CONFIG_PROFILER
4200 qemu_time
+= profile_getclock() - ti
;
4203 /* Fold pending instructions back into the
4204 instruction counter, and clear the interrupt flag. */
4205 qemu_icount
-= (env
->icount_decr
.u16
.low
4206 + env
->icount_extra
);
4207 env
->icount_decr
.u32
= 0;
4208 env
->icount_extra
= 0;
4213 static void tcg_cpu_exec(void)
4217 if (next_cpu
== NULL
)
4218 next_cpu
= first_cpu
;
4219 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4220 CPUState
*env
= cur_cpu
= next_cpu
;
4224 if (timer_alarm_pending
) {
4225 timer_alarm_pending
= 0;
4228 if (cpu_can_run(env
))
4229 ret
= qemu_cpu_exec(env
);
4230 if (ret
== EXCP_DEBUG
) {
4231 gdb_set_stop_cpu(env
);
4232 debug_requested
= 1;
4238 static int cpu_has_work(CPUState
*env
)
4246 if (qemu_cpu_has_work(env
))
4251 static int tcg_has_work(void)
4255 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4256 if (cpu_has_work(env
))
4261 static int qemu_calculate_timeout(void)
4263 #ifndef CONFIG_IOTHREAD
4268 else if (tcg_has_work())
4270 else if (!use_icount
)
4273 /* XXX: use timeout computed from timers */
4276 /* Advance virtual time to the next event. */
4277 if (use_icount
== 1) {
4278 /* When not using an adaptive execution frequency
4279 we tend to get badly out of sync with real time,
4280 so just delay for a reasonable amount of time. */
4283 delta
= cpu_get_icount() - cpu_get_clock();
4286 /* If virtual time is ahead of real time then just
4288 timeout
= (delta
/ 1000000) + 1;
4290 /* Wait for either IO to occur or the next
4292 add
= qemu_next_deadline();
4293 /* We advance the timer before checking for IO.
4294 Limit the amount we advance so that early IO
4295 activity won't get the guest too far ahead. */
4299 add
= (add
+ (1 << icount_time_shift
) - 1)
4300 >> icount_time_shift
;
4302 timeout
= delta
/ 1000000;
4309 #else /* CONFIG_IOTHREAD */
4314 static int vm_can_run(void)
4316 if (powerdown_requested
)
4318 if (reset_requested
)
4320 if (shutdown_requested
)
4322 if (debug_requested
)
4327 static void main_loop(void)
4331 #ifdef CONFIG_IOTHREAD
4332 qemu_system_ready
= 1;
4333 qemu_cond_broadcast(&qemu_system_cond
);
4338 #ifdef CONFIG_PROFILER
4341 #ifndef CONFIG_IOTHREAD
4344 #ifdef CONFIG_PROFILER
4345 ti
= profile_getclock();
4347 main_loop_wait(qemu_calculate_timeout());
4348 #ifdef CONFIG_PROFILER
4349 dev_time
+= profile_getclock() - ti
;
4351 } while (vm_can_run());
4353 if (qemu_debug_requested())
4354 vm_stop(EXCP_DEBUG
);
4355 if (qemu_shutdown_requested()) {
4362 if (qemu_reset_requested()) {
4364 qemu_system_reset();
4367 if (qemu_powerdown_requested())
4368 qemu_system_powerdown();
4369 if ((r
= qemu_vmstop_requested()))
4375 static void version(void)
4377 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4380 static void help(int exitcode
)
4383 printf("usage: %s [options] [disk_image]\n"
4385 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4387 #define DEF(option, opt_arg, opt_enum, opt_help) \
4389 #define DEFHEADING(text) stringify(text) "\n"
4390 #include "qemu-options.h"
4395 "During emulation, the following keys are useful:\n"
4396 "ctrl-alt-f toggle full screen\n"
4397 "ctrl-alt-n switch to virtual console 'n'\n"
4398 "ctrl-alt toggle mouse and keyboard grab\n"
4400 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4405 DEFAULT_NETWORK_SCRIPT
,
4406 DEFAULT_NETWORK_DOWN_SCRIPT
,
4408 DEFAULT_GDBSTUB_PORT
,
4413 #define HAS_ARG 0x0001
4416 #define DEF(option, opt_arg, opt_enum, opt_help) \
4418 #define DEFHEADING(text)
4419 #include "qemu-options.h"
4425 typedef struct QEMUOption
{
4431 static const QEMUOption qemu_options
[] = {
4432 { "h", 0, QEMU_OPTION_h
},
4433 #define DEF(option, opt_arg, opt_enum, opt_help) \
4434 { option, opt_arg, opt_enum },
4435 #define DEFHEADING(text)
4436 #include "qemu-options.h"
4444 struct soundhw soundhw
[] = {
4445 #ifdef HAS_AUDIO_CHOICE
4446 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4452 { .init_isa
= pcspk_audio_init
}
4459 "Creative Sound Blaster 16",
4462 { .init_isa
= SB16_init
}
4466 #ifdef CONFIG_CS4231A
4472 { .init_isa
= cs4231a_init
}
4480 "Yamaha YMF262 (OPL3)",
4482 "Yamaha YM3812 (OPL2)",
4486 { .init_isa
= Adlib_init
}
4493 "Gravis Ultrasound GF1",
4496 { .init_isa
= GUS_init
}
4503 "Intel 82801AA AC97 Audio",
4506 { .init_pci
= ac97_init
}
4510 #ifdef CONFIG_ES1370
4513 "ENSONIQ AudioPCI ES1370",
4516 { .init_pci
= es1370_init
}
4520 #endif /* HAS_AUDIO_CHOICE */
4522 { NULL
, NULL
, 0, 0, { NULL
} }
4525 static void select_soundhw (const char *optarg
)
4529 if (*optarg
== '?') {
4532 printf ("Valid sound card names (comma separated):\n");
4533 for (c
= soundhw
; c
->name
; ++c
) {
4534 printf ("%-11s %s\n", c
->name
, c
->descr
);
4536 printf ("\n-soundhw all will enable all of the above\n");
4537 exit (*optarg
!= '?');
4545 if (!strcmp (optarg
, "all")) {
4546 for (c
= soundhw
; c
->name
; ++c
) {
4554 e
= strchr (p
, ',');
4555 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4557 for (c
= soundhw
; c
->name
; ++c
) {
4558 if (!strncmp (c
->name
, p
, l
)) {
4567 "Unknown sound card name (too big to show)\n");
4570 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4575 p
+= l
+ (e
!= NULL
);
4579 goto show_valid_cards
;
4584 static void select_vgahw (const char *p
)
4588 vga_interface_type
= VGA_NONE
;
4589 if (strstart(p
, "std", &opts
)) {
4590 vga_interface_type
= VGA_STD
;
4591 } else if (strstart(p
, "cirrus", &opts
)) {
4592 vga_interface_type
= VGA_CIRRUS
;
4593 } else if (strstart(p
, "vmware", &opts
)) {
4594 vga_interface_type
= VGA_VMWARE
;
4595 } else if (strstart(p
, "xenfb", &opts
)) {
4596 vga_interface_type
= VGA_XENFB
;
4597 } else if (!strstart(p
, "none", &opts
)) {
4599 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4603 const char *nextopt
;
4605 if (strstart(opts
, ",retrace=", &nextopt
)) {
4607 if (strstart(opts
, "dumb", &nextopt
))
4608 vga_retrace_method
= VGA_RETRACE_DUMB
;
4609 else if (strstart(opts
, "precise", &nextopt
))
4610 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4611 else goto invalid_vga
;
4612 } else goto invalid_vga
;
4618 static int balloon_parse(const char *arg
)
4623 if (!strcmp(arg
, "none")) {
4625 } else if (!strncmp(arg
, "virtio", 6)) {
4627 if (arg
[6] == ',') {
4629 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4630 virtio_balloon_devaddr
= strdup(buf
);
4641 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4643 exit(STATUS_CONTROL_C_EXIT
);
4648 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4652 if(strlen(str
) != 36)
4655 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4656 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4657 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4663 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4669 #define MAX_NET_CLIENTS 32
4673 static void termsig_handler(int signal
)
4675 qemu_system_shutdown_request();
4678 static void sigchld_handler(int signal
)
4680 waitpid(-1, NULL
, WNOHANG
);
4683 static void sighandler_setup(void)
4685 struct sigaction act
;
4687 memset(&act
, 0, sizeof(act
));
4688 act
.sa_handler
= termsig_handler
;
4689 sigaction(SIGINT
, &act
, NULL
);
4690 sigaction(SIGHUP
, &act
, NULL
);
4691 sigaction(SIGTERM
, &act
, NULL
);
4693 act
.sa_handler
= sigchld_handler
;
4694 act
.sa_flags
= SA_NOCLDSTOP
;
4695 sigaction(SIGCHLD
, &act
, NULL
);
4701 /* Look for support files in the same directory as the executable. */
4702 static char *find_datadir(const char *argv0
)
4708 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4715 while (p
!= buf
&& *p
!= '\\')
4718 if (access(buf
, R_OK
) == 0) {
4719 return qemu_strdup(buf
);
4725 /* Find a likely location for support files using the location of the binary.
4726 For installed binaries this will be "$bindir/../share/qemu". When
4727 running from the build tree this will be "$bindir/../pc-bios". */
4728 #define SHARE_SUFFIX "/share/qemu"
4729 #define BUILD_SUFFIX "/pc-bios"
4730 static char *find_datadir(const char *argv0
)
4740 #if defined(__linux__)
4743 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4749 #elif defined(__FreeBSD__)
4752 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4759 /* If we don't have any way of figuring out the actual executable
4760 location then try argv[0]. */
4765 p
= realpath(argv0
, p
);
4773 max_len
= strlen(dir
) +
4774 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4775 res
= qemu_mallocz(max_len
);
4776 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4777 if (access(res
, R_OK
)) {
4778 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4779 if (access(res
, R_OK
)) {
4793 char *qemu_find_file(int type
, const char *name
)
4799 /* If name contains path separators then try it as a straight path. */
4800 if ((strchr(name
, '/') || strchr(name
, '\\'))
4801 && access(name
, R_OK
) == 0) {
4802 return strdup(name
);
4805 case QEMU_FILE_TYPE_BIOS
:
4808 case QEMU_FILE_TYPE_KEYMAP
:
4809 subdir
= "keymaps/";
4814 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4815 buf
= qemu_mallocz(len
);
4816 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4817 if (access(buf
, R_OK
)) {
4824 int main(int argc
, char **argv
, char **envp
)
4826 const char *gdbstub_dev
= NULL
;
4827 uint32_t boot_devices_bitmap
= 0;
4829 int snapshot
, linux_boot
, net_boot
;
4830 const char *initrd_filename
;
4831 const char *kernel_filename
, *kernel_cmdline
;
4832 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4834 DisplayChangeListener
*dcl
;
4835 int cyls
, heads
, secs
, translation
;
4836 const char *net_clients
[MAX_NET_CLIENTS
];
4838 const char *bt_opts
[MAX_BT_CMDLINE
];
4842 const char *r
, *optarg
;
4843 CharDriverState
*monitor_hd
= NULL
;
4844 const char *monitor_device
;
4845 const char *serial_devices
[MAX_SERIAL_PORTS
];
4846 int serial_device_index
;
4847 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4848 int parallel_device_index
;
4849 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4850 int virtio_console_index
;
4851 const char *loadvm
= NULL
;
4852 QEMUMachine
*machine
;
4853 const char *cpu_model
;
4854 const char *usb_devices
[MAX_USB_CMDLINE
];
4855 int usb_devices_index
;
4860 const char *pid_file
= NULL
;
4861 const char *incoming
= NULL
;
4864 struct passwd
*pwd
= NULL
;
4865 const char *chroot_dir
= NULL
;
4866 const char *run_as
= NULL
;
4869 int show_vnc_port
= 0;
4871 qemu_cache_utils_init(envp
);
4873 LIST_INIT (&vm_change_state_head
);
4876 struct sigaction act
;
4877 sigfillset(&act
.sa_mask
);
4879 act
.sa_handler
= SIG_IGN
;
4880 sigaction(SIGPIPE
, &act
, NULL
);
4883 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4884 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4885 QEMU to run on a single CPU */
4890 h
= GetCurrentProcess();
4891 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4892 for(i
= 0; i
< 32; i
++) {
4893 if (mask
& (1 << i
))
4898 SetProcessAffinityMask(h
, mask
);
4904 module_call_init(MODULE_INIT_MACHINE
);
4905 machine
= find_default_machine();
4907 initrd_filename
= NULL
;
4910 kernel_filename
= NULL
;
4911 kernel_cmdline
= "";
4912 cyls
= heads
= secs
= 0;
4913 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4914 monitor_device
= "vc:80Cx24C";
4916 serial_devices
[0] = "vc:80Cx24C";
4917 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4918 serial_devices
[i
] = NULL
;
4919 serial_device_index
= 0;
4921 parallel_devices
[0] = "vc:80Cx24C";
4922 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4923 parallel_devices
[i
] = NULL
;
4924 parallel_device_index
= 0;
4926 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4927 virtio_consoles
[i
] = NULL
;
4928 virtio_console_index
= 0;
4930 for (i
= 0; i
< MAX_NODES
; i
++) {
4932 node_cpumask
[i
] = 0;
4935 usb_devices_index
= 0;
4949 register_watchdogs();
4957 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4959 const QEMUOption
*popt
;
4962 /* Treat --foo the same as -foo. */
4965 popt
= qemu_options
;
4968 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4972 if (!strcmp(popt
->name
, r
+ 1))
4976 if (popt
->flags
& HAS_ARG
) {
4977 if (optind
>= argc
) {
4978 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4982 optarg
= argv
[optind
++];
4987 switch(popt
->index
) {
4989 machine
= find_machine(optarg
);
4992 printf("Supported machines are:\n");
4993 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4995 printf("%-10s %s (alias of %s)\n",
4996 m
->alias
, m
->desc
, m
->name
);
4997 printf("%-10s %s%s\n",
4999 m
->is_default
? " (default)" : "");
5001 exit(*optarg
!= '?');
5004 case QEMU_OPTION_cpu
:
5005 /* hw initialization will check this */
5006 if (*optarg
== '?') {
5007 /* XXX: implement xxx_cpu_list for targets that still miss it */
5008 #if defined(cpu_list)
5009 cpu_list(stdout
, &fprintf
);
5016 case QEMU_OPTION_initrd
:
5017 initrd_filename
= optarg
;
5019 case QEMU_OPTION_hda
:
5021 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5023 hda_index
= drive_add(optarg
, HD_ALIAS
5024 ",cyls=%d,heads=%d,secs=%d%s",
5025 0, cyls
, heads
, secs
,
5026 translation
== BIOS_ATA_TRANSLATION_LBA
?
5028 translation
== BIOS_ATA_TRANSLATION_NONE
?
5029 ",trans=none" : "");
5031 case QEMU_OPTION_hdb
:
5032 case QEMU_OPTION_hdc
:
5033 case QEMU_OPTION_hdd
:
5034 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5036 case QEMU_OPTION_drive
:
5037 drive_add(NULL
, "%s", optarg
);
5039 case QEMU_OPTION_mtdblock
:
5040 drive_add(optarg
, MTD_ALIAS
);
5042 case QEMU_OPTION_sd
:
5043 drive_add(optarg
, SD_ALIAS
);
5045 case QEMU_OPTION_pflash
:
5046 drive_add(optarg
, PFLASH_ALIAS
);
5048 case QEMU_OPTION_snapshot
:
5051 case QEMU_OPTION_hdachs
:
5055 cyls
= strtol(p
, (char **)&p
, 0);
5056 if (cyls
< 1 || cyls
> 16383)
5061 heads
= strtol(p
, (char **)&p
, 0);
5062 if (heads
< 1 || heads
> 16)
5067 secs
= strtol(p
, (char **)&p
, 0);
5068 if (secs
< 1 || secs
> 63)
5072 if (!strcmp(p
, "none"))
5073 translation
= BIOS_ATA_TRANSLATION_NONE
;
5074 else if (!strcmp(p
, "lba"))
5075 translation
= BIOS_ATA_TRANSLATION_LBA
;
5076 else if (!strcmp(p
, "auto"))
5077 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5080 } else if (*p
!= '\0') {
5082 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5085 if (hda_index
!= -1)
5086 snprintf(drives_opt
[hda_index
].opt
,
5087 sizeof(drives_opt
[hda_index
].opt
),
5088 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5089 0, cyls
, heads
, secs
,
5090 translation
== BIOS_ATA_TRANSLATION_LBA
?
5092 translation
== BIOS_ATA_TRANSLATION_NONE
?
5093 ",trans=none" : "");
5096 case QEMU_OPTION_numa
:
5097 if (nb_numa_nodes
>= MAX_NODES
) {
5098 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5103 case QEMU_OPTION_nographic
:
5104 display_type
= DT_NOGRAPHIC
;
5106 #ifdef CONFIG_CURSES
5107 case QEMU_OPTION_curses
:
5108 display_type
= DT_CURSES
;
5111 case QEMU_OPTION_portrait
:
5114 case QEMU_OPTION_kernel
:
5115 kernel_filename
= optarg
;
5117 case QEMU_OPTION_append
:
5118 kernel_cmdline
= optarg
;
5120 case QEMU_OPTION_cdrom
:
5121 drive_add(optarg
, CDROM_ALIAS
);
5123 case QEMU_OPTION_boot
:
5125 static const char * const params
[] = {
5126 "order", "once", "menu", NULL
5128 char buf
[sizeof(boot_devices
)];
5129 char *standard_boot_devices
;
5132 if (!strchr(optarg
, '=')) {
5134 pstrcpy(buf
, sizeof(buf
), optarg
);
5135 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5137 "qemu: unknown boot parameter '%s' in '%s'\n",
5143 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5144 boot_devices_bitmap
= parse_bootdevices(buf
);
5145 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5148 if (get_param_value(buf
, sizeof(buf
),
5150 boot_devices_bitmap
|= parse_bootdevices(buf
);
5151 standard_boot_devices
= qemu_strdup(boot_devices
);
5152 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5153 qemu_register_reset(restore_boot_devices
,
5154 standard_boot_devices
);
5156 if (get_param_value(buf
, sizeof(buf
),
5158 if (!strcmp(buf
, "on")) {
5160 } else if (!strcmp(buf
, "off")) {
5164 "qemu: invalid option value '%s'\n",
5172 case QEMU_OPTION_fda
:
5173 case QEMU_OPTION_fdb
:
5174 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5177 case QEMU_OPTION_no_fd_bootchk
:
5181 case QEMU_OPTION_net
:
5182 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5183 fprintf(stderr
, "qemu: too many network clients\n");
5186 net_clients
[nb_net_clients
] = optarg
;
5190 case QEMU_OPTION_tftp
:
5191 legacy_tftp_prefix
= optarg
;
5193 case QEMU_OPTION_bootp
:
5194 legacy_bootp_filename
= optarg
;
5197 case QEMU_OPTION_smb
:
5198 net_slirp_smb(optarg
);
5201 case QEMU_OPTION_redir
:
5202 net_slirp_redir(optarg
);
5205 case QEMU_OPTION_bt
:
5206 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5207 fprintf(stderr
, "qemu: too many bluetooth options\n");
5210 bt_opts
[nb_bt_opts
++] = optarg
;
5213 case QEMU_OPTION_audio_help
:
5217 case QEMU_OPTION_soundhw
:
5218 select_soundhw (optarg
);
5224 case QEMU_OPTION_version
:
5228 case QEMU_OPTION_m
: {
5232 value
= strtoul(optarg
, &ptr
, 10);
5234 case 0: case 'M': case 'm':
5241 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5245 /* On 32-bit hosts, QEMU is limited by virtual address space */
5246 if (value
> (2047 << 20)
5247 #ifndef CONFIG_KQEMU
5248 && HOST_LONG_BITS
== 32
5251 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5254 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5255 fprintf(stderr
, "qemu: ram size too large\n");
5264 const CPULogItem
*item
;
5266 mask
= cpu_str_to_log_mask(optarg
);
5268 printf("Log items (comma separated):\n");
5269 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5270 printf("%-10s %s\n", item
->name
, item
->help
);
5278 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5280 case QEMU_OPTION_gdb
:
5281 gdbstub_dev
= optarg
;
5286 case QEMU_OPTION_bios
:
5289 case QEMU_OPTION_singlestep
:
5297 keyboard_layout
= optarg
;
5300 case QEMU_OPTION_localtime
:
5303 case QEMU_OPTION_vga
:
5304 select_vgahw (optarg
);
5306 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5312 w
= strtol(p
, (char **)&p
, 10);
5315 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5321 h
= strtol(p
, (char **)&p
, 10);
5326 depth
= strtol(p
, (char **)&p
, 10);
5327 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5328 depth
!= 24 && depth
!= 32)
5330 } else if (*p
== '\0') {
5331 depth
= graphic_depth
;
5338 graphic_depth
= depth
;
5342 case QEMU_OPTION_echr
:
5345 term_escape_char
= strtol(optarg
, &r
, 0);
5347 printf("Bad argument to echr\n");
5350 case QEMU_OPTION_monitor
:
5351 monitor_device
= optarg
;
5353 case QEMU_OPTION_serial
:
5354 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5355 fprintf(stderr
, "qemu: too many serial ports\n");
5358 serial_devices
[serial_device_index
] = optarg
;
5359 serial_device_index
++;
5361 case QEMU_OPTION_watchdog
:
5362 i
= select_watchdog(optarg
);
5364 exit (i
== 1 ? 1 : 0);
5366 case QEMU_OPTION_watchdog_action
:
5367 if (select_watchdog_action(optarg
) == -1) {
5368 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5372 case QEMU_OPTION_virtiocon
:
5373 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5374 fprintf(stderr
, "qemu: too many virtio consoles\n");
5377 virtio_consoles
[virtio_console_index
] = optarg
;
5378 virtio_console_index
++;
5380 case QEMU_OPTION_parallel
:
5381 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5382 fprintf(stderr
, "qemu: too many parallel ports\n");
5385 parallel_devices
[parallel_device_index
] = optarg
;
5386 parallel_device_index
++;
5388 case QEMU_OPTION_loadvm
:
5391 case QEMU_OPTION_full_screen
:
5395 case QEMU_OPTION_no_frame
:
5398 case QEMU_OPTION_alt_grab
:
5401 case QEMU_OPTION_no_quit
:
5404 case QEMU_OPTION_sdl
:
5405 display_type
= DT_SDL
;
5408 case QEMU_OPTION_pidfile
:
5412 case QEMU_OPTION_win2k_hack
:
5413 win2k_install_hack
= 1;
5415 case QEMU_OPTION_rtc_td_hack
:
5418 case QEMU_OPTION_acpitable
:
5419 if(acpi_table_add(optarg
) < 0) {
5420 fprintf(stderr
, "Wrong acpi table provided\n");
5424 case QEMU_OPTION_smbios
:
5425 if(smbios_entry_add(optarg
) < 0) {
5426 fprintf(stderr
, "Wrong smbios provided\n");
5432 case QEMU_OPTION_enable_kqemu
:
5435 case QEMU_OPTION_kernel_kqemu
:
5440 case QEMU_OPTION_enable_kvm
:
5447 case QEMU_OPTION_usb
:
5450 case QEMU_OPTION_usbdevice
:
5452 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5453 fprintf(stderr
, "Too many USB devices\n");
5456 usb_devices
[usb_devices_index
] = optarg
;
5457 usb_devices_index
++;
5459 case QEMU_OPTION_smp
:
5460 smp_cpus
= atoi(optarg
);
5462 fprintf(stderr
, "Invalid number of CPUs\n");
5466 case QEMU_OPTION_vnc
:
5467 display_type
= DT_VNC
;
5468 vnc_display
= optarg
;
5471 case QEMU_OPTION_no_acpi
:
5474 case QEMU_OPTION_no_hpet
:
5477 case QEMU_OPTION_balloon
:
5478 if (balloon_parse(optarg
) < 0) {
5479 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5484 case QEMU_OPTION_no_reboot
:
5487 case QEMU_OPTION_no_shutdown
:
5490 case QEMU_OPTION_show_cursor
:
5493 case QEMU_OPTION_uuid
:
5494 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5495 fprintf(stderr
, "Fail to parse UUID string."
5496 " Wrong format.\n");
5501 case QEMU_OPTION_daemonize
:
5505 case QEMU_OPTION_option_rom
:
5506 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5507 fprintf(stderr
, "Too many option ROMs\n");
5510 option_rom
[nb_option_roms
] = optarg
;
5513 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5514 case QEMU_OPTION_semihosting
:
5515 semihosting_enabled
= 1;
5518 case QEMU_OPTION_name
:
5519 qemu_name
= qemu_strdup(optarg
);
5521 char *p
= strchr(qemu_name
, ',');
5524 if (strncmp(p
, "process=", 8)) {
5525 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5533 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5534 case QEMU_OPTION_prom_env
:
5535 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5536 fprintf(stderr
, "Too many prom variables\n");
5539 prom_envs
[nb_prom_envs
] = optarg
;
5544 case QEMU_OPTION_old_param
:
5548 case QEMU_OPTION_clock
:
5549 configure_alarms(optarg
);
5551 case QEMU_OPTION_startdate
:
5554 time_t rtc_start_date
;
5555 if (!strcmp(optarg
, "now")) {
5556 rtc_date_offset
= -1;
5558 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5566 } else if (sscanf(optarg
, "%d-%d-%d",
5569 &tm
.tm_mday
) == 3) {
5578 rtc_start_date
= mktimegm(&tm
);
5579 if (rtc_start_date
== -1) {
5581 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5582 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5585 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5589 case QEMU_OPTION_tb_size
:
5590 tb_size
= strtol(optarg
, NULL
, 0);
5594 case QEMU_OPTION_icount
:
5596 if (strcmp(optarg
, "auto") == 0) {
5597 icount_time_shift
= -1;
5599 icount_time_shift
= strtol(optarg
, NULL
, 0);
5602 case QEMU_OPTION_incoming
:
5606 case QEMU_OPTION_chroot
:
5607 chroot_dir
= optarg
;
5609 case QEMU_OPTION_runas
:
5614 case QEMU_OPTION_xen_domid
:
5615 xen_domid
= atoi(optarg
);
5617 case QEMU_OPTION_xen_create
:
5618 xen_mode
= XEN_CREATE
;
5620 case QEMU_OPTION_xen_attach
:
5621 xen_mode
= XEN_ATTACH
;
5628 /* If no data_dir is specified then try to find it relative to the
5631 data_dir
= find_datadir(argv
[0]);
5633 /* If all else fails use the install patch specified when building. */
5635 data_dir
= CONFIG_QEMU_SHAREDIR
;
5638 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5639 if (kvm_allowed
&& kqemu_allowed
) {
5641 "You can not enable both KVM and kqemu at the same time\n");
5646 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5647 if (smp_cpus
> machine
->max_cpus
) {
5648 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5649 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5654 if (display_type
== DT_NOGRAPHIC
) {
5655 if (serial_device_index
== 0)
5656 serial_devices
[0] = "stdio";
5657 if (parallel_device_index
== 0)
5658 parallel_devices
[0] = "null";
5659 if (strncmp(monitor_device
, "vc", 2) == 0)
5660 monitor_device
= "stdio";
5667 if (pipe(fds
) == -1)
5678 len
= read(fds
[0], &status
, 1);
5679 if (len
== -1 && (errno
== EINTR
))
5684 else if (status
== 1) {
5685 fprintf(stderr
, "Could not acquire pidfile\n");
5702 signal(SIGTSTP
, SIG_IGN
);
5703 signal(SIGTTOU
, SIG_IGN
);
5704 signal(SIGTTIN
, SIG_IGN
);
5707 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5710 write(fds
[1], &status
, 1);
5712 fprintf(stderr
, "Could not acquire pid file\n");
5721 if (qemu_init_main_loop()) {
5722 fprintf(stderr
, "qemu_init_main_loop failed\n");
5725 linux_boot
= (kernel_filename
!= NULL
);
5727 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5728 fprintf(stderr
, "-append only allowed with -kernel option\n");
5732 if (!linux_boot
&& initrd_filename
!= NULL
) {
5733 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5737 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5740 if (init_timer_alarm() < 0) {
5741 fprintf(stderr
, "could not initialize alarm timer\n");
5744 if (use_icount
&& icount_time_shift
< 0) {
5746 /* 125MIPS seems a reasonable initial guess at the guest speed.
5747 It will be corrected fairly quickly anyway. */
5748 icount_time_shift
= 3;
5749 init_icount_adjust();
5756 /* init network clients */
5757 if (nb_net_clients
== 0) {
5758 /* if no clients, we use a default config */
5759 net_clients
[nb_net_clients
++] = "nic";
5761 net_clients
[nb_net_clients
++] = "user";
5765 for(i
= 0;i
< nb_net_clients
; i
++) {
5766 if (net_client_parse(net_clients
[i
]) < 0)
5770 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5771 net_set_boot_mask(net_boot
);
5775 /* init the bluetooth world */
5776 for (i
= 0; i
< nb_bt_opts
; i
++)
5777 if (bt_parse(bt_opts
[i
]))
5780 /* init the memory */
5782 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5785 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5786 guest ram allocation. It needs to go away. */
5787 if (kqemu_allowed
) {
5788 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5789 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5790 if (!kqemu_phys_ram_base
) {
5791 fprintf(stderr
, "Could not allocate physical memory\n");
5797 /* init the dynamic translator */
5798 cpu_exec_init_all(tb_size
* 1024 * 1024);
5802 /* we always create the cdrom drive, even if no disk is there */
5804 if (nb_drives_opt
< MAX_DRIVES
)
5805 drive_add(NULL
, CDROM_ALIAS
);
5807 /* we always create at least one floppy */
5809 if (nb_drives_opt
< MAX_DRIVES
)
5810 drive_add(NULL
, FD_ALIAS
, 0);
5812 /* we always create one sd slot, even if no card is in it */
5814 if (nb_drives_opt
< MAX_DRIVES
)
5815 drive_add(NULL
, SD_ALIAS
);
5817 /* open the virtual block devices */
5819 for(i
= 0; i
< nb_drives_opt
; i
++)
5820 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5823 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5824 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5827 /* must be after terminal init, SDL library changes signal handlers */
5831 /* Maintain compatibility with multiple stdio monitors */
5832 if (!strcmp(monitor_device
,"stdio")) {
5833 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5834 const char *devname
= serial_devices
[i
];
5835 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5836 monitor_device
= NULL
;
5838 } else if (devname
&& !strcmp(devname
,"stdio")) {
5839 monitor_device
= NULL
;
5840 serial_devices
[i
] = "mon:stdio";
5846 if (nb_numa_nodes
> 0) {
5849 if (nb_numa_nodes
> smp_cpus
) {
5850 nb_numa_nodes
= smp_cpus
;
5853 /* If no memory size if given for any node, assume the default case
5854 * and distribute the available memory equally across all nodes
5856 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5857 if (node_mem
[i
] != 0)
5860 if (i
== nb_numa_nodes
) {
5861 uint64_t usedmem
= 0;
5863 /* On Linux, the each node's border has to be 8MB aligned,
5864 * the final node gets the rest.
5866 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5867 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5868 usedmem
+= node_mem
[i
];
5870 node_mem
[i
] = ram_size
- usedmem
;
5873 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5874 if (node_cpumask
[i
] != 0)
5877 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5878 * must cope with this anyway, because there are BIOSes out there in
5879 * real machines which also use this scheme.
5881 if (i
== nb_numa_nodes
) {
5882 for (i
= 0; i
< smp_cpus
; i
++) {
5883 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5888 if (kvm_enabled()) {
5891 ret
= kvm_init(smp_cpus
);
5893 fprintf(stderr
, "failed to initialize KVM\n");
5898 if (monitor_device
) {
5899 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5901 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5906 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5907 const char *devname
= serial_devices
[i
];
5908 if (devname
&& strcmp(devname
, "none")) {
5910 snprintf(label
, sizeof(label
), "serial%d", i
);
5911 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5912 if (!serial_hds
[i
]) {
5913 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5920 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5921 const char *devname
= parallel_devices
[i
];
5922 if (devname
&& strcmp(devname
, "none")) {
5924 snprintf(label
, sizeof(label
), "parallel%d", i
);
5925 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5926 if (!parallel_hds
[i
]) {
5927 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5934 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5935 const char *devname
= virtio_consoles
[i
];
5936 if (devname
&& strcmp(devname
, "none")) {
5938 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5939 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5940 if (!virtcon_hds
[i
]) {
5941 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5948 module_call_init(MODULE_INIT_DEVICE
);
5950 if (machine
->compat_props
) {
5951 qdev_prop_register_compat(machine
->compat_props
);
5953 machine
->init(ram_size
, boot_devices
,
5954 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5957 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5958 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5959 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5965 current_machine
= machine
;
5967 /* init USB devices */
5969 for(i
= 0; i
< usb_devices_index
; i
++) {
5970 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5971 fprintf(stderr
, "Warning: could not add USB device %s\n",
5978 dumb_display_init();
5979 /* just use the first displaystate for the moment */
5982 if (display_type
== DT_DEFAULT
) {
5983 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5984 display_type
= DT_SDL
;
5986 display_type
= DT_VNC
;
5987 vnc_display
= "localhost:0,to=99";
5993 switch (display_type
) {
5996 #if defined(CONFIG_CURSES)
5998 curses_display_init(ds
, full_screen
);
6001 #if defined(CONFIG_SDL)
6003 sdl_display_init(ds
, full_screen
, no_frame
);
6005 #elif defined(CONFIG_COCOA)
6007 cocoa_display_init(ds
, full_screen
);
6011 vnc_display_init(ds
);
6012 if (vnc_display_open(ds
, vnc_display
) < 0)
6015 if (show_vnc_port
) {
6016 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6024 dcl
= ds
->listeners
;
6025 while (dcl
!= NULL
) {
6026 if (dcl
->dpy_refresh
!= NULL
) {
6027 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6028 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6033 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6034 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6035 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6038 text_consoles_set_display(display_state
);
6039 qemu_chr_initial_reset();
6041 if (monitor_device
&& monitor_hd
)
6042 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6044 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6045 const char *devname
= serial_devices
[i
];
6046 if (devname
&& strcmp(devname
, "none")) {
6047 if (strstart(devname
, "vc", 0))
6048 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6052 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6053 const char *devname
= parallel_devices
[i
];
6054 if (devname
&& strcmp(devname
, "none")) {
6055 if (strstart(devname
, "vc", 0))
6056 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6060 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6061 const char *devname
= virtio_consoles
[i
];
6062 if (virtcon_hds
[i
] && devname
) {
6063 if (strstart(devname
, "vc", 0))
6064 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6068 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6069 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6075 do_loadvm(cur_mon
, loadvm
);
6078 qemu_start_incoming_migration(incoming
);
6079 } else if (autostart
) {
6089 len
= write(fds
[1], &status
, 1);
6090 if (len
== -1 && (errno
== EINTR
))
6097 TFR(fd
= open("/dev/null", O_RDWR
));
6103 pwd
= getpwnam(run_as
);
6105 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6111 if (chroot(chroot_dir
) < 0) {
6112 fprintf(stderr
, "chroot failed\n");
6119 if (setgid(pwd
->pw_gid
) < 0) {
6120 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6123 if (setuid(pwd
->pw_uid
) < 0) {
6124 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6127 if (setuid(0) != -1) {
6128 fprintf(stderr
, "Dropping privileges failed\n");