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"
155 #include "qemu-timer.h"
156 #include "qemu-char.h"
157 #include "cache-utils.h"
160 #include "audio/audio.h"
161 #include "migration.h"
164 #include "qemu-option.h"
168 #include "exec-all.h"
170 #include "qemu_socket.h"
172 #include "slirp/libslirp.h"
175 //#define DEBUG_SLIRP
177 #define DEFAULT_RAM_SIZE 128
179 static const char *data_dir
;
180 const char *bios_name
= NULL
;
181 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
182 to store the VM snapshots */
183 struct drivelist drives
= TAILQ_HEAD_INITIALIZER(drives
);
184 struct driveoptlist driveopts
= TAILQ_HEAD_INITIALIZER(driveopts
);
185 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
186 static DisplayState
*display_state
;
187 DisplayType display_type
= DT_DEFAULT
;
188 const char* keyboard_layout
= NULL
;
189 int64_t ticks_per_sec
;
192 NICInfo nd_table
[MAX_NICS
];
194 static int autostart
;
195 static int rtc_utc
= 1;
196 static int rtc_date_offset
= -1; /* -1 means no change */
197 int cirrus_vga_enabled
= 1;
198 int std_vga_enabled
= 0;
199 int vmsvga_enabled
= 0;
200 int xenfb_enabled
= 0;
202 int graphic_width
= 1024;
203 int graphic_height
= 768;
204 int graphic_depth
= 8;
206 int graphic_width
= 800;
207 int graphic_height
= 600;
208 int graphic_depth
= 15;
210 static int full_screen
= 0;
212 static int no_frame
= 0;
215 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
216 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
217 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
219 int win2k_install_hack
= 0;
225 const char *vnc_display
;
226 int acpi_enabled
= 1;
228 int virtio_balloon
= 1;
229 const char *virtio_balloon_devaddr
;
234 int graphic_rotate
= 0;
238 WatchdogTimerModel
*watchdog
= NULL
;
239 int watchdog_action
= WDT_RESET
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
248 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
249 unsigned int nb_prom_envs
= 0;
250 const char *prom_envs
[MAX_PROM_ENVS
];
255 uint64_t node_mem
[MAX_NODES
];
256 uint64_t node_cpumask
[MAX_NODES
];
258 static CPUState
*cur_cpu
;
259 static CPUState
*next_cpu
;
260 static int timer_alarm_pending
= 1;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
269 static QEMUTimer
*nographic_timer
;
271 uint8_t qemu_uuid
[16];
273 static QEMUBootSetHandler
*boot_set_handler
;
274 static void *boot_set_opaque
;
276 /***********************************************************/
277 /* x86 ISA bus support */
279 target_phys_addr_t isa_mem_base
= 0;
282 /***********************************************************/
283 void hw_error(const char *fmt
, ...)
289 fprintf(stderr
, "qemu: hardware error: ");
290 vfprintf(stderr
, fmt
, ap
);
291 fprintf(stderr
, "\n");
292 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
293 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
295 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
297 cpu_dump_state(env
, stderr
, fprintf
, 0);
304 static void set_proc_name(const char *s
)
310 name
[sizeof(name
) - 1] = 0;
311 strncpy(name
, s
, sizeof(name
));
312 /* Could rewrite argv[0] too, but that's a bit more complicated.
313 This simple way is enough for `top'. */
314 prctl(PR_SET_NAME
, name
);
321 static QEMUBalloonEvent
*qemu_balloon_event
;
322 void *qemu_balloon_event_opaque
;
324 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
326 qemu_balloon_event
= func
;
327 qemu_balloon_event_opaque
= opaque
;
330 void qemu_balloon(ram_addr_t target
)
332 if (qemu_balloon_event
)
333 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
336 ram_addr_t
qemu_balloon_status(void)
338 if (qemu_balloon_event
)
339 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
343 /***********************************************************/
346 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
347 static void *qemu_put_kbd_event_opaque
;
348 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
349 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
351 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
353 qemu_put_kbd_event_opaque
= opaque
;
354 qemu_put_kbd_event
= func
;
357 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
358 void *opaque
, int absolute
,
361 QEMUPutMouseEntry
*s
, *cursor
;
363 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
365 s
->qemu_put_mouse_event
= func
;
366 s
->qemu_put_mouse_event_opaque
= opaque
;
367 s
->qemu_put_mouse_event_absolute
= absolute
;
368 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
371 if (!qemu_put_mouse_event_head
) {
372 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
376 cursor
= qemu_put_mouse_event_head
;
377 while (cursor
->next
!= NULL
)
378 cursor
= cursor
->next
;
381 qemu_put_mouse_event_current
= s
;
386 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
388 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
390 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
393 cursor
= qemu_put_mouse_event_head
;
394 while (cursor
!= NULL
&& cursor
!= entry
) {
396 cursor
= cursor
->next
;
399 if (cursor
== NULL
) // does not exist or list empty
401 else if (prev
== NULL
) { // entry is head
402 qemu_put_mouse_event_head
= cursor
->next
;
403 if (qemu_put_mouse_event_current
== entry
)
404 qemu_put_mouse_event_current
= cursor
->next
;
405 qemu_free(entry
->qemu_put_mouse_event_name
);
410 prev
->next
= entry
->next
;
412 if (qemu_put_mouse_event_current
== entry
)
413 qemu_put_mouse_event_current
= prev
;
415 qemu_free(entry
->qemu_put_mouse_event_name
);
419 void kbd_put_keycode(int keycode
)
421 if (qemu_put_kbd_event
) {
422 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
426 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
428 QEMUPutMouseEvent
*mouse_event
;
429 void *mouse_event_opaque
;
432 if (!qemu_put_mouse_event_current
) {
437 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
439 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
442 if (graphic_rotate
) {
443 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
446 width
= graphic_width
- 1;
447 mouse_event(mouse_event_opaque
,
448 width
- dy
, dx
, dz
, buttons_state
);
450 mouse_event(mouse_event_opaque
,
451 dx
, dy
, dz
, buttons_state
);
455 int kbd_mouse_is_absolute(void)
457 if (!qemu_put_mouse_event_current
)
460 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
463 void do_info_mice(Monitor
*mon
)
465 QEMUPutMouseEntry
*cursor
;
468 if (!qemu_put_mouse_event_head
) {
469 monitor_printf(mon
, "No mouse devices connected\n");
473 monitor_printf(mon
, "Mouse devices available:\n");
474 cursor
= qemu_put_mouse_event_head
;
475 while (cursor
!= NULL
) {
476 monitor_printf(mon
, "%c Mouse #%d: %s\n",
477 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
478 index
, cursor
->qemu_put_mouse_event_name
);
480 cursor
= cursor
->next
;
484 void do_mouse_set(Monitor
*mon
, int index
)
486 QEMUPutMouseEntry
*cursor
;
489 if (!qemu_put_mouse_event_head
) {
490 monitor_printf(mon
, "No mouse devices connected\n");
494 cursor
= qemu_put_mouse_event_head
;
495 while (cursor
!= NULL
&& index
!= i
) {
497 cursor
= cursor
->next
;
501 qemu_put_mouse_event_current
= cursor
;
503 monitor_printf(mon
, "Mouse at given index not found\n");
506 /* compute with 96 bit intermediate result: (a*b)/c */
507 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
512 #ifdef WORDS_BIGENDIAN
522 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
523 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
526 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
530 /***********************************************************/
531 /* real time host monotonic timer */
533 #define QEMU_TIMER_BASE 1000000000LL
537 static int64_t clock_freq
;
539 static void init_get_clock(void)
543 ret
= QueryPerformanceFrequency(&freq
);
545 fprintf(stderr
, "Could not calibrate ticks\n");
548 clock_freq
= freq
.QuadPart
;
551 static int64_t get_clock(void)
554 QueryPerformanceCounter(&ti
);
555 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
560 static int use_rt_clock
;
562 static void init_get_clock(void)
565 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
566 || defined(__DragonFly__)
569 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
576 static int64_t get_clock(void)
578 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
579 || defined(__DragonFly__)
582 clock_gettime(CLOCK_MONOTONIC
, &ts
);
583 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
587 /* XXX: using gettimeofday leads to problems if the date
588 changes, so it should be avoided. */
590 gettimeofday(&tv
, NULL
);
591 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
596 /* Return the virtual CPU time, based on the instruction counter. */
597 static int64_t cpu_get_icount(void)
600 CPUState
*env
= cpu_single_env
;;
601 icount
= qemu_icount
;
604 fprintf(stderr
, "Bad clock read\n");
605 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
607 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
610 /***********************************************************/
611 /* guest cycle counter */
613 static int64_t cpu_ticks_prev
;
614 static int64_t cpu_ticks_offset
;
615 static int64_t cpu_clock_offset
;
616 static int cpu_ticks_enabled
;
618 /* return the host CPU cycle counter and handle stop/restart */
619 int64_t cpu_get_ticks(void)
622 return cpu_get_icount();
624 if (!cpu_ticks_enabled
) {
625 return cpu_ticks_offset
;
628 ticks
= cpu_get_real_ticks();
629 if (cpu_ticks_prev
> ticks
) {
630 /* Note: non increasing ticks may happen if the host uses
632 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
634 cpu_ticks_prev
= ticks
;
635 return ticks
+ cpu_ticks_offset
;
639 /* return the host CPU monotonic timer and handle stop/restart */
640 static int64_t cpu_get_clock(void)
643 if (!cpu_ticks_enabled
) {
644 return cpu_clock_offset
;
647 return ti
+ cpu_clock_offset
;
651 /* enable cpu_get_ticks() */
652 void cpu_enable_ticks(void)
654 if (!cpu_ticks_enabled
) {
655 cpu_ticks_offset
-= cpu_get_real_ticks();
656 cpu_clock_offset
-= get_clock();
657 cpu_ticks_enabled
= 1;
661 /* disable cpu_get_ticks() : the clock is stopped. You must not call
662 cpu_get_ticks() after that. */
663 void cpu_disable_ticks(void)
665 if (cpu_ticks_enabled
) {
666 cpu_ticks_offset
= cpu_get_ticks();
667 cpu_clock_offset
= cpu_get_clock();
668 cpu_ticks_enabled
= 0;
672 /***********************************************************/
675 #define QEMU_TIMER_REALTIME 0
676 #define QEMU_TIMER_VIRTUAL 1
680 /* XXX: add frequency */
688 struct QEMUTimer
*next
;
691 struct qemu_alarm_timer
{
695 int (*start
)(struct qemu_alarm_timer
*t
);
696 void (*stop
)(struct qemu_alarm_timer
*t
);
697 void (*rearm
)(struct qemu_alarm_timer
*t
);
701 #define ALARM_FLAG_DYNTICKS 0x1
702 #define ALARM_FLAG_EXPIRED 0x2
704 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
706 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
709 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
711 if (!alarm_has_dynticks(t
))
717 /* TODO: MIN_TIMER_REARM_US should be optimized */
718 #define MIN_TIMER_REARM_US 250
720 static struct qemu_alarm_timer
*alarm_timer
;
724 struct qemu_alarm_win32
{
727 } alarm_win32_data
= {0, -1};
729 static int win32_start_timer(struct qemu_alarm_timer
*t
);
730 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
731 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
735 static int unix_start_timer(struct qemu_alarm_timer
*t
);
736 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
740 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
741 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
742 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
744 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
745 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
747 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
748 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
750 #endif /* __linux__ */
754 /* Correlation between real and virtual time is always going to be
755 fairly approximate, so ignore small variation.
756 When the guest is idle real and virtual time will be aligned in
758 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
760 static void icount_adjust(void)
765 static int64_t last_delta
;
766 /* If the VM is not running, then do nothing. */
770 cur_time
= cpu_get_clock();
771 cur_icount
= qemu_get_clock(vm_clock
);
772 delta
= cur_icount
- cur_time
;
773 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
775 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
776 && icount_time_shift
> 0) {
777 /* The guest is getting too far ahead. Slow time down. */
781 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
782 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
783 /* The guest is getting too far behind. Speed time up. */
787 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
790 static void icount_adjust_rt(void * opaque
)
792 qemu_mod_timer(icount_rt_timer
,
793 qemu_get_clock(rt_clock
) + 1000);
797 static void icount_adjust_vm(void * opaque
)
799 qemu_mod_timer(icount_vm_timer
,
800 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
804 static void init_icount_adjust(void)
806 /* Have both realtime and virtual time triggers for speed adjustment.
807 The realtime trigger catches emulated time passing too slowly,
808 the virtual time trigger catches emulated time passing too fast.
809 Realtime triggers occur even when idle, so use them less frequently
811 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
812 qemu_mod_timer(icount_rt_timer
,
813 qemu_get_clock(rt_clock
) + 1000);
814 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
815 qemu_mod_timer(icount_vm_timer
,
816 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
819 static struct qemu_alarm_timer alarm_timers
[] = {
822 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
823 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
824 /* HPET - if available - is preferred */
825 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
826 /* ...otherwise try RTC */
827 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
829 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
831 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
832 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
833 {"win32", 0, win32_start_timer
,
834 win32_stop_timer
, NULL
, &alarm_win32_data
},
839 static void show_available_alarms(void)
843 printf("Available alarm timers, in order of precedence:\n");
844 for (i
= 0; alarm_timers
[i
].name
; i
++)
845 printf("%s\n", alarm_timers
[i
].name
);
848 static void configure_alarms(char const *opt
)
852 int count
= ARRAY_SIZE(alarm_timers
) - 1;
855 struct qemu_alarm_timer tmp
;
857 if (!strcmp(opt
, "?")) {
858 show_available_alarms();
864 /* Reorder the array */
865 name
= strtok(arg
, ",");
867 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
868 if (!strcmp(alarm_timers
[i
].name
, name
))
873 fprintf(stderr
, "Unknown clock %s\n", name
);
882 tmp
= alarm_timers
[i
];
883 alarm_timers
[i
] = alarm_timers
[cur
];
884 alarm_timers
[cur
] = tmp
;
888 name
= strtok(NULL
, ",");
894 /* Disable remaining timers */
895 for (i
= cur
; i
< count
; i
++)
896 alarm_timers
[i
].name
= NULL
;
898 show_available_alarms();
906 static QEMUTimer
*active_timers
[2];
908 static QEMUClock
*qemu_new_clock(int type
)
911 clock
= qemu_mallocz(sizeof(QEMUClock
));
916 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
920 ts
= qemu_mallocz(sizeof(QEMUTimer
));
927 void qemu_free_timer(QEMUTimer
*ts
)
932 /* stop a timer, but do not dealloc it */
933 void qemu_del_timer(QEMUTimer
*ts
)
937 /* NOTE: this code must be signal safe because
938 qemu_timer_expired() can be called from a signal. */
939 pt
= &active_timers
[ts
->clock
->type
];
952 /* modify the current timer so that it will be fired when current_time
953 >= expire_time. The corresponding callback will be called. */
954 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
960 /* add the timer in the sorted list */
961 /* NOTE: this code must be signal safe because
962 qemu_timer_expired() can be called from a signal. */
963 pt
= &active_timers
[ts
->clock
->type
];
968 if (t
->expire_time
> expire_time
)
972 ts
->expire_time
= expire_time
;
976 /* Rearm if necessary */
977 if (pt
== &active_timers
[ts
->clock
->type
]) {
978 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
979 qemu_rearm_alarm_timer(alarm_timer
);
981 /* Interrupt execution to force deadline recalculation. */
987 int qemu_timer_pending(QEMUTimer
*ts
)
990 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
997 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1001 return (timer_head
->expire_time
<= current_time
);
1004 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1010 if (!ts
|| ts
->expire_time
> current_time
)
1012 /* remove timer from the list before calling the callback */
1013 *ptimer_head
= ts
->next
;
1016 /* run the callback (the timer list can be modified) */
1021 int64_t qemu_get_clock(QEMUClock
*clock
)
1023 switch(clock
->type
) {
1024 case QEMU_TIMER_REALTIME
:
1025 return get_clock() / 1000000;
1027 case QEMU_TIMER_VIRTUAL
:
1029 return cpu_get_icount();
1031 return cpu_get_clock();
1036 static void init_timers(void)
1039 ticks_per_sec
= QEMU_TIMER_BASE
;
1040 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1041 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1045 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1047 uint64_t expire_time
;
1049 if (qemu_timer_pending(ts
)) {
1050 expire_time
= ts
->expire_time
;
1054 qemu_put_be64(f
, expire_time
);
1057 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1059 uint64_t expire_time
;
1061 expire_time
= qemu_get_be64(f
);
1062 if (expire_time
!= -1) {
1063 qemu_mod_timer(ts
, expire_time
);
1069 static void timer_save(QEMUFile
*f
, void *opaque
)
1071 if (cpu_ticks_enabled
) {
1072 hw_error("cannot save state if virtual timers are running");
1074 qemu_put_be64(f
, cpu_ticks_offset
);
1075 qemu_put_be64(f
, ticks_per_sec
);
1076 qemu_put_be64(f
, cpu_clock_offset
);
1079 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1081 if (version_id
!= 1 && version_id
!= 2)
1083 if (cpu_ticks_enabled
) {
1086 cpu_ticks_offset
=qemu_get_be64(f
);
1087 ticks_per_sec
=qemu_get_be64(f
);
1088 if (version_id
== 2) {
1089 cpu_clock_offset
=qemu_get_be64(f
);
1094 static void qemu_event_increment(void);
1097 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1098 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1101 static void host_alarm_handler(int host_signum
)
1105 #define DISP_FREQ 1000
1107 static int64_t delta_min
= INT64_MAX
;
1108 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1110 ti
= qemu_get_clock(vm_clock
);
1111 if (last_clock
!= 0) {
1112 delta
= ti
- last_clock
;
1113 if (delta
< delta_min
)
1115 if (delta
> delta_max
)
1118 if (++count
== DISP_FREQ
) {
1119 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1120 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1121 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1122 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1123 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1125 delta_min
= INT64_MAX
;
1133 if (alarm_has_dynticks(alarm_timer
) ||
1135 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1136 qemu_get_clock(vm_clock
))) ||
1137 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1138 qemu_get_clock(rt_clock
))) {
1139 qemu_event_increment();
1140 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1142 #ifndef CONFIG_IOTHREAD
1144 /* stop the currently executing cpu because a timer occured */
1147 if (next_cpu
->kqemu_enabled
) {
1148 kqemu_cpu_interrupt(next_cpu
);
1153 timer_alarm_pending
= 1;
1154 qemu_notify_event();
1158 static int64_t qemu_next_deadline(void)
1162 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1163 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1164 qemu_get_clock(vm_clock
);
1166 /* To avoid problems with overflow limit this to 2^32. */
1176 #if defined(__linux__) || defined(_WIN32)
1177 static uint64_t qemu_next_deadline_dyntick(void)
1185 delta
= (qemu_next_deadline() + 999) / 1000;
1187 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1188 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1189 qemu_get_clock(rt_clock
))*1000;
1190 if (rtdelta
< delta
)
1194 if (delta
< MIN_TIMER_REARM_US
)
1195 delta
= MIN_TIMER_REARM_US
;
1203 /* Sets a specific flag */
1204 static int fcntl_setfl(int fd
, int flag
)
1208 flags
= fcntl(fd
, F_GETFL
);
1212 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1218 #if defined(__linux__)
1220 #define RTC_FREQ 1024
1222 static void enable_sigio_timer(int fd
)
1224 struct sigaction act
;
1227 sigfillset(&act
.sa_mask
);
1229 act
.sa_handler
= host_alarm_handler
;
1231 sigaction(SIGIO
, &act
, NULL
);
1232 fcntl_setfl(fd
, O_ASYNC
);
1233 fcntl(fd
, F_SETOWN
, getpid());
1236 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1238 struct hpet_info info
;
1241 fd
= open("/dev/hpet", O_RDONLY
);
1246 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1248 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1249 "error, but for better emulation accuracy type:\n"
1250 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1254 /* Check capabilities */
1255 r
= ioctl(fd
, HPET_INFO
, &info
);
1259 /* Enable periodic mode */
1260 r
= ioctl(fd
, HPET_EPI
, 0);
1261 if (info
.hi_flags
&& (r
< 0))
1264 /* Enable interrupt */
1265 r
= ioctl(fd
, HPET_IE_ON
, 0);
1269 enable_sigio_timer(fd
);
1270 t
->priv
= (void *)(long)fd
;
1278 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1280 int fd
= (long)t
->priv
;
1285 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1288 unsigned long current_rtc_freq
= 0;
1290 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1293 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1294 if (current_rtc_freq
!= RTC_FREQ
&&
1295 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1296 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1297 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1298 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1301 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1307 enable_sigio_timer(rtc_fd
);
1309 t
->priv
= (void *)(long)rtc_fd
;
1314 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1316 int rtc_fd
= (long)t
->priv
;
1321 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1325 struct sigaction act
;
1327 sigfillset(&act
.sa_mask
);
1329 act
.sa_handler
= host_alarm_handler
;
1331 sigaction(SIGALRM
, &act
, NULL
);
1334 * Initialize ev struct to 0 to avoid valgrind complaining
1335 * about uninitialized data in timer_create call
1337 memset(&ev
, 0, sizeof(ev
));
1338 ev
.sigev_value
.sival_int
= 0;
1339 ev
.sigev_notify
= SIGEV_SIGNAL
;
1340 ev
.sigev_signo
= SIGALRM
;
1342 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1343 perror("timer_create");
1345 /* disable dynticks */
1346 fprintf(stderr
, "Dynamic Ticks disabled\n");
1351 t
->priv
= (void *)(long)host_timer
;
1356 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1358 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1360 timer_delete(host_timer
);
1363 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1365 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1366 struct itimerspec timeout
;
1367 int64_t nearest_delta_us
= INT64_MAX
;
1370 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1371 !active_timers
[QEMU_TIMER_VIRTUAL
])
1374 nearest_delta_us
= qemu_next_deadline_dyntick();
1376 /* check whether a timer is already running */
1377 if (timer_gettime(host_timer
, &timeout
)) {
1379 fprintf(stderr
, "Internal timer error: aborting\n");
1382 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1383 if (current_us
&& current_us
<= nearest_delta_us
)
1386 timeout
.it_interval
.tv_sec
= 0;
1387 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1388 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1389 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1390 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1392 fprintf(stderr
, "Internal timer error: aborting\n");
1397 #endif /* defined(__linux__) */
1399 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1401 struct sigaction act
;
1402 struct itimerval itv
;
1406 sigfillset(&act
.sa_mask
);
1408 act
.sa_handler
= host_alarm_handler
;
1410 sigaction(SIGALRM
, &act
, NULL
);
1412 itv
.it_interval
.tv_sec
= 0;
1413 /* for i386 kernel 2.6 to get 1 ms */
1414 itv
.it_interval
.tv_usec
= 999;
1415 itv
.it_value
.tv_sec
= 0;
1416 itv
.it_value
.tv_usec
= 10 * 1000;
1418 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1425 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1427 struct itimerval itv
;
1429 memset(&itv
, 0, sizeof(itv
));
1430 setitimer(ITIMER_REAL
, &itv
, NULL
);
1433 #endif /* !defined(_WIN32) */
1438 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1441 struct qemu_alarm_win32
*data
= t
->priv
;
1444 memset(&tc
, 0, sizeof(tc
));
1445 timeGetDevCaps(&tc
, sizeof(tc
));
1447 if (data
->period
< tc
.wPeriodMin
)
1448 data
->period
= tc
.wPeriodMin
;
1450 timeBeginPeriod(data
->period
);
1452 flags
= TIME_CALLBACK_FUNCTION
;
1453 if (alarm_has_dynticks(t
))
1454 flags
|= TIME_ONESHOT
;
1456 flags
|= TIME_PERIODIC
;
1458 data
->timerId
= timeSetEvent(1, // interval (ms)
1459 data
->period
, // resolution
1460 host_alarm_handler
, // function
1461 (DWORD
)t
, // parameter
1464 if (!data
->timerId
) {
1465 perror("Failed to initialize win32 alarm timer");
1466 timeEndPeriod(data
->period
);
1473 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1475 struct qemu_alarm_win32
*data
= t
->priv
;
1477 timeKillEvent(data
->timerId
);
1478 timeEndPeriod(data
->period
);
1481 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1483 struct qemu_alarm_win32
*data
= t
->priv
;
1484 uint64_t nearest_delta_us
;
1486 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1487 !active_timers
[QEMU_TIMER_VIRTUAL
])
1490 nearest_delta_us
= qemu_next_deadline_dyntick();
1491 nearest_delta_us
/= 1000;
1493 timeKillEvent(data
->timerId
);
1495 data
->timerId
= timeSetEvent(1,
1499 TIME_ONESHOT
| TIME_PERIODIC
);
1501 if (!data
->timerId
) {
1502 perror("Failed to re-arm win32 alarm timer");
1504 timeEndPeriod(data
->period
);
1511 static int init_timer_alarm(void)
1513 struct qemu_alarm_timer
*t
= NULL
;
1516 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1517 t
= &alarm_timers
[i
];
1537 static void quit_timers(void)
1539 alarm_timer
->stop(alarm_timer
);
1543 /***********************************************************/
1544 /* host time/date access */
1545 void qemu_get_timedate(struct tm
*tm
, int offset
)
1552 if (rtc_date_offset
== -1) {
1556 ret
= localtime(&ti
);
1558 ti
-= rtc_date_offset
;
1562 memcpy(tm
, ret
, sizeof(struct tm
));
1565 int qemu_timedate_diff(struct tm
*tm
)
1569 if (rtc_date_offset
== -1)
1571 seconds
= mktimegm(tm
);
1573 seconds
= mktime(tm
);
1575 seconds
= mktimegm(tm
) + rtc_date_offset
;
1577 return seconds
- time(NULL
);
1581 static void socket_cleanup(void)
1586 static int socket_init(void)
1591 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1593 err
= WSAGetLastError();
1594 fprintf(stderr
, "WSAStartup: %d\n", err
);
1597 atexit(socket_cleanup
);
1602 int get_next_param_value(char *buf
, int buf_size
,
1603 const char *tag
, const char **pstr
)
1610 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1614 if (!strcmp(tag
, option
)) {
1615 *pstr
= get_opt_value(buf
, buf_size
, p
);
1616 if (**pstr
== ',') {
1621 p
= get_opt_value(NULL
, 0, p
);
1630 int get_param_value(char *buf
, int buf_size
,
1631 const char *tag
, const char *str
)
1633 return get_next_param_value(buf
, buf_size
, tag
, &str
);
1636 int check_params(char *buf
, int buf_size
,
1637 const char * const *params
, const char *str
)
1643 while (*p
!= '\0') {
1644 p
= get_opt_name(buf
, buf_size
, p
, '=');
1649 for (i
= 0; params
[i
] != NULL
; i
++) {
1650 if (!strcmp(params
[i
], buf
)) {
1654 if (params
[i
] == NULL
) {
1657 p
= get_opt_value(NULL
, 0, p
);
1666 /***********************************************************/
1667 /* Bluetooth support */
1670 static struct HCIInfo
*hci_table
[MAX_NICS
];
1672 static struct bt_vlan_s
{
1673 struct bt_scatternet_s net
;
1675 struct bt_vlan_s
*next
;
1678 /* find or alloc a new bluetooth "VLAN" */
1679 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1681 struct bt_vlan_s
**pvlan
, *vlan
;
1682 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1686 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1688 pvlan
= &first_bt_vlan
;
1689 while (*pvlan
!= NULL
)
1690 pvlan
= &(*pvlan
)->next
;
1695 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1699 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1704 static struct HCIInfo null_hci
= {
1705 .cmd_send
= null_hci_send
,
1706 .sco_send
= null_hci_send
,
1707 .acl_send
= null_hci_send
,
1708 .bdaddr_set
= null_hci_addr_set
,
1711 struct HCIInfo
*qemu_next_hci(void)
1713 if (cur_hci
== nb_hcis
)
1716 return hci_table
[cur_hci
++];
1719 static struct HCIInfo
*hci_init(const char *str
)
1722 struct bt_scatternet_s
*vlan
= 0;
1724 if (!strcmp(str
, "null"))
1727 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1729 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1730 else if (!strncmp(str
, "hci", 3)) {
1733 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1734 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1739 vlan
= qemu_find_bt_vlan(0);
1741 return bt_new_hci(vlan
);
1744 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1749 static int bt_hci_parse(const char *str
)
1751 struct HCIInfo
*hci
;
1754 if (nb_hcis
>= MAX_NICS
) {
1755 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1759 hci
= hci_init(str
);
1768 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1769 hci
->bdaddr_set(hci
, bdaddr
.b
);
1771 hci_table
[nb_hcis
++] = hci
;
1776 static void bt_vhci_add(int vlan_id
)
1778 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1781 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1782 "an empty scatternet %i\n", vlan_id
);
1784 bt_vhci_init(bt_new_hci(vlan
));
1787 static struct bt_device_s
*bt_device_add(const char *opt
)
1789 struct bt_scatternet_s
*vlan
;
1791 char *endp
= strstr(opt
, ",vlan=");
1792 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1795 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1798 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1800 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1805 vlan
= qemu_find_bt_vlan(vlan_id
);
1808 fprintf(stderr
, "qemu: warning: adding a slave device to "
1809 "an empty scatternet %i\n", vlan_id
);
1811 if (!strcmp(devname
, "keyboard"))
1812 return bt_keyboard_init(vlan
);
1814 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1818 static int bt_parse(const char *opt
)
1820 const char *endp
, *p
;
1823 if (strstart(opt
, "hci", &endp
)) {
1824 if (!*endp
|| *endp
== ',') {
1826 if (!strstart(endp
, ",vlan=", 0))
1829 return bt_hci_parse(opt
);
1831 } else if (strstart(opt
, "vhci", &endp
)) {
1832 if (!*endp
|| *endp
== ',') {
1834 if (strstart(endp
, ",vlan=", &p
)) {
1835 vlan
= strtol(p
, (char **) &endp
, 0);
1837 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1841 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1850 } else if (strstart(opt
, "device:", &endp
))
1851 return !bt_device_add(endp
);
1853 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1857 /***********************************************************/
1858 /* QEMU Block devices */
1860 #define HD_ALIAS "index=%d,media=disk"
1861 #define CDROM_ALIAS "index=2,media=cdrom"
1862 #define FD_ALIAS "index=%d,if=floppy"
1863 #define PFLASH_ALIAS "if=pflash"
1864 #define MTD_ALIAS "if=mtd"
1865 #define SD_ALIAS "index=0,if=sd"
1867 DriveOpt
*drive_add(const char *file
, const char *fmt
, ...)
1872 dopt
= qemu_mallocz(sizeof(*dopt
));
1876 vsnprintf(dopt
->opt
,
1877 sizeof(dopt
->opt
), fmt
, ap
);
1880 TAILQ_INSERT_TAIL(&driveopts
, dopt
, next
);
1884 void drive_remove(DriveOpt
*dopt
)
1886 TAILQ_REMOVE(&driveopts
, dopt
, next
);
1890 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1894 /* seek interface, bus and unit */
1896 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1897 if (dinfo
->type
== type
&&
1898 dinfo
->bus
== bus
&&
1899 dinfo
->unit
== unit
)
1906 DriveInfo
*drive_get_by_id(char *id
)
1910 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1911 if (strcmp(id
, dinfo
->id
))
1918 int drive_get_max_bus(BlockInterfaceType type
)
1924 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1925 if(dinfo
->type
== type
&&
1926 dinfo
->bus
> max_bus
)
1927 max_bus
= dinfo
->bus
;
1932 const char *drive_get_serial(BlockDriverState
*bdrv
)
1936 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1937 if (dinfo
->bdrv
== bdrv
)
1938 return dinfo
->serial
;
1944 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1948 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1949 if (dinfo
->bdrv
== bdrv
)
1950 return dinfo
->onerror
;
1953 return BLOCK_ERR_STOP_ENOSPC
;
1956 static void bdrv_format_print(void *opaque
, const char *name
)
1958 fprintf(stderr
, " %s", name
);
1961 void drive_uninit(BlockDriverState
*bdrv
)
1965 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1966 if (dinfo
->bdrv
!= bdrv
)
1968 drive_remove(dinfo
->opt
);
1969 TAILQ_REMOVE(&drives
, dinfo
, next
);
1975 DriveInfo
*drive_init(DriveOpt
*arg
, int snapshot
, void *opaque
,
1982 const char *mediastr
= "";
1983 BlockInterfaceType type
;
1984 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1985 int bus_id
, unit_id
;
1986 int cyls
, heads
, secs
, translation
;
1987 BlockDriver
*drv
= NULL
;
1988 QEMUMachine
*machine
= opaque
;
1992 int bdrv_flags
, onerror
;
1993 const char *devaddr
;
1995 char *str
= arg
->opt
;
1996 static const char * const params
[] = { "bus", "unit", "if", "index",
1997 "cyls", "heads", "secs", "trans",
1998 "media", "snapshot", "file",
1999 "cache", "format", "serial",
2000 "werror", "addr", "id",
2004 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2005 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2011 cyls
= heads
= secs
= 0;
2014 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2018 if (machine
->use_scsi
) {
2020 max_devs
= MAX_SCSI_DEVS
;
2021 pstrcpy(devname
, sizeof(devname
), "scsi");
2024 max_devs
= MAX_IDE_DEVS
;
2025 pstrcpy(devname
, sizeof(devname
), "ide");
2029 /* extract parameters */
2031 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2032 bus_id
= strtol(buf
, NULL
, 0);
2034 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2039 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2040 unit_id
= strtol(buf
, NULL
, 0);
2042 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2047 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2048 pstrcpy(devname
, sizeof(devname
), buf
);
2049 if (!strcmp(buf
, "ide")) {
2051 max_devs
= MAX_IDE_DEVS
;
2052 } else if (!strcmp(buf
, "scsi")) {
2054 max_devs
= MAX_SCSI_DEVS
;
2055 } else if (!strcmp(buf
, "floppy")) {
2058 } else if (!strcmp(buf
, "pflash")) {
2061 } else if (!strcmp(buf
, "mtd")) {
2064 } else if (!strcmp(buf
, "sd")) {
2067 } else if (!strcmp(buf
, "virtio")) {
2070 } else if (!strcmp(buf
, "xen")) {
2074 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2079 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2080 index
= strtol(buf
, NULL
, 0);
2082 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2087 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2088 cyls
= strtol(buf
, NULL
, 0);
2091 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2092 heads
= strtol(buf
, NULL
, 0);
2095 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2096 secs
= strtol(buf
, NULL
, 0);
2099 if (cyls
|| heads
|| secs
) {
2100 if (cyls
< 1 || cyls
> 16383) {
2101 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2104 if (heads
< 1 || heads
> 16) {
2105 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2108 if (secs
< 1 || secs
> 63) {
2109 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2114 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2117 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2121 if (!strcmp(buf
, "none"))
2122 translation
= BIOS_ATA_TRANSLATION_NONE
;
2123 else if (!strcmp(buf
, "lba"))
2124 translation
= BIOS_ATA_TRANSLATION_LBA
;
2125 else if (!strcmp(buf
, "auto"))
2126 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2128 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2133 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2134 if (!strcmp(buf
, "disk")) {
2136 } else if (!strcmp(buf
, "cdrom")) {
2137 if (cyls
|| secs
|| heads
) {
2139 "qemu: '%s' invalid physical CHS format\n", str
);
2142 media
= MEDIA_CDROM
;
2144 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2149 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2150 if (!strcmp(buf
, "on"))
2152 else if (!strcmp(buf
, "off"))
2155 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2160 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2161 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2163 else if (!strcmp(buf
, "writethrough"))
2165 else if (!strcmp(buf
, "writeback"))
2168 fprintf(stderr
, "qemu: invalid cache option\n");
2173 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2174 if (strcmp(buf
, "?") == 0) {
2175 fprintf(stderr
, "qemu: Supported formats:");
2176 bdrv_iterate_format(bdrv_format_print
, NULL
);
2177 fprintf(stderr
, "\n");
2180 drv
= bdrv_find_format(buf
);
2182 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2187 if (arg
->file
== NULL
)
2188 get_param_value(file
, sizeof(file
), "file", str
);
2190 pstrcpy(file
, sizeof(file
), arg
->file
);
2192 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2193 memset(serial
, 0, sizeof(serial
));
2195 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2196 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2197 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2198 fprintf(stderr
, "werror is no supported by this format\n");
2201 if (!strcmp(buf
, "ignore"))
2202 onerror
= BLOCK_ERR_IGNORE
;
2203 else if (!strcmp(buf
, "enospc"))
2204 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2205 else if (!strcmp(buf
, "stop"))
2206 onerror
= BLOCK_ERR_STOP_ANY
;
2207 else if (!strcmp(buf
, "report"))
2208 onerror
= BLOCK_ERR_REPORT
;
2210 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2216 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2217 if (type
!= IF_VIRTIO
) {
2218 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2221 devaddr
= strdup(buf
);
2224 /* compute bus and unit according index */
2227 if (bus_id
!= 0 || unit_id
!= -1) {
2229 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2237 unit_id
= index
% max_devs
;
2238 bus_id
= index
/ max_devs
;
2242 /* if user doesn't specify a unit_id,
2243 * try to find the first free
2246 if (unit_id
== -1) {
2248 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2250 if (max_devs
&& unit_id
>= max_devs
) {
2251 unit_id
-= max_devs
;
2259 if (max_devs
&& unit_id
>= max_devs
) {
2260 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2261 str
, unit_id
, max_devs
- 1);
2266 * ignore multiple definitions
2269 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2276 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2277 if (!get_param_value(buf
, sizeof(buf
), "id", str
)) {
2278 /* no id supplied -> create one */
2279 if (type
== IF_IDE
|| type
== IF_SCSI
)
2280 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2282 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2283 devname
, bus_id
, mediastr
, unit_id
);
2285 snprintf(buf
, sizeof(buf
), "%s%s%i",
2286 devname
, mediastr
, unit_id
);
2288 dinfo
->id
= qemu_strdup(buf
);
2289 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2290 dinfo
->devaddr
= devaddr
;
2292 dinfo
->bus
= bus_id
;
2293 dinfo
->unit
= unit_id
;
2294 dinfo
->onerror
= onerror
;
2296 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2297 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2306 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2307 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2311 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2316 /* FIXME: This isn't really a floppy, but it's a reasonable
2319 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2334 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2335 cache
= 2; /* always use write-back with snapshot */
2337 if (cache
== 0) /* no caching */
2338 bdrv_flags
|= BDRV_O_NOCACHE
;
2339 else if (cache
== 2) /* write-back */
2340 bdrv_flags
|= BDRV_O_CACHE_WB
;
2341 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2342 fprintf(stderr
, "qemu: could not open disk image %s\n",
2346 if (bdrv_key_required(dinfo
->bdrv
))
2352 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2354 boot_set_handler
= func
;
2355 boot_set_opaque
= opaque
;
2358 int qemu_boot_set(const char *boot_devices
)
2360 if (!boot_set_handler
) {
2363 return boot_set_handler(boot_set_opaque
, boot_devices
);
2366 static int parse_bootdevices(char *devices
)
2368 /* We just do some generic consistency checks */
2372 for (p
= devices
; *p
!= '\0'; p
++) {
2373 /* Allowed boot devices are:
2374 * a-b: floppy disk drives
2375 * c-f: IDE disk drives
2376 * g-m: machine implementation dependant drives
2377 * n-p: network devices
2378 * It's up to each machine implementation to check if the given boot
2379 * devices match the actual hardware implementation and firmware
2382 if (*p
< 'a' || *p
> 'p') {
2383 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2386 if (bitmap
& (1 << (*p
- 'a'))) {
2387 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2390 bitmap
|= 1 << (*p
- 'a');
2395 static void restore_boot_devices(void *opaque
)
2397 char *standard_boot_devices
= opaque
;
2399 qemu_boot_set(standard_boot_devices
);
2401 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2402 qemu_free(standard_boot_devices
);
2405 static void numa_add(const char *optarg
)
2409 unsigned long long value
, endvalue
;
2412 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2413 if (!strcmp(option
, "node")) {
2414 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2415 nodenr
= nb_numa_nodes
;
2417 nodenr
= strtoull(option
, NULL
, 10);
2420 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2421 node_mem
[nodenr
] = 0;
2423 value
= strtoull(option
, &endptr
, 0);
2425 case 0: case 'M': case 'm':
2432 node_mem
[nodenr
] = value
;
2434 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2435 node_cpumask
[nodenr
] = 0;
2437 value
= strtoull(option
, &endptr
, 10);
2440 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2442 if (*endptr
== '-') {
2443 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2444 if (endvalue
>= 63) {
2447 "only 63 CPUs in NUMA mode supported.\n");
2449 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2454 node_cpumask
[nodenr
] = value
;
2461 /***********************************************************/
2464 static USBPort
*used_usb_ports
;
2465 static USBPort
*free_usb_ports
;
2467 /* ??? Maybe change this to register a hub to keep track of the topology. */
2468 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2469 usb_attachfn attach
)
2471 port
->opaque
= opaque
;
2472 port
->index
= index
;
2473 port
->attach
= attach
;
2474 port
->next
= free_usb_ports
;
2475 free_usb_ports
= port
;
2478 int usb_device_add_dev(USBDevice
*dev
)
2482 /* Find a USB port to add the device to. */
2483 port
= free_usb_ports
;
2487 /* Create a new hub and chain it on. */
2488 free_usb_ports
= NULL
;
2489 port
->next
= used_usb_ports
;
2490 used_usb_ports
= port
;
2492 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2493 usb_attach(port
, hub
);
2494 port
= free_usb_ports
;
2497 free_usb_ports
= port
->next
;
2498 port
->next
= used_usb_ports
;
2499 used_usb_ports
= port
;
2500 usb_attach(port
, dev
);
2504 static void usb_msd_password_cb(void *opaque
, int err
)
2506 USBDevice
*dev
= opaque
;
2509 usb_device_add_dev(dev
);
2511 dev
->handle_destroy(dev
);
2514 static int usb_device_add(const char *devname
, int is_hotplug
)
2519 if (!free_usb_ports
)
2522 if (strstart(devname
, "host:", &p
)) {
2523 dev
= usb_host_device_open(p
);
2524 } else if (!strcmp(devname
, "mouse")) {
2525 dev
= usb_mouse_init();
2526 } else if (!strcmp(devname
, "tablet")) {
2527 dev
= usb_tablet_init();
2528 } else if (!strcmp(devname
, "keyboard")) {
2529 dev
= usb_keyboard_init();
2530 } else if (strstart(devname
, "disk:", &p
)) {
2531 BlockDriverState
*bs
;
2533 dev
= usb_msd_init(p
);
2536 bs
= usb_msd_get_bdrv(dev
);
2537 if (bdrv_key_required(bs
)) {
2540 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2545 } else if (!strcmp(devname
, "wacom-tablet")) {
2546 dev
= usb_wacom_init();
2547 } else if (strstart(devname
, "serial:", &p
)) {
2548 dev
= usb_serial_init(p
);
2549 #ifdef CONFIG_BRLAPI
2550 } else if (!strcmp(devname
, "braille")) {
2551 dev
= usb_baum_init();
2553 } else if (strstart(devname
, "net:", &p
)) {
2556 if (net_client_init(NULL
, "nic", p
) < 0)
2558 nd_table
[nic
].model
= "usb";
2559 dev
= usb_net_init(&nd_table
[nic
]);
2560 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2561 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2562 bt_new_hci(qemu_find_bt_vlan(0)));
2569 return usb_device_add_dev(dev
);
2572 int usb_device_del_addr(int bus_num
, int addr
)
2578 if (!used_usb_ports
)
2584 lastp
= &used_usb_ports
;
2585 port
= used_usb_ports
;
2586 while (port
&& port
->dev
->addr
!= addr
) {
2587 lastp
= &port
->next
;
2595 *lastp
= port
->next
;
2596 usb_attach(port
, NULL
);
2597 dev
->handle_destroy(dev
);
2598 port
->next
= free_usb_ports
;
2599 free_usb_ports
= port
;
2603 static int usb_device_del(const char *devname
)
2608 if (strstart(devname
, "host:", &p
))
2609 return usb_host_device_close(p
);
2611 if (!used_usb_ports
)
2614 p
= strchr(devname
, '.');
2617 bus_num
= strtoul(devname
, NULL
, 0);
2618 addr
= strtoul(p
+ 1, NULL
, 0);
2620 return usb_device_del_addr(bus_num
, addr
);
2623 static int usb_parse(const char *cmdline
)
2625 return usb_device_add(cmdline
, 0);
2628 void do_usb_add(Monitor
*mon
, const char *devname
)
2630 usb_device_add(devname
, 1);
2633 void do_usb_del(Monitor
*mon
, const char *devname
)
2635 usb_device_del(devname
);
2638 void usb_info(Monitor
*mon
)
2642 const char *speed_str
;
2645 monitor_printf(mon
, "USB support not enabled\n");
2649 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2653 switch(dev
->speed
) {
2657 case USB_SPEED_FULL
:
2660 case USB_SPEED_HIGH
:
2667 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2668 0, dev
->addr
, speed_str
, dev
->devname
);
2672 /***********************************************************/
2673 /* PCMCIA/Cardbus */
2675 static struct pcmcia_socket_entry_s
{
2676 PCMCIASocket
*socket
;
2677 struct pcmcia_socket_entry_s
*next
;
2678 } *pcmcia_sockets
= 0;
2680 void pcmcia_socket_register(PCMCIASocket
*socket
)
2682 struct pcmcia_socket_entry_s
*entry
;
2684 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2685 entry
->socket
= socket
;
2686 entry
->next
= pcmcia_sockets
;
2687 pcmcia_sockets
= entry
;
2690 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2692 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2694 ptr
= &pcmcia_sockets
;
2695 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2696 if (entry
->socket
== socket
) {
2702 void pcmcia_info(Monitor
*mon
)
2704 struct pcmcia_socket_entry_s
*iter
;
2706 if (!pcmcia_sockets
)
2707 monitor_printf(mon
, "No PCMCIA sockets\n");
2709 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2710 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2711 iter
->socket
->attached
? iter
->socket
->card_string
:
2715 /***********************************************************/
2716 /* register display */
2718 struct DisplayAllocator default_allocator
= {
2719 defaultallocator_create_displaysurface
,
2720 defaultallocator_resize_displaysurface
,
2721 defaultallocator_free_displaysurface
2724 void register_displaystate(DisplayState
*ds
)
2734 DisplayState
*get_displaystate(void)
2736 return display_state
;
2739 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2741 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2742 return ds
->allocator
;
2747 static void dumb_display_init(void)
2749 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2750 ds
->allocator
= &default_allocator
;
2751 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2752 register_displaystate(ds
);
2755 /***********************************************************/
2758 typedef struct IOHandlerRecord
{
2760 IOCanRWHandler
*fd_read_poll
;
2762 IOHandler
*fd_write
;
2765 /* temporary data */
2767 struct IOHandlerRecord
*next
;
2770 static IOHandlerRecord
*first_io_handler
;
2772 /* XXX: fd_read_poll should be suppressed, but an API change is
2773 necessary in the character devices to suppress fd_can_read(). */
2774 int qemu_set_fd_handler2(int fd
,
2775 IOCanRWHandler
*fd_read_poll
,
2777 IOHandler
*fd_write
,
2780 IOHandlerRecord
**pioh
, *ioh
;
2782 if (!fd_read
&& !fd_write
) {
2783 pioh
= &first_io_handler
;
2788 if (ioh
->fd
== fd
) {
2795 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2799 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2800 ioh
->next
= first_io_handler
;
2801 first_io_handler
= ioh
;
2804 ioh
->fd_read_poll
= fd_read_poll
;
2805 ioh
->fd_read
= fd_read
;
2806 ioh
->fd_write
= fd_write
;
2807 ioh
->opaque
= opaque
;
2813 int qemu_set_fd_handler(int fd
,
2815 IOHandler
*fd_write
,
2818 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2822 /***********************************************************/
2823 /* Polling handling */
2825 typedef struct PollingEntry
{
2828 struct PollingEntry
*next
;
2831 static PollingEntry
*first_polling_entry
;
2833 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2835 PollingEntry
**ppe
, *pe
;
2836 pe
= qemu_mallocz(sizeof(PollingEntry
));
2838 pe
->opaque
= opaque
;
2839 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2844 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2846 PollingEntry
**ppe
, *pe
;
2847 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2849 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2857 /***********************************************************/
2858 /* Wait objects support */
2859 typedef struct WaitObjects
{
2861 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2862 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2863 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2866 static WaitObjects wait_objects
= {0};
2868 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2870 WaitObjects
*w
= &wait_objects
;
2872 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2874 w
->events
[w
->num
] = handle
;
2875 w
->func
[w
->num
] = func
;
2876 w
->opaque
[w
->num
] = opaque
;
2881 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2884 WaitObjects
*w
= &wait_objects
;
2887 for (i
= 0; i
< w
->num
; i
++) {
2888 if (w
->events
[i
] == handle
)
2891 w
->events
[i
] = w
->events
[i
+ 1];
2892 w
->func
[i
] = w
->func
[i
+ 1];
2893 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2901 /***********************************************************/
2902 /* ram save/restore */
2904 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2908 v
= qemu_get_byte(f
);
2911 if (qemu_get_buffer(f
, buf
, len
) != len
)
2915 v
= qemu_get_byte(f
);
2916 memset(buf
, v
, len
);
2922 if (qemu_file_has_error(f
))
2928 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2933 if (qemu_get_be32(f
) != last_ram_offset
)
2935 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2936 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2943 #define BDRV_HASH_BLOCK_SIZE 1024
2944 #define IOBUF_SIZE 4096
2945 #define RAM_CBLOCK_MAGIC 0xfabe
2947 typedef struct RamDecompressState
{
2950 uint8_t buf
[IOBUF_SIZE
];
2951 } RamDecompressState
;
2953 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2956 memset(s
, 0, sizeof(*s
));
2958 ret
= inflateInit(&s
->zstream
);
2964 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2968 s
->zstream
.avail_out
= len
;
2969 s
->zstream
.next_out
= buf
;
2970 while (s
->zstream
.avail_out
> 0) {
2971 if (s
->zstream
.avail_in
== 0) {
2972 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2974 clen
= qemu_get_be16(s
->f
);
2975 if (clen
> IOBUF_SIZE
)
2977 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2978 s
->zstream
.avail_in
= clen
;
2979 s
->zstream
.next_in
= s
->buf
;
2981 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2982 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2989 static void ram_decompress_close(RamDecompressState
*s
)
2991 inflateEnd(&s
->zstream
);
2994 #define RAM_SAVE_FLAG_FULL 0x01
2995 #define RAM_SAVE_FLAG_COMPRESS 0x02
2996 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2997 #define RAM_SAVE_FLAG_PAGE 0x08
2998 #define RAM_SAVE_FLAG_EOS 0x10
3000 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3002 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3003 uint32_t *array
= (uint32_t *)page
;
3006 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3007 if (array
[i
] != val
)
3014 static int ram_save_block(QEMUFile
*f
)
3016 static ram_addr_t current_addr
= 0;
3017 ram_addr_t saved_addr
= current_addr
;
3018 ram_addr_t addr
= 0;
3021 while (addr
< last_ram_offset
) {
3022 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3025 cpu_physical_memory_reset_dirty(current_addr
,
3026 current_addr
+ TARGET_PAGE_SIZE
,
3027 MIGRATION_DIRTY_FLAG
);
3029 p
= qemu_get_ram_ptr(current_addr
);
3031 if (is_dup_page(p
, *p
)) {
3032 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3033 qemu_put_byte(f
, *p
);
3035 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3036 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3042 addr
+= TARGET_PAGE_SIZE
;
3043 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3049 static uint64_t bytes_transferred
= 0;
3051 static ram_addr_t
ram_save_remaining(void)
3054 ram_addr_t count
= 0;
3056 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3057 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3064 uint64_t ram_bytes_remaining(void)
3066 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3069 uint64_t ram_bytes_transferred(void)
3071 return bytes_transferred
;
3074 uint64_t ram_bytes_total(void)
3076 return last_ram_offset
;
3079 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3082 uint64_t bytes_transferred_last
;
3084 uint64_t expected_time
= 0;
3086 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3087 qemu_file_set_error(f
);
3092 /* Make sure all dirty bits are set */
3093 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3094 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3095 cpu_physical_memory_set_dirty(addr
);
3098 /* Enable dirty memory tracking */
3099 cpu_physical_memory_set_dirty_tracking(1);
3101 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3104 bytes_transferred_last
= bytes_transferred
;
3105 bwidth
= get_clock();
3107 while (!qemu_file_rate_limit(f
)) {
3110 ret
= ram_save_block(f
);
3111 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3112 if (ret
== 0) /* no more blocks */
3116 bwidth
= get_clock() - bwidth
;
3117 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3119 /* if we haven't transferred anything this round, force expected_time to a
3120 * a very high value, but without crashing */
3124 /* try transferring iterative blocks of memory */
3128 /* flush all remaining blocks regardless of rate limiting */
3129 while (ram_save_block(f
) != 0) {
3130 bytes_transferred
+= TARGET_PAGE_SIZE
;
3132 cpu_physical_memory_set_dirty_tracking(0);
3135 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3137 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3139 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3142 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3144 RamDecompressState s1
, *s
= &s1
;
3148 if (ram_decompress_open(s
, f
) < 0)
3150 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3151 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3152 fprintf(stderr
, "Error while reading ram block header\n");
3156 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3157 BDRV_HASH_BLOCK_SIZE
) < 0) {
3158 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3163 printf("Error block header\n");
3167 ram_decompress_close(s
);
3172 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3177 if (version_id
== 1)
3178 return ram_load_v1(f
, opaque
);
3180 if (version_id
== 2) {
3181 if (qemu_get_be32(f
) != last_ram_offset
)
3183 return ram_load_dead(f
, opaque
);
3186 if (version_id
!= 3)
3190 addr
= qemu_get_be64(f
);
3192 flags
= addr
& ~TARGET_PAGE_MASK
;
3193 addr
&= TARGET_PAGE_MASK
;
3195 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3196 if (addr
!= last_ram_offset
)
3200 if (flags
& RAM_SAVE_FLAG_FULL
) {
3201 if (ram_load_dead(f
, opaque
) < 0)
3205 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3206 uint8_t ch
= qemu_get_byte(f
);
3207 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3210 (!kvm_enabled() || kvm_has_sync_mmu())) {
3211 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3214 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3215 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3216 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3221 void qemu_service_io(void)
3223 qemu_notify_event();
3226 /***********************************************************/
3227 /* bottom halves (can be seen as timers which expire ASAP) */
3238 static QEMUBH
*first_bh
= NULL
;
3240 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3243 bh
= qemu_mallocz(sizeof(QEMUBH
));
3245 bh
->opaque
= opaque
;
3246 bh
->next
= first_bh
;
3251 int qemu_bh_poll(void)
3257 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3258 if (!bh
->deleted
&& bh
->scheduled
) {
3267 /* remove deleted bhs */
3281 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3289 void qemu_bh_schedule(QEMUBH
*bh
)
3295 /* stop the currently executing CPU to execute the BH ASAP */
3296 qemu_notify_event();
3299 void qemu_bh_cancel(QEMUBH
*bh
)
3304 void qemu_bh_delete(QEMUBH
*bh
)
3310 static void qemu_bh_update_timeout(int *timeout
)
3314 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3315 if (!bh
->deleted
&& bh
->scheduled
) {
3317 /* idle bottom halves will be polled at least
3319 *timeout
= MIN(10, *timeout
);
3321 /* non-idle bottom halves will be executed
3330 /***********************************************************/
3331 /* machine registration */
3333 static QEMUMachine
*first_machine
= NULL
;
3334 QEMUMachine
*current_machine
= NULL
;
3336 int qemu_register_machine(QEMUMachine
*m
)
3339 pm
= &first_machine
;
3347 static QEMUMachine
*find_machine(const char *name
)
3351 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3352 if (!strcmp(m
->name
, name
))
3354 if (m
->alias
&& !strcmp(m
->alias
, name
))
3360 static QEMUMachine
*find_default_machine(void)
3364 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3365 if (m
->is_default
) {
3372 /***********************************************************/
3373 /* main execution loop */
3375 static void gui_update(void *opaque
)
3377 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3378 DisplayState
*ds
= opaque
;
3379 DisplayChangeListener
*dcl
= ds
->listeners
;
3383 while (dcl
!= NULL
) {
3384 if (dcl
->gui_timer_interval
&&
3385 dcl
->gui_timer_interval
< interval
)
3386 interval
= dcl
->gui_timer_interval
;
3389 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3392 static void nographic_update(void *opaque
)
3394 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3396 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3399 struct vm_change_state_entry
{
3400 VMChangeStateHandler
*cb
;
3402 LIST_ENTRY (vm_change_state_entry
) entries
;
3405 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3407 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3410 VMChangeStateEntry
*e
;
3412 e
= qemu_mallocz(sizeof (*e
));
3416 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3420 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3422 LIST_REMOVE (e
, entries
);
3426 static void vm_state_notify(int running
, int reason
)
3428 VMChangeStateEntry
*e
;
3430 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3431 e
->cb(e
->opaque
, running
, reason
);
3435 static void resume_all_vcpus(void);
3436 static void pause_all_vcpus(void);
3443 vm_state_notify(1, 0);
3444 qemu_rearm_alarm_timer(alarm_timer
);
3449 /* reset/shutdown handler */
3451 typedef struct QEMUResetEntry
{
3452 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3453 QEMUResetHandler
*func
;
3457 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3458 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3459 static int reset_requested
;
3460 static int shutdown_requested
;
3461 static int powerdown_requested
;
3462 static int debug_requested
;
3463 static int vmstop_requested
;
3465 int qemu_shutdown_requested(void)
3467 int r
= shutdown_requested
;
3468 shutdown_requested
= 0;
3472 int qemu_reset_requested(void)
3474 int r
= reset_requested
;
3475 reset_requested
= 0;
3479 int qemu_powerdown_requested(void)
3481 int r
= powerdown_requested
;
3482 powerdown_requested
= 0;
3486 static int qemu_debug_requested(void)
3488 int r
= debug_requested
;
3489 debug_requested
= 0;
3493 static int qemu_vmstop_requested(void)
3495 int r
= vmstop_requested
;
3496 vmstop_requested
= 0;
3500 static void do_vm_stop(int reason
)
3503 cpu_disable_ticks();
3506 vm_state_notify(0, reason
);
3510 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3512 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3515 re
->opaque
= opaque
;
3516 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3519 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3523 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3524 if (re
->func
== func
&& re
->opaque
== opaque
) {
3525 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3532 void qemu_system_reset(void)
3534 QEMUResetEntry
*re
, *nre
;
3536 /* reset all devices */
3537 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3538 re
->func(re
->opaque
);
3542 void qemu_system_reset_request(void)
3545 shutdown_requested
= 1;
3547 reset_requested
= 1;
3549 qemu_notify_event();
3552 void qemu_system_shutdown_request(void)
3554 shutdown_requested
= 1;
3555 qemu_notify_event();
3558 void qemu_system_powerdown_request(void)
3560 powerdown_requested
= 1;
3561 qemu_notify_event();
3564 #ifdef CONFIG_IOTHREAD
3565 static void qemu_system_vmstop_request(int reason
)
3567 vmstop_requested
= reason
;
3568 qemu_notify_event();
3573 static int io_thread_fd
= -1;
3575 static void qemu_event_increment(void)
3577 static const char byte
= 0;
3579 if (io_thread_fd
== -1)
3582 write(io_thread_fd
, &byte
, sizeof(byte
));
3585 static void qemu_event_read(void *opaque
)
3587 int fd
= (unsigned long)opaque
;
3590 /* Drain the notify pipe */
3593 len
= read(fd
, buffer
, sizeof(buffer
));
3594 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3597 static int qemu_event_init(void)
3606 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3610 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3614 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3615 (void *)(unsigned long)fds
[0]);
3617 io_thread_fd
= fds
[1];
3626 HANDLE qemu_event_handle
;
3628 static void dummy_event_handler(void *opaque
)
3632 static int qemu_event_init(void)
3634 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3635 if (!qemu_event_handle
) {
3636 perror("Failed CreateEvent");
3639 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3643 static void qemu_event_increment(void)
3645 SetEvent(qemu_event_handle
);
3649 static int cpu_can_run(CPUState
*env
)
3658 #ifndef CONFIG_IOTHREAD
3659 static int qemu_init_main_loop(void)
3661 return qemu_event_init();
3664 void qemu_init_vcpu(void *_env
)
3666 CPUState
*env
= _env
;
3673 int qemu_cpu_self(void *env
)
3678 static void resume_all_vcpus(void)
3682 static void pause_all_vcpus(void)
3686 void qemu_cpu_kick(void *env
)
3691 void qemu_notify_event(void)
3693 CPUState
*env
= cpu_single_env
;
3698 if (env
->kqemu_enabled
)
3699 kqemu_cpu_interrupt(env
);
3704 #define qemu_mutex_lock_iothread() do { } while (0)
3705 #define qemu_mutex_unlock_iothread() do { } while (0)
3707 void vm_stop(int reason
)
3712 #else /* CONFIG_IOTHREAD */
3714 #include "qemu-thread.h"
3716 QemuMutex qemu_global_mutex
;
3717 static QemuMutex qemu_fair_mutex
;
3719 static QemuThread io_thread
;
3721 static QemuThread
*tcg_cpu_thread
;
3722 static QemuCond
*tcg_halt_cond
;
3724 static int qemu_system_ready
;
3726 static QemuCond qemu_cpu_cond
;
3728 static QemuCond qemu_system_cond
;
3729 static QemuCond qemu_pause_cond
;
3731 static void block_io_signals(void);
3732 static void unblock_io_signals(void);
3733 static int tcg_has_work(void);
3735 static int qemu_init_main_loop(void)
3739 ret
= qemu_event_init();
3743 qemu_cond_init(&qemu_pause_cond
);
3744 qemu_mutex_init(&qemu_fair_mutex
);
3745 qemu_mutex_init(&qemu_global_mutex
);
3746 qemu_mutex_lock(&qemu_global_mutex
);
3748 unblock_io_signals();
3749 qemu_thread_self(&io_thread
);
3754 static void qemu_wait_io_event(CPUState
*env
)
3756 while (!tcg_has_work())
3757 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3759 qemu_mutex_unlock(&qemu_global_mutex
);
3762 * Users of qemu_global_mutex can be starved, having no chance
3763 * to acquire it since this path will get to it first.
3764 * So use another lock to provide fairness.
3766 qemu_mutex_lock(&qemu_fair_mutex
);
3767 qemu_mutex_unlock(&qemu_fair_mutex
);
3769 qemu_mutex_lock(&qemu_global_mutex
);
3773 qemu_cond_signal(&qemu_pause_cond
);
3777 static int qemu_cpu_exec(CPUState
*env
);
3779 static void *kvm_cpu_thread_fn(void *arg
)
3781 CPUState
*env
= arg
;
3784 qemu_thread_self(env
->thread
);
3786 /* signal CPU creation */
3787 qemu_mutex_lock(&qemu_global_mutex
);
3789 qemu_cond_signal(&qemu_cpu_cond
);
3791 /* and wait for machine initialization */
3792 while (!qemu_system_ready
)
3793 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3796 if (cpu_can_run(env
))
3798 qemu_wait_io_event(env
);
3804 static void tcg_cpu_exec(void);
3806 static void *tcg_cpu_thread_fn(void *arg
)
3808 CPUState
*env
= arg
;
3811 qemu_thread_self(env
->thread
);
3813 /* signal CPU creation */
3814 qemu_mutex_lock(&qemu_global_mutex
);
3815 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3817 qemu_cond_signal(&qemu_cpu_cond
);
3819 /* and wait for machine initialization */
3820 while (!qemu_system_ready
)
3821 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3825 qemu_wait_io_event(cur_cpu
);
3831 void qemu_cpu_kick(void *_env
)
3833 CPUState
*env
= _env
;
3834 qemu_cond_broadcast(env
->halt_cond
);
3836 qemu_thread_signal(env
->thread
, SIGUSR1
);
3839 int qemu_cpu_self(void *env
)
3841 return (cpu_single_env
!= NULL
);
3844 static void cpu_signal(int sig
)
3847 cpu_exit(cpu_single_env
);
3850 static void block_io_signals(void)
3853 struct sigaction sigact
;
3856 sigaddset(&set
, SIGUSR2
);
3857 sigaddset(&set
, SIGIO
);
3858 sigaddset(&set
, SIGALRM
);
3859 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3862 sigaddset(&set
, SIGUSR1
);
3863 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3865 memset(&sigact
, 0, sizeof(sigact
));
3866 sigact
.sa_handler
= cpu_signal
;
3867 sigaction(SIGUSR1
, &sigact
, NULL
);
3870 static void unblock_io_signals(void)
3875 sigaddset(&set
, SIGUSR2
);
3876 sigaddset(&set
, SIGIO
);
3877 sigaddset(&set
, SIGALRM
);
3878 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3881 sigaddset(&set
, SIGUSR1
);
3882 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3885 static void qemu_signal_lock(unsigned int msecs
)
3887 qemu_mutex_lock(&qemu_fair_mutex
);
3889 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3890 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3891 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3894 qemu_mutex_unlock(&qemu_fair_mutex
);
3897 static void qemu_mutex_lock_iothread(void)
3899 if (kvm_enabled()) {
3900 qemu_mutex_lock(&qemu_fair_mutex
);
3901 qemu_mutex_lock(&qemu_global_mutex
);
3902 qemu_mutex_unlock(&qemu_fair_mutex
);
3904 qemu_signal_lock(100);
3907 static void qemu_mutex_unlock_iothread(void)
3909 qemu_mutex_unlock(&qemu_global_mutex
);
3912 static int all_vcpus_paused(void)
3914 CPUState
*penv
= first_cpu
;
3919 penv
= (CPUState
*)penv
->next_cpu
;
3925 static void pause_all_vcpus(void)
3927 CPUState
*penv
= first_cpu
;
3931 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3932 qemu_cpu_kick(penv
);
3933 penv
= (CPUState
*)penv
->next_cpu
;
3936 while (!all_vcpus_paused()) {
3937 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3940 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3941 penv
= (CPUState
*)penv
->next_cpu
;
3946 static void resume_all_vcpus(void)
3948 CPUState
*penv
= first_cpu
;
3953 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3954 qemu_cpu_kick(penv
);
3955 penv
= (CPUState
*)penv
->next_cpu
;
3959 static void tcg_init_vcpu(void *_env
)
3961 CPUState
*env
= _env
;
3962 /* share a single thread for all cpus with TCG */
3963 if (!tcg_cpu_thread
) {
3964 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3965 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3966 qemu_cond_init(env
->halt_cond
);
3967 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3968 while (env
->created
== 0)
3969 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3970 tcg_cpu_thread
= env
->thread
;
3971 tcg_halt_cond
= env
->halt_cond
;
3973 env
->thread
= tcg_cpu_thread
;
3974 env
->halt_cond
= tcg_halt_cond
;
3978 static void kvm_start_vcpu(CPUState
*env
)
3981 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3982 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3983 qemu_cond_init(env
->halt_cond
);
3984 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3985 while (env
->created
== 0)
3986 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3989 void qemu_init_vcpu(void *_env
)
3991 CPUState
*env
= _env
;
3994 kvm_start_vcpu(env
);
3999 void qemu_notify_event(void)
4001 qemu_event_increment();
4004 void vm_stop(int reason
)
4007 qemu_thread_self(&me
);
4009 if (!qemu_thread_equal(&me
, &io_thread
)) {
4010 qemu_system_vmstop_request(reason
);
4012 * FIXME: should not return to device code in case
4013 * vm_stop() has been requested.
4015 if (cpu_single_env
) {
4016 cpu_exit(cpu_single_env
);
4017 cpu_single_env
->stop
= 1;
4028 static void host_main_loop_wait(int *timeout
)
4034 /* XXX: need to suppress polling by better using win32 events */
4036 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4037 ret
|= pe
->func(pe
->opaque
);
4041 WaitObjects
*w
= &wait_objects
;
4043 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4044 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4045 if (w
->func
[ret
- WAIT_OBJECT_0
])
4046 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4048 /* Check for additional signaled events */
4049 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4051 /* Check if event is signaled */
4052 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4053 if(ret2
== WAIT_OBJECT_0
) {
4055 w
->func
[i
](w
->opaque
[i
]);
4056 } else if (ret2
== WAIT_TIMEOUT
) {
4058 err
= GetLastError();
4059 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4062 } else if (ret
== WAIT_TIMEOUT
) {
4064 err
= GetLastError();
4065 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4072 static void host_main_loop_wait(int *timeout
)
4077 void main_loop_wait(int timeout
)
4079 IOHandlerRecord
*ioh
;
4080 fd_set rfds
, wfds
, xfds
;
4084 qemu_bh_update_timeout(&timeout
);
4086 host_main_loop_wait(&timeout
);
4088 /* poll any events */
4089 /* XXX: separate device handlers from system ones */
4094 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4098 (!ioh
->fd_read_poll
||
4099 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4100 FD_SET(ioh
->fd
, &rfds
);
4104 if (ioh
->fd_write
) {
4105 FD_SET(ioh
->fd
, &wfds
);
4111 tv
.tv_sec
= timeout
/ 1000;
4112 tv
.tv_usec
= (timeout
% 1000) * 1000;
4114 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4116 qemu_mutex_unlock_iothread();
4117 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4118 qemu_mutex_lock_iothread();
4120 IOHandlerRecord
**pioh
;
4122 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4123 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4124 ioh
->fd_read(ioh
->opaque
);
4126 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4127 ioh
->fd_write(ioh
->opaque
);
4131 /* remove deleted IO handlers */
4132 pioh
= &first_io_handler
;
4143 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4145 /* rearm timer, if not periodic */
4146 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4147 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4148 qemu_rearm_alarm_timer(alarm_timer
);
4151 /* vm time timers */
4153 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4154 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4155 qemu_get_clock(vm_clock
));
4158 /* real time timers */
4159 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4160 qemu_get_clock(rt_clock
));
4162 /* Check bottom-halves last in case any of the earlier events triggered
4168 static int qemu_cpu_exec(CPUState
*env
)
4171 #ifdef CONFIG_PROFILER
4175 #ifdef CONFIG_PROFILER
4176 ti
= profile_getclock();
4181 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4182 env
->icount_decr
.u16
.low
= 0;
4183 env
->icount_extra
= 0;
4184 count
= qemu_next_deadline();
4185 count
= (count
+ (1 << icount_time_shift
) - 1)
4186 >> icount_time_shift
;
4187 qemu_icount
+= count
;
4188 decr
= (count
> 0xffff) ? 0xffff : count
;
4190 env
->icount_decr
.u16
.low
= decr
;
4191 env
->icount_extra
= count
;
4193 ret
= cpu_exec(env
);
4194 #ifdef CONFIG_PROFILER
4195 qemu_time
+= profile_getclock() - ti
;
4198 /* Fold pending instructions back into the
4199 instruction counter, and clear the interrupt flag. */
4200 qemu_icount
-= (env
->icount_decr
.u16
.low
4201 + env
->icount_extra
);
4202 env
->icount_decr
.u32
= 0;
4203 env
->icount_extra
= 0;
4208 static void tcg_cpu_exec(void)
4212 if (next_cpu
== NULL
)
4213 next_cpu
= first_cpu
;
4214 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4215 CPUState
*env
= cur_cpu
= next_cpu
;
4219 if (timer_alarm_pending
) {
4220 timer_alarm_pending
= 0;
4223 if (cpu_can_run(env
))
4224 ret
= qemu_cpu_exec(env
);
4225 if (ret
== EXCP_DEBUG
) {
4226 gdb_set_stop_cpu(env
);
4227 debug_requested
= 1;
4233 static int cpu_has_work(CPUState
*env
)
4241 if (qemu_cpu_has_work(env
))
4246 static int tcg_has_work(void)
4250 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4251 if (cpu_has_work(env
))
4256 static int qemu_calculate_timeout(void)
4258 #ifndef CONFIG_IOTHREAD
4263 else if (tcg_has_work())
4265 else if (!use_icount
)
4268 /* XXX: use timeout computed from timers */
4271 /* Advance virtual time to the next event. */
4272 if (use_icount
== 1) {
4273 /* When not using an adaptive execution frequency
4274 we tend to get badly out of sync with real time,
4275 so just delay for a reasonable amount of time. */
4278 delta
= cpu_get_icount() - cpu_get_clock();
4281 /* If virtual time is ahead of real time then just
4283 timeout
= (delta
/ 1000000) + 1;
4285 /* Wait for either IO to occur or the next
4287 add
= qemu_next_deadline();
4288 /* We advance the timer before checking for IO.
4289 Limit the amount we advance so that early IO
4290 activity won't get the guest too far ahead. */
4294 add
= (add
+ (1 << icount_time_shift
) - 1)
4295 >> icount_time_shift
;
4297 timeout
= delta
/ 1000000;
4304 #else /* CONFIG_IOTHREAD */
4309 static int vm_can_run(void)
4311 if (powerdown_requested
)
4313 if (reset_requested
)
4315 if (shutdown_requested
)
4317 if (debug_requested
)
4322 static void main_loop(void)
4326 #ifdef CONFIG_IOTHREAD
4327 qemu_system_ready
= 1;
4328 qemu_cond_broadcast(&qemu_system_cond
);
4333 #ifdef CONFIG_PROFILER
4336 #ifndef CONFIG_IOTHREAD
4339 #ifdef CONFIG_PROFILER
4340 ti
= profile_getclock();
4342 main_loop_wait(qemu_calculate_timeout());
4343 #ifdef CONFIG_PROFILER
4344 dev_time
+= profile_getclock() - ti
;
4346 } while (vm_can_run());
4348 if (qemu_debug_requested())
4349 vm_stop(EXCP_DEBUG
);
4350 if (qemu_shutdown_requested()) {
4357 if (qemu_reset_requested()) {
4359 qemu_system_reset();
4362 if (qemu_powerdown_requested())
4363 qemu_system_powerdown();
4364 if ((r
= qemu_vmstop_requested()))
4370 static void version(void)
4372 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4375 static void help(int exitcode
)
4378 printf("usage: %s [options] [disk_image]\n"
4380 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4382 #define DEF(option, opt_arg, opt_enum, opt_help) \
4384 #define DEFHEADING(text) stringify(text) "\n"
4385 #include "qemu-options.h"
4390 "During emulation, the following keys are useful:\n"
4391 "ctrl-alt-f toggle full screen\n"
4392 "ctrl-alt-n switch to virtual console 'n'\n"
4393 "ctrl-alt toggle mouse and keyboard grab\n"
4395 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4400 DEFAULT_NETWORK_SCRIPT
,
4401 DEFAULT_NETWORK_DOWN_SCRIPT
,
4403 DEFAULT_GDBSTUB_PORT
,
4408 #define HAS_ARG 0x0001
4411 #define DEF(option, opt_arg, opt_enum, opt_help) \
4413 #define DEFHEADING(text)
4414 #include "qemu-options.h"
4420 typedef struct QEMUOption
{
4426 static const QEMUOption qemu_options
[] = {
4427 { "h", 0, QEMU_OPTION_h
},
4428 #define DEF(option, opt_arg, opt_enum, opt_help) \
4429 { option, opt_arg, opt_enum },
4430 #define DEFHEADING(text)
4431 #include "qemu-options.h"
4439 struct soundhw soundhw
[] = {
4440 #ifdef HAS_AUDIO_CHOICE
4441 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4447 { .init_isa
= pcspk_audio_init
}
4454 "Creative Sound Blaster 16",
4457 { .init_isa
= SB16_init
}
4461 #ifdef CONFIG_CS4231A
4467 { .init_isa
= cs4231a_init
}
4475 "Yamaha YMF262 (OPL3)",
4477 "Yamaha YM3812 (OPL2)",
4481 { .init_isa
= Adlib_init
}
4488 "Gravis Ultrasound GF1",
4491 { .init_isa
= GUS_init
}
4498 "Intel 82801AA AC97 Audio",
4501 { .init_pci
= ac97_init
}
4505 #ifdef CONFIG_ES1370
4508 "ENSONIQ AudioPCI ES1370",
4511 { .init_pci
= es1370_init
}
4515 #endif /* HAS_AUDIO_CHOICE */
4517 { NULL
, NULL
, 0, 0, { NULL
} }
4520 static void select_soundhw (const char *optarg
)
4524 if (*optarg
== '?') {
4527 printf ("Valid sound card names (comma separated):\n");
4528 for (c
= soundhw
; c
->name
; ++c
) {
4529 printf ("%-11s %s\n", c
->name
, c
->descr
);
4531 printf ("\n-soundhw all will enable all of the above\n");
4532 exit (*optarg
!= '?');
4540 if (!strcmp (optarg
, "all")) {
4541 for (c
= soundhw
; c
->name
; ++c
) {
4549 e
= strchr (p
, ',');
4550 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4552 for (c
= soundhw
; c
->name
; ++c
) {
4553 if (!strncmp (c
->name
, p
, l
)) {
4562 "Unknown sound card name (too big to show)\n");
4565 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4570 p
+= l
+ (e
!= NULL
);
4574 goto show_valid_cards
;
4579 static void select_vgahw (const char *p
)
4583 cirrus_vga_enabled
= 0;
4584 std_vga_enabled
= 0;
4587 if (strstart(p
, "std", &opts
)) {
4588 std_vga_enabled
= 1;
4589 } else if (strstart(p
, "cirrus", &opts
)) {
4590 cirrus_vga_enabled
= 1;
4591 } else if (strstart(p
, "vmware", &opts
)) {
4593 } else if (strstart(p
, "xenfb", &opts
)) {
4595 } else if (!strstart(p
, "none", &opts
)) {
4597 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4601 const char *nextopt
;
4603 if (strstart(opts
, ",retrace=", &nextopt
)) {
4605 if (strstart(opts
, "dumb", &nextopt
))
4606 vga_retrace_method
= VGA_RETRACE_DUMB
;
4607 else if (strstart(opts
, "precise", &nextopt
))
4608 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4609 else goto invalid_vga
;
4610 } else goto invalid_vga
;
4616 static int balloon_parse(const char *arg
)
4621 if (!strcmp(arg
, "none")) {
4623 } else if (!strncmp(arg
, "virtio", 6)) {
4625 if (arg
[6] == ',') {
4627 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4628 virtio_balloon_devaddr
= strdup(buf
);
4639 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4641 exit(STATUS_CONTROL_C_EXIT
);
4646 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4650 if(strlen(str
) != 36)
4653 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4654 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4655 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4661 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4667 #define MAX_NET_CLIENTS 32
4671 static void termsig_handler(int signal
)
4673 qemu_system_shutdown_request();
4676 static void sigchld_handler(int signal
)
4678 waitpid(-1, NULL
, WNOHANG
);
4681 static void sighandler_setup(void)
4683 struct sigaction act
;
4685 memset(&act
, 0, sizeof(act
));
4686 act
.sa_handler
= termsig_handler
;
4687 sigaction(SIGINT
, &act
, NULL
);
4688 sigaction(SIGHUP
, &act
, NULL
);
4689 sigaction(SIGTERM
, &act
, NULL
);
4691 act
.sa_handler
= sigchld_handler
;
4692 act
.sa_flags
= SA_NOCLDSTOP
;
4693 sigaction(SIGCHLD
, &act
, NULL
);
4699 /* Look for support files in the same directory as the executable. */
4700 static char *find_datadir(const char *argv0
)
4706 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4713 while (p
!= buf
&& *p
!= '\\')
4716 if (access(buf
, R_OK
) == 0) {
4717 return qemu_strdup(buf
);
4723 /* Find a likely location for support files using the location of the binary.
4724 For installed binaries this will be "$bindir/../share/qemu". When
4725 running from the build tree this will be "$bindir/../pc-bios". */
4726 #define SHARE_SUFFIX "/share/qemu"
4727 #define BUILD_SUFFIX "/pc-bios"
4728 static char *find_datadir(const char *argv0
)
4738 #if defined(__linux__)
4741 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4747 #elif defined(__FreeBSD__)
4750 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4757 /* If we don't have any way of figuring out the actual executable
4758 location then try argv[0]. */
4763 p
= realpath(argv0
, p
);
4771 max_len
= strlen(dir
) +
4772 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4773 res
= qemu_mallocz(max_len
);
4774 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4775 if (access(res
, R_OK
)) {
4776 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4777 if (access(res
, R_OK
)) {
4791 char *qemu_find_file(int type
, const char *name
)
4797 /* If name contains path separators then try it as a straight path. */
4798 if ((strchr(name
, '/') || strchr(name
, '\\'))
4799 && access(name
, R_OK
) == 0) {
4800 return strdup(name
);
4803 case QEMU_FILE_TYPE_BIOS
:
4806 case QEMU_FILE_TYPE_KEYMAP
:
4807 subdir
= "keymaps/";
4812 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4813 buf
= qemu_mallocz(len
);
4814 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4815 if (access(buf
, R_OK
)) {
4822 struct device_config
{
4824 DEV_GENERIC
, /* -device */
4825 DEV_USB
, /* -usbdevice */
4828 const char *cmdline
;
4829 TAILQ_ENTRY(device_config
) next
;
4831 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4833 static void add_device_config(int type
, const char *cmdline
)
4835 struct device_config
*conf
;
4837 conf
= qemu_mallocz(sizeof(*conf
));
4839 conf
->cmdline
= cmdline
;
4840 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4843 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4845 struct device_config
*conf
;
4848 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4849 if (conf
->type
!= type
)
4851 rc
= func(conf
->cmdline
);
4858 static int generic_parse(const char *cmdline
)
4862 dev
= qdev_device_add(cmdline
);
4868 int main(int argc
, char **argv
, char **envp
)
4870 const char *gdbstub_dev
= NULL
;
4871 uint32_t boot_devices_bitmap
= 0;
4873 int snapshot
, linux_boot
, net_boot
;
4874 const char *initrd_filename
;
4875 const char *kernel_filename
, *kernel_cmdline
;
4876 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4878 DisplayChangeListener
*dcl
;
4879 int cyls
, heads
, secs
, translation
;
4880 const char *net_clients
[MAX_NET_CLIENTS
];
4882 DriveOpt
*dopt
, *hda_opt
= NULL
;
4884 const char *r
, *optarg
;
4885 CharDriverState
*monitor_hd
= NULL
;
4886 const char *monitor_device
;
4887 const char *serial_devices
[MAX_SERIAL_PORTS
];
4888 int serial_device_index
;
4889 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4890 int parallel_device_index
;
4891 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4892 int virtio_console_index
;
4893 const char *loadvm
= NULL
;
4894 QEMUMachine
*machine
;
4895 const char *cpu_model
;
4900 const char *pid_file
= NULL
;
4901 const char *incoming
= NULL
;
4904 struct passwd
*pwd
= NULL
;
4905 const char *chroot_dir
= NULL
;
4906 const char *run_as
= NULL
;
4909 int show_vnc_port
= 0;
4911 qemu_cache_utils_init(envp
);
4913 LIST_INIT (&vm_change_state_head
);
4916 struct sigaction act
;
4917 sigfillset(&act
.sa_mask
);
4919 act
.sa_handler
= SIG_IGN
;
4920 sigaction(SIGPIPE
, &act
, NULL
);
4923 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4924 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4925 QEMU to run on a single CPU */
4930 h
= GetCurrentProcess();
4931 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4932 for(i
= 0; i
< 32; i
++) {
4933 if (mask
& (1 << i
))
4938 SetProcessAffinityMask(h
, mask
);
4944 module_call_init(MODULE_INIT_MACHINE
);
4945 machine
= find_default_machine();
4947 initrd_filename
= NULL
;
4950 kernel_filename
= NULL
;
4951 kernel_cmdline
= "";
4952 cyls
= heads
= secs
= 0;
4953 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4954 monitor_device
= "vc:80Cx24C";
4956 serial_devices
[0] = "vc:80Cx24C";
4957 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4958 serial_devices
[i
] = NULL
;
4959 serial_device_index
= 0;
4961 parallel_devices
[0] = "vc:80Cx24C";
4962 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4963 parallel_devices
[i
] = NULL
;
4964 parallel_device_index
= 0;
4966 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4967 virtio_consoles
[i
] = NULL
;
4968 virtio_console_index
= 0;
4970 for (i
= 0; i
< MAX_NODES
; i
++) {
4972 node_cpumask
[i
] = 0;
4982 register_watchdogs();
4990 hda_opt
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4992 const QEMUOption
*popt
;
4995 /* Treat --foo the same as -foo. */
4998 popt
= qemu_options
;
5001 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5005 if (!strcmp(popt
->name
, r
+ 1))
5009 if (popt
->flags
& HAS_ARG
) {
5010 if (optind
>= argc
) {
5011 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5015 optarg
= argv
[optind
++];
5020 switch(popt
->index
) {
5022 machine
= find_machine(optarg
);
5025 printf("Supported machines are:\n");
5026 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5028 printf("%-10s %s (alias of %s)\n",
5029 m
->alias
, m
->desc
, m
->name
);
5030 printf("%-10s %s%s\n",
5032 m
->is_default
? " (default)" : "");
5034 exit(*optarg
!= '?');
5037 case QEMU_OPTION_cpu
:
5038 /* hw initialization will check this */
5039 if (*optarg
== '?') {
5040 /* XXX: implement xxx_cpu_list for targets that still miss it */
5041 #if defined(cpu_list)
5042 cpu_list(stdout
, &fprintf
);
5049 case QEMU_OPTION_initrd
:
5050 initrd_filename
= optarg
;
5052 case QEMU_OPTION_hda
:
5054 hda_opt
= drive_add(optarg
, HD_ALIAS
, 0);
5056 hda_opt
= drive_add(optarg
, HD_ALIAS
5057 ",cyls=%d,heads=%d,secs=%d%s",
5058 0, cyls
, heads
, secs
,
5059 translation
== BIOS_ATA_TRANSLATION_LBA
?
5061 translation
== BIOS_ATA_TRANSLATION_NONE
?
5062 ",trans=none" : "");
5064 case QEMU_OPTION_hdb
:
5065 case QEMU_OPTION_hdc
:
5066 case QEMU_OPTION_hdd
:
5067 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5069 case QEMU_OPTION_drive
:
5070 drive_add(NULL
, "%s", optarg
);
5072 case QEMU_OPTION_mtdblock
:
5073 drive_add(optarg
, MTD_ALIAS
);
5075 case QEMU_OPTION_sd
:
5076 drive_add(optarg
, SD_ALIAS
);
5078 case QEMU_OPTION_pflash
:
5079 drive_add(optarg
, PFLASH_ALIAS
);
5081 case QEMU_OPTION_snapshot
:
5084 case QEMU_OPTION_hdachs
:
5088 cyls
= strtol(p
, (char **)&p
, 0);
5089 if (cyls
< 1 || cyls
> 16383)
5094 heads
= strtol(p
, (char **)&p
, 0);
5095 if (heads
< 1 || heads
> 16)
5100 secs
= strtol(p
, (char **)&p
, 0);
5101 if (secs
< 1 || secs
> 63)
5105 if (!strcmp(p
, "none"))
5106 translation
= BIOS_ATA_TRANSLATION_NONE
;
5107 else if (!strcmp(p
, "lba"))
5108 translation
= BIOS_ATA_TRANSLATION_LBA
;
5109 else if (!strcmp(p
, "auto"))
5110 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5113 } else if (*p
!= '\0') {
5115 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5118 if (hda_opt
!= NULL
)
5119 snprintf(hda_opt
->opt
,
5120 sizeof(hda_opt
->opt
),
5121 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5122 0, cyls
, heads
, secs
,
5123 translation
== BIOS_ATA_TRANSLATION_LBA
?
5125 translation
== BIOS_ATA_TRANSLATION_NONE
?
5126 ",trans=none" : "");
5129 case QEMU_OPTION_numa
:
5130 if (nb_numa_nodes
>= MAX_NODES
) {
5131 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5136 case QEMU_OPTION_nographic
:
5137 display_type
= DT_NOGRAPHIC
;
5139 #ifdef CONFIG_CURSES
5140 case QEMU_OPTION_curses
:
5141 display_type
= DT_CURSES
;
5144 case QEMU_OPTION_portrait
:
5147 case QEMU_OPTION_kernel
:
5148 kernel_filename
= optarg
;
5150 case QEMU_OPTION_append
:
5151 kernel_cmdline
= optarg
;
5153 case QEMU_OPTION_cdrom
:
5154 drive_add(optarg
, CDROM_ALIAS
);
5156 case QEMU_OPTION_boot
:
5158 static const char * const params
[] = {
5159 "order", "once", "menu", NULL
5161 char buf
[sizeof(boot_devices
)];
5162 char *standard_boot_devices
;
5165 if (!strchr(optarg
, '=')) {
5167 pstrcpy(buf
, sizeof(buf
), optarg
);
5168 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5170 "qemu: unknown boot parameter '%s' in '%s'\n",
5176 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5177 boot_devices_bitmap
= parse_bootdevices(buf
);
5178 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5181 if (get_param_value(buf
, sizeof(buf
),
5183 boot_devices_bitmap
|= parse_bootdevices(buf
);
5184 standard_boot_devices
= qemu_strdup(boot_devices
);
5185 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5186 qemu_register_reset(restore_boot_devices
,
5187 standard_boot_devices
);
5189 if (get_param_value(buf
, sizeof(buf
),
5191 if (!strcmp(buf
, "on")) {
5193 } else if (!strcmp(buf
, "off")) {
5197 "qemu: invalid option value '%s'\n",
5205 case QEMU_OPTION_fda
:
5206 case QEMU_OPTION_fdb
:
5207 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5210 case QEMU_OPTION_no_fd_bootchk
:
5214 case QEMU_OPTION_net
:
5215 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5216 fprintf(stderr
, "qemu: too many network clients\n");
5219 net_clients
[nb_net_clients
] = optarg
;
5223 case QEMU_OPTION_tftp
:
5224 legacy_tftp_prefix
= optarg
;
5226 case QEMU_OPTION_bootp
:
5227 legacy_bootp_filename
= optarg
;
5230 case QEMU_OPTION_smb
:
5231 net_slirp_smb(optarg
);
5234 case QEMU_OPTION_redir
:
5235 net_slirp_redir(optarg
);
5238 case QEMU_OPTION_bt
:
5239 add_device_config(DEV_BT
, optarg
);
5242 case QEMU_OPTION_audio_help
:
5246 case QEMU_OPTION_soundhw
:
5247 select_soundhw (optarg
);
5253 case QEMU_OPTION_version
:
5257 case QEMU_OPTION_m
: {
5261 value
= strtoul(optarg
, &ptr
, 10);
5263 case 0: case 'M': case 'm':
5270 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5274 /* On 32-bit hosts, QEMU is limited by virtual address space */
5275 if (value
> (2047 << 20)
5276 #ifndef CONFIG_KQEMU
5277 && HOST_LONG_BITS
== 32
5280 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5283 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5284 fprintf(stderr
, "qemu: ram size too large\n");
5293 const CPULogItem
*item
;
5295 mask
= cpu_str_to_log_mask(optarg
);
5297 printf("Log items (comma separated):\n");
5298 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5299 printf("%-10s %s\n", item
->name
, item
->help
);
5307 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5309 case QEMU_OPTION_gdb
:
5310 gdbstub_dev
= optarg
;
5315 case QEMU_OPTION_bios
:
5318 case QEMU_OPTION_singlestep
:
5326 keyboard_layout
= optarg
;
5329 case QEMU_OPTION_localtime
:
5332 case QEMU_OPTION_vga
:
5333 select_vgahw (optarg
);
5335 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5341 w
= strtol(p
, (char **)&p
, 10);
5344 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5350 h
= strtol(p
, (char **)&p
, 10);
5355 depth
= strtol(p
, (char **)&p
, 10);
5356 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5357 depth
!= 24 && depth
!= 32)
5359 } else if (*p
== '\0') {
5360 depth
= graphic_depth
;
5367 graphic_depth
= depth
;
5371 case QEMU_OPTION_echr
:
5374 term_escape_char
= strtol(optarg
, &r
, 0);
5376 printf("Bad argument to echr\n");
5379 case QEMU_OPTION_monitor
:
5380 monitor_device
= optarg
;
5382 case QEMU_OPTION_serial
:
5383 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5384 fprintf(stderr
, "qemu: too many serial ports\n");
5387 serial_devices
[serial_device_index
] = optarg
;
5388 serial_device_index
++;
5390 case QEMU_OPTION_watchdog
:
5391 i
= select_watchdog(optarg
);
5393 exit (i
== 1 ? 1 : 0);
5395 case QEMU_OPTION_watchdog_action
:
5396 if (select_watchdog_action(optarg
) == -1) {
5397 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5401 case QEMU_OPTION_virtiocon
:
5402 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5403 fprintf(stderr
, "qemu: too many virtio consoles\n");
5406 virtio_consoles
[virtio_console_index
] = optarg
;
5407 virtio_console_index
++;
5409 case QEMU_OPTION_parallel
:
5410 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5411 fprintf(stderr
, "qemu: too many parallel ports\n");
5414 parallel_devices
[parallel_device_index
] = optarg
;
5415 parallel_device_index
++;
5417 case QEMU_OPTION_loadvm
:
5420 case QEMU_OPTION_full_screen
:
5424 case QEMU_OPTION_no_frame
:
5427 case QEMU_OPTION_alt_grab
:
5430 case QEMU_OPTION_no_quit
:
5433 case QEMU_OPTION_sdl
:
5434 display_type
= DT_SDL
;
5437 case QEMU_OPTION_pidfile
:
5441 case QEMU_OPTION_win2k_hack
:
5442 win2k_install_hack
= 1;
5444 case QEMU_OPTION_rtc_td_hack
:
5447 case QEMU_OPTION_acpitable
:
5448 if(acpi_table_add(optarg
) < 0) {
5449 fprintf(stderr
, "Wrong acpi table provided\n");
5453 case QEMU_OPTION_smbios
:
5454 if(smbios_entry_add(optarg
) < 0) {
5455 fprintf(stderr
, "Wrong smbios provided\n");
5461 case QEMU_OPTION_enable_kqemu
:
5464 case QEMU_OPTION_kernel_kqemu
:
5469 case QEMU_OPTION_enable_kvm
:
5476 case QEMU_OPTION_usb
:
5479 case QEMU_OPTION_usbdevice
:
5481 add_device_config(DEV_USB
, optarg
);
5483 case QEMU_OPTION_device
:
5484 add_device_config(DEV_GENERIC
, optarg
);
5486 case QEMU_OPTION_smp
:
5487 smp_cpus
= atoi(optarg
);
5489 fprintf(stderr
, "Invalid number of CPUs\n");
5493 case QEMU_OPTION_vnc
:
5494 display_type
= DT_VNC
;
5495 vnc_display
= optarg
;
5498 case QEMU_OPTION_no_acpi
:
5501 case QEMU_OPTION_no_hpet
:
5504 case QEMU_OPTION_balloon
:
5505 if (balloon_parse(optarg
) < 0) {
5506 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5511 case QEMU_OPTION_no_reboot
:
5514 case QEMU_OPTION_no_shutdown
:
5517 case QEMU_OPTION_show_cursor
:
5520 case QEMU_OPTION_uuid
:
5521 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5522 fprintf(stderr
, "Fail to parse UUID string."
5523 " Wrong format.\n");
5528 case QEMU_OPTION_daemonize
:
5532 case QEMU_OPTION_option_rom
:
5533 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5534 fprintf(stderr
, "Too many option ROMs\n");
5537 option_rom
[nb_option_roms
] = optarg
;
5540 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5541 case QEMU_OPTION_semihosting
:
5542 semihosting_enabled
= 1;
5545 case QEMU_OPTION_name
:
5546 qemu_name
= qemu_strdup(optarg
);
5548 char *p
= strchr(qemu_name
, ',');
5551 if (strncmp(p
, "process=", 8)) {
5552 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5560 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5561 case QEMU_OPTION_prom_env
:
5562 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5563 fprintf(stderr
, "Too many prom variables\n");
5566 prom_envs
[nb_prom_envs
] = optarg
;
5571 case QEMU_OPTION_old_param
:
5575 case QEMU_OPTION_clock
:
5576 configure_alarms(optarg
);
5578 case QEMU_OPTION_startdate
:
5581 time_t rtc_start_date
;
5582 if (!strcmp(optarg
, "now")) {
5583 rtc_date_offset
= -1;
5585 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5593 } else if (sscanf(optarg
, "%d-%d-%d",
5596 &tm
.tm_mday
) == 3) {
5605 rtc_start_date
= mktimegm(&tm
);
5606 if (rtc_start_date
== -1) {
5608 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5609 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5612 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5616 case QEMU_OPTION_tb_size
:
5617 tb_size
= strtol(optarg
, NULL
, 0);
5621 case QEMU_OPTION_icount
:
5623 if (strcmp(optarg
, "auto") == 0) {
5624 icount_time_shift
= -1;
5626 icount_time_shift
= strtol(optarg
, NULL
, 0);
5629 case QEMU_OPTION_incoming
:
5633 case QEMU_OPTION_chroot
:
5634 chroot_dir
= optarg
;
5636 case QEMU_OPTION_runas
:
5641 case QEMU_OPTION_xen_domid
:
5642 xen_domid
= atoi(optarg
);
5644 case QEMU_OPTION_xen_create
:
5645 xen_mode
= XEN_CREATE
;
5647 case QEMU_OPTION_xen_attach
:
5648 xen_mode
= XEN_ATTACH
;
5655 /* If no data_dir is specified then try to find it relative to the
5658 data_dir
= find_datadir(argv
[0]);
5660 /* If all else fails use the install patch specified when building. */
5662 data_dir
= CONFIG_QEMU_SHAREDIR
;
5665 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5666 if (kvm_allowed
&& kqemu_allowed
) {
5668 "You can not enable both KVM and kqemu at the same time\n");
5673 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5674 if (smp_cpus
> machine
->max_cpus
) {
5675 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5676 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5681 if (display_type
== DT_NOGRAPHIC
) {
5682 if (serial_device_index
== 0)
5683 serial_devices
[0] = "stdio";
5684 if (parallel_device_index
== 0)
5685 parallel_devices
[0] = "null";
5686 if (strncmp(monitor_device
, "vc", 2) == 0)
5687 monitor_device
= "stdio";
5694 if (pipe(fds
) == -1)
5705 len
= read(fds
[0], &status
, 1);
5706 if (len
== -1 && (errno
== EINTR
))
5711 else if (status
== 1) {
5712 fprintf(stderr
, "Could not acquire pidfile\n");
5729 signal(SIGTSTP
, SIG_IGN
);
5730 signal(SIGTTOU
, SIG_IGN
);
5731 signal(SIGTTIN
, SIG_IGN
);
5734 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5737 write(fds
[1], &status
, 1);
5739 fprintf(stderr
, "Could not acquire pid file\n");
5748 if (qemu_init_main_loop()) {
5749 fprintf(stderr
, "qemu_init_main_loop failed\n");
5752 linux_boot
= (kernel_filename
!= NULL
);
5754 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5755 fprintf(stderr
, "-append only allowed with -kernel option\n");
5759 if (!linux_boot
&& initrd_filename
!= NULL
) {
5760 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5764 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5767 if (init_timer_alarm() < 0) {
5768 fprintf(stderr
, "could not initialize alarm timer\n");
5771 if (use_icount
&& icount_time_shift
< 0) {
5773 /* 125MIPS seems a reasonable initial guess at the guest speed.
5774 It will be corrected fairly quickly anyway. */
5775 icount_time_shift
= 3;
5776 init_icount_adjust();
5783 /* init network clients */
5784 if (nb_net_clients
== 0) {
5785 /* if no clients, we use a default config */
5786 net_clients
[nb_net_clients
++] = "nic";
5788 net_clients
[nb_net_clients
++] = "user";
5792 for(i
= 0;i
< nb_net_clients
; i
++) {
5793 if (net_client_parse(net_clients
[i
]) < 0)
5797 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5798 net_set_boot_mask(net_boot
);
5802 /* init the bluetooth world */
5803 if (foreach_device_config(DEV_BT
, bt_parse
))
5806 /* init the memory */
5808 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5811 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5812 guest ram allocation. It needs to go away. */
5813 if (kqemu_allowed
) {
5814 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5815 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5816 if (!kqemu_phys_ram_base
) {
5817 fprintf(stderr
, "Could not allocate physical memory\n");
5823 /* init the dynamic translator */
5824 cpu_exec_init_all(tb_size
* 1024 * 1024);
5828 /* we always create the cdrom drive, even if no disk is there */
5829 drive_add(NULL
, CDROM_ALIAS
);
5831 /* we always create at least one floppy */
5832 drive_add(NULL
, FD_ALIAS
, 0);
5834 /* we always create one sd slot, even if no card is in it */
5835 drive_add(NULL
, SD_ALIAS
);
5837 /* open the virtual block devices */
5839 TAILQ_FOREACH(dopt
, &driveopts
, next
) {
5841 if (drive_init(dopt
, snapshot
, machine
, &fatal_error
) == NULL
)
5846 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5847 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5850 /* must be after terminal init, SDL library changes signal handlers */
5854 /* Maintain compatibility with multiple stdio monitors */
5855 if (!strcmp(monitor_device
,"stdio")) {
5856 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5857 const char *devname
= serial_devices
[i
];
5858 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5859 monitor_device
= NULL
;
5861 } else if (devname
&& !strcmp(devname
,"stdio")) {
5862 monitor_device
= NULL
;
5863 serial_devices
[i
] = "mon:stdio";
5869 if (nb_numa_nodes
> 0) {
5872 if (nb_numa_nodes
> smp_cpus
) {
5873 nb_numa_nodes
= smp_cpus
;
5876 /* If no memory size if given for any node, assume the default case
5877 * and distribute the available memory equally across all nodes
5879 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5880 if (node_mem
[i
] != 0)
5883 if (i
== nb_numa_nodes
) {
5884 uint64_t usedmem
= 0;
5886 /* On Linux, the each node's border has to be 8MB aligned,
5887 * the final node gets the rest.
5889 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5890 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5891 usedmem
+= node_mem
[i
];
5893 node_mem
[i
] = ram_size
- usedmem
;
5896 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5897 if (node_cpumask
[i
] != 0)
5900 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5901 * must cope with this anyway, because there are BIOSes out there in
5902 * real machines which also use this scheme.
5904 if (i
== nb_numa_nodes
) {
5905 for (i
= 0; i
< smp_cpus
; i
++) {
5906 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5911 if (kvm_enabled()) {
5914 ret
= kvm_init(smp_cpus
);
5916 fprintf(stderr
, "failed to initialize KVM\n");
5921 if (monitor_device
) {
5922 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5924 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5929 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5930 const char *devname
= serial_devices
[i
];
5931 if (devname
&& strcmp(devname
, "none")) {
5933 snprintf(label
, sizeof(label
), "serial%d", i
);
5934 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5935 if (!serial_hds
[i
]) {
5936 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5943 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5944 const char *devname
= parallel_devices
[i
];
5945 if (devname
&& strcmp(devname
, "none")) {
5947 snprintf(label
, sizeof(label
), "parallel%d", i
);
5948 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5949 if (!parallel_hds
[i
]) {
5950 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5957 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5958 const char *devname
= virtio_consoles
[i
];
5959 if (devname
&& strcmp(devname
, "none")) {
5961 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5962 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5963 if (!virtcon_hds
[i
]) {
5964 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5971 module_call_init(MODULE_INIT_DEVICE
);
5973 if (machine
->compat_props
) {
5974 qdev_prop_register_compat(machine
->compat_props
);
5976 machine
->init(ram_size
, boot_devices
,
5977 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5980 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5981 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5982 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5988 current_machine
= machine
;
5990 /* init USB devices */
5992 foreach_device_config(DEV_USB
, usb_parse
);
5995 /* init generic devices */
5996 if (foreach_device_config(DEV_GENERIC
, generic_parse
))
6000 dumb_display_init();
6001 /* just use the first displaystate for the moment */
6004 if (display_type
== DT_DEFAULT
) {
6005 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6006 display_type
= DT_SDL
;
6008 display_type
= DT_VNC
;
6009 vnc_display
= "localhost:0,to=99";
6015 switch (display_type
) {
6018 #if defined(CONFIG_CURSES)
6020 curses_display_init(ds
, full_screen
);
6023 #if defined(CONFIG_SDL)
6025 sdl_display_init(ds
, full_screen
, no_frame
);
6027 #elif defined(CONFIG_COCOA)
6029 cocoa_display_init(ds
, full_screen
);
6033 vnc_display_init(ds
);
6034 if (vnc_display_open(ds
, vnc_display
) < 0)
6037 if (show_vnc_port
) {
6038 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6046 dcl
= ds
->listeners
;
6047 while (dcl
!= NULL
) {
6048 if (dcl
->dpy_refresh
!= NULL
) {
6049 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6050 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6055 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6056 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6057 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6060 text_consoles_set_display(display_state
);
6061 qemu_chr_initial_reset();
6063 if (monitor_device
&& monitor_hd
)
6064 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6066 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6067 const char *devname
= serial_devices
[i
];
6068 if (devname
&& strcmp(devname
, "none")) {
6069 if (strstart(devname
, "vc", 0))
6070 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6074 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6075 const char *devname
= parallel_devices
[i
];
6076 if (devname
&& strcmp(devname
, "none")) {
6077 if (strstart(devname
, "vc", 0))
6078 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6082 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6083 const char *devname
= virtio_consoles
[i
];
6084 if (virtcon_hds
[i
] && devname
) {
6085 if (strstart(devname
, "vc", 0))
6086 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6090 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6091 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6097 do_loadvm(cur_mon
, loadvm
);
6100 qemu_start_incoming_migration(incoming
);
6111 len
= write(fds
[1], &status
, 1);
6112 if (len
== -1 && (errno
== EINTR
))
6119 TFR(fd
= open("/dev/null", O_RDWR
));
6125 pwd
= getpwnam(run_as
);
6127 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6133 if (chroot(chroot_dir
) < 0) {
6134 fprintf(stderr
, "chroot failed\n");
6141 if (setgid(pwd
->pw_gid
) < 0) {
6142 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6145 if (setuid(pwd
->pw_uid
) < 0) {
6146 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6149 if (setuid(0) != -1) {
6150 fprintf(stderr
, "Dropping privileges failed\n");