4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
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>
111 #include <mmsystem.h>
115 #if defined(__APPLE__) || defined(main)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 return qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/watchdog.h"
142 #include "hw/smbios.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
156 #include "audio/audio.h"
157 #include "migration.h"
160 #include "qemu-option.h"
161 #include "qemu-config.h"
165 #include "exec-all.h"
167 #include "qemu_socket.h"
169 #include "slirp/libslirp.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
179 to store the VM snapshots */
180 struct drivelist drives
= TAILQ_HEAD_INITIALIZER(drives
);
181 struct driveoptlist driveopts
= TAILQ_HEAD_INITIALIZER(driveopts
);
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 static DisplayState
*display_state
;
184 DisplayType display_type
= DT_DEFAULT
;
185 const char* keyboard_layout
= NULL
;
186 int64_t ticks_per_sec
;
189 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 int vga_interface_type
= VGA_CIRRUS
;
196 int graphic_width
= 1024;
197 int graphic_height
= 768;
198 int graphic_depth
= 8;
200 int graphic_width
= 800;
201 int graphic_height
= 600;
202 int graphic_depth
= 15;
204 static int full_screen
= 0;
206 static int no_frame
= 0;
209 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
210 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
211 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
213 int win2k_install_hack
= 0;
220 const char *vnc_display
;
221 int acpi_enabled
= 1;
223 int virtio_balloon
= 1;
224 const char *virtio_balloon_devaddr
;
229 int graphic_rotate
= 0;
230 uint8_t irq0override
= 1;
234 WatchdogTimerModel
*watchdog
= NULL
;
235 int watchdog_action
= WDT_RESET
;
236 const char *option_rom
[MAX_OPTION_ROMS
];
238 int semihosting_enabled
= 0;
242 const char *qemu_name
;
244 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
245 unsigned int nb_prom_envs
= 0;
246 const char *prom_envs
[MAX_PROM_ENVS
];
251 uint64_t node_mem
[MAX_NODES
];
252 uint64_t node_cpumask
[MAX_NODES
];
254 static CPUState
*cur_cpu
;
255 static CPUState
*next_cpu
;
256 static int timer_alarm_pending
= 1;
257 /* Conversion factor from emulated instructions to virtual clock ticks. */
258 static int icount_time_shift
;
259 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
260 #define MAX_ICOUNT_SHIFT 10
261 /* Compensate for varying guest execution speed. */
262 static int64_t qemu_icount_bias
;
263 static QEMUTimer
*icount_rt_timer
;
264 static QEMUTimer
*icount_vm_timer
;
265 static QEMUTimer
*nographic_timer
;
267 uint8_t qemu_uuid
[16];
269 static QEMUBootSetHandler
*boot_set_handler
;
270 static void *boot_set_opaque
;
272 /***********************************************************/
273 /* x86 ISA bus support */
275 target_phys_addr_t isa_mem_base
= 0;
278 /***********************************************************/
279 void hw_error(const char *fmt
, ...)
285 fprintf(stderr
, "qemu: hardware error: ");
286 vfprintf(stderr
, fmt
, ap
);
287 fprintf(stderr
, "\n");
288 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
289 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
291 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
293 cpu_dump_state(env
, stderr
, fprintf
, 0);
300 static void set_proc_name(const char *s
)
302 #if defined(__linux__) && defined(PR_SET_NAME)
306 name
[sizeof(name
) - 1] = 0;
307 strncpy(name
, s
, sizeof(name
));
308 /* Could rewrite argv[0] too, but that's a bit more complicated.
309 This simple way is enough for `top'. */
310 prctl(PR_SET_NAME
, name
);
317 static QEMUBalloonEvent
*qemu_balloon_event
;
318 void *qemu_balloon_event_opaque
;
320 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
322 qemu_balloon_event
= func
;
323 qemu_balloon_event_opaque
= opaque
;
326 void qemu_balloon(ram_addr_t target
)
328 if (qemu_balloon_event
)
329 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
332 ram_addr_t
qemu_balloon_status(void)
334 if (qemu_balloon_event
)
335 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
339 /***********************************************************/
342 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
343 static void *qemu_put_kbd_event_opaque
;
344 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
345 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
347 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
349 qemu_put_kbd_event_opaque
= opaque
;
350 qemu_put_kbd_event
= func
;
353 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
354 void *opaque
, int absolute
,
357 QEMUPutMouseEntry
*s
, *cursor
;
359 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
361 s
->qemu_put_mouse_event
= func
;
362 s
->qemu_put_mouse_event_opaque
= opaque
;
363 s
->qemu_put_mouse_event_absolute
= absolute
;
364 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
367 if (!qemu_put_mouse_event_head
) {
368 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
372 cursor
= qemu_put_mouse_event_head
;
373 while (cursor
->next
!= NULL
)
374 cursor
= cursor
->next
;
377 qemu_put_mouse_event_current
= s
;
382 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
384 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
386 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
389 cursor
= qemu_put_mouse_event_head
;
390 while (cursor
!= NULL
&& cursor
!= entry
) {
392 cursor
= cursor
->next
;
395 if (cursor
== NULL
) // does not exist or list empty
397 else if (prev
== NULL
) { // entry is head
398 qemu_put_mouse_event_head
= cursor
->next
;
399 if (qemu_put_mouse_event_current
== entry
)
400 qemu_put_mouse_event_current
= cursor
->next
;
401 qemu_free(entry
->qemu_put_mouse_event_name
);
406 prev
->next
= entry
->next
;
408 if (qemu_put_mouse_event_current
== entry
)
409 qemu_put_mouse_event_current
= prev
;
411 qemu_free(entry
->qemu_put_mouse_event_name
);
415 void kbd_put_keycode(int keycode
)
417 if (qemu_put_kbd_event
) {
418 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
422 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
424 QEMUPutMouseEvent
*mouse_event
;
425 void *mouse_event_opaque
;
428 if (!qemu_put_mouse_event_current
) {
433 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
435 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
438 if (graphic_rotate
) {
439 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
442 width
= graphic_width
- 1;
443 mouse_event(mouse_event_opaque
,
444 width
- dy
, dx
, dz
, buttons_state
);
446 mouse_event(mouse_event_opaque
,
447 dx
, dy
, dz
, buttons_state
);
451 int kbd_mouse_is_absolute(void)
453 if (!qemu_put_mouse_event_current
)
456 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
459 void do_info_mice(Monitor
*mon
)
461 QEMUPutMouseEntry
*cursor
;
464 if (!qemu_put_mouse_event_head
) {
465 monitor_printf(mon
, "No mouse devices connected\n");
469 monitor_printf(mon
, "Mouse devices available:\n");
470 cursor
= qemu_put_mouse_event_head
;
471 while (cursor
!= NULL
) {
472 monitor_printf(mon
, "%c Mouse #%d: %s\n",
473 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
474 index
, cursor
->qemu_put_mouse_event_name
);
476 cursor
= cursor
->next
;
480 void do_mouse_set(Monitor
*mon
, int index
)
482 QEMUPutMouseEntry
*cursor
;
485 if (!qemu_put_mouse_event_head
) {
486 monitor_printf(mon
, "No mouse devices connected\n");
490 cursor
= qemu_put_mouse_event_head
;
491 while (cursor
!= NULL
&& index
!= i
) {
493 cursor
= cursor
->next
;
497 qemu_put_mouse_event_current
= cursor
;
499 monitor_printf(mon
, "Mouse at given index not found\n");
502 /* compute with 96 bit intermediate result: (a*b)/c */
503 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
508 #ifdef HOST_WORDS_BIGENDIAN
518 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
519 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
522 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
526 /***********************************************************/
527 /* real time host monotonic timer */
529 #define QEMU_TIMER_BASE 1000000000LL
533 static int64_t clock_freq
;
535 static void init_get_clock(void)
539 ret
= QueryPerformanceFrequency(&freq
);
541 fprintf(stderr
, "Could not calibrate ticks\n");
544 clock_freq
= freq
.QuadPart
;
547 static int64_t get_clock(void)
550 QueryPerformanceCounter(&ti
);
551 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
556 static int use_rt_clock
;
558 static void init_get_clock(void)
561 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
562 || defined(__DragonFly__)
565 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
572 static int64_t get_clock(void)
574 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
575 || defined(__DragonFly__)
578 clock_gettime(CLOCK_MONOTONIC
, &ts
);
579 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
583 /* XXX: using gettimeofday leads to problems if the date
584 changes, so it should be avoided. */
586 gettimeofday(&tv
, NULL
);
587 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
592 /* Return the virtual CPU time, based on the instruction counter. */
593 static int64_t cpu_get_icount(void)
596 CPUState
*env
= cpu_single_env
;;
597 icount
= qemu_icount
;
600 fprintf(stderr
, "Bad clock read\n");
601 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
603 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
606 /***********************************************************/
607 /* guest cycle counter */
609 static int64_t cpu_ticks_prev
;
610 static int64_t cpu_ticks_offset
;
611 static int64_t cpu_clock_offset
;
612 static int cpu_ticks_enabled
;
614 /* return the host CPU cycle counter and handle stop/restart */
615 int64_t cpu_get_ticks(void)
618 return cpu_get_icount();
620 if (!cpu_ticks_enabled
) {
621 return cpu_ticks_offset
;
624 ticks
= cpu_get_real_ticks();
625 if (cpu_ticks_prev
> ticks
) {
626 /* Note: non increasing ticks may happen if the host uses
628 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
630 cpu_ticks_prev
= ticks
;
631 return ticks
+ cpu_ticks_offset
;
635 /* return the host CPU monotonic timer and handle stop/restart */
636 static int64_t cpu_get_clock(void)
639 if (!cpu_ticks_enabled
) {
640 return cpu_clock_offset
;
643 return ti
+ cpu_clock_offset
;
647 /* enable cpu_get_ticks() */
648 void cpu_enable_ticks(void)
650 if (!cpu_ticks_enabled
) {
651 cpu_ticks_offset
-= cpu_get_real_ticks();
652 cpu_clock_offset
-= get_clock();
653 cpu_ticks_enabled
= 1;
657 /* disable cpu_get_ticks() : the clock is stopped. You must not call
658 cpu_get_ticks() after that. */
659 void cpu_disable_ticks(void)
661 if (cpu_ticks_enabled
) {
662 cpu_ticks_offset
= cpu_get_ticks();
663 cpu_clock_offset
= cpu_get_clock();
664 cpu_ticks_enabled
= 0;
668 /***********************************************************/
671 #define QEMU_TIMER_REALTIME 0
672 #define QEMU_TIMER_VIRTUAL 1
676 /* XXX: add frequency */
684 struct QEMUTimer
*next
;
687 struct qemu_alarm_timer
{
691 int (*start
)(struct qemu_alarm_timer
*t
);
692 void (*stop
)(struct qemu_alarm_timer
*t
);
693 void (*rearm
)(struct qemu_alarm_timer
*t
);
697 #define ALARM_FLAG_DYNTICKS 0x1
698 #define ALARM_FLAG_EXPIRED 0x2
700 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
702 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
705 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
707 if (!alarm_has_dynticks(t
))
713 /* TODO: MIN_TIMER_REARM_US should be optimized */
714 #define MIN_TIMER_REARM_US 250
716 static struct qemu_alarm_timer
*alarm_timer
;
720 struct qemu_alarm_win32
{
723 } alarm_win32_data
= {0, -1};
725 static int win32_start_timer(struct qemu_alarm_timer
*t
);
726 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
727 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
731 static int unix_start_timer(struct qemu_alarm_timer
*t
);
732 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
736 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
737 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
738 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
740 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
741 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
743 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
744 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
746 #endif /* __linux__ */
750 /* Correlation between real and virtual time is always going to be
751 fairly approximate, so ignore small variation.
752 When the guest is idle real and virtual time will be aligned in
754 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
756 static void icount_adjust(void)
761 static int64_t last_delta
;
762 /* If the VM is not running, then do nothing. */
766 cur_time
= cpu_get_clock();
767 cur_icount
= qemu_get_clock(vm_clock
);
768 delta
= cur_icount
- cur_time
;
769 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
771 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
772 && icount_time_shift
> 0) {
773 /* The guest is getting too far ahead. Slow time down. */
777 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
778 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
779 /* The guest is getting too far behind. Speed time up. */
783 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
786 static void icount_adjust_rt(void * opaque
)
788 qemu_mod_timer(icount_rt_timer
,
789 qemu_get_clock(rt_clock
) + 1000);
793 static void icount_adjust_vm(void * opaque
)
795 qemu_mod_timer(icount_vm_timer
,
796 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
800 static void init_icount_adjust(void)
802 /* Have both realtime and virtual time triggers for speed adjustment.
803 The realtime trigger catches emulated time passing too slowly,
804 the virtual time trigger catches emulated time passing too fast.
805 Realtime triggers occur even when idle, so use them less frequently
807 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
808 qemu_mod_timer(icount_rt_timer
,
809 qemu_get_clock(rt_clock
) + 1000);
810 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
811 qemu_mod_timer(icount_vm_timer
,
812 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
815 static struct qemu_alarm_timer alarm_timers
[] = {
818 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
819 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
820 /* HPET - if available - is preferred */
821 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
822 /* ...otherwise try RTC */
823 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
825 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
827 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
828 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
829 {"win32", 0, win32_start_timer
,
830 win32_stop_timer
, NULL
, &alarm_win32_data
},
835 static void show_available_alarms(void)
839 printf("Available alarm timers, in order of precedence:\n");
840 for (i
= 0; alarm_timers
[i
].name
; i
++)
841 printf("%s\n", alarm_timers
[i
].name
);
844 static void configure_alarms(char const *opt
)
848 int count
= ARRAY_SIZE(alarm_timers
) - 1;
851 struct qemu_alarm_timer tmp
;
853 if (!strcmp(opt
, "?")) {
854 show_available_alarms();
860 /* Reorder the array */
861 name
= strtok(arg
, ",");
863 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
864 if (!strcmp(alarm_timers
[i
].name
, name
))
869 fprintf(stderr
, "Unknown clock %s\n", name
);
878 tmp
= alarm_timers
[i
];
879 alarm_timers
[i
] = alarm_timers
[cur
];
880 alarm_timers
[cur
] = tmp
;
884 name
= strtok(NULL
, ",");
890 /* Disable remaining timers */
891 for (i
= cur
; i
< count
; i
++)
892 alarm_timers
[i
].name
= NULL
;
894 show_available_alarms();
902 static QEMUTimer
*active_timers
[2];
904 static QEMUClock
*qemu_new_clock(int type
)
907 clock
= qemu_mallocz(sizeof(QEMUClock
));
912 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
916 ts
= qemu_mallocz(sizeof(QEMUTimer
));
923 void qemu_free_timer(QEMUTimer
*ts
)
928 /* stop a timer, but do not dealloc it */
929 void qemu_del_timer(QEMUTimer
*ts
)
933 /* NOTE: this code must be signal safe because
934 qemu_timer_expired() can be called from a signal. */
935 pt
= &active_timers
[ts
->clock
->type
];
948 /* modify the current timer so that it will be fired when current_time
949 >= expire_time. The corresponding callback will be called. */
950 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
956 /* add the timer in the sorted list */
957 /* NOTE: this code must be signal safe because
958 qemu_timer_expired() can be called from a signal. */
959 pt
= &active_timers
[ts
->clock
->type
];
964 if (t
->expire_time
> expire_time
)
968 ts
->expire_time
= expire_time
;
972 /* Rearm if necessary */
973 if (pt
== &active_timers
[ts
->clock
->type
]) {
974 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
975 qemu_rearm_alarm_timer(alarm_timer
);
977 /* Interrupt execution to force deadline recalculation. */
983 int qemu_timer_pending(QEMUTimer
*ts
)
986 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
993 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
997 return (timer_head
->expire_time
<= current_time
);
1000 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1006 if (!ts
|| ts
->expire_time
> current_time
)
1008 /* remove timer from the list before calling the callback */
1009 *ptimer_head
= ts
->next
;
1012 /* run the callback (the timer list can be modified) */
1017 int64_t qemu_get_clock(QEMUClock
*clock
)
1019 switch(clock
->type
) {
1020 case QEMU_TIMER_REALTIME
:
1021 return get_clock() / 1000000;
1023 case QEMU_TIMER_VIRTUAL
:
1025 return cpu_get_icount();
1027 return cpu_get_clock();
1032 static void init_timers(void)
1035 ticks_per_sec
= QEMU_TIMER_BASE
;
1036 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1037 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1041 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1043 uint64_t expire_time
;
1045 if (qemu_timer_pending(ts
)) {
1046 expire_time
= ts
->expire_time
;
1050 qemu_put_be64(f
, expire_time
);
1053 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1055 uint64_t expire_time
;
1057 expire_time
= qemu_get_be64(f
);
1058 if (expire_time
!= -1) {
1059 qemu_mod_timer(ts
, expire_time
);
1065 static void timer_save(QEMUFile
*f
, void *opaque
)
1067 if (cpu_ticks_enabled
) {
1068 hw_error("cannot save state if virtual timers are running");
1070 qemu_put_be64(f
, cpu_ticks_offset
);
1071 qemu_put_be64(f
, ticks_per_sec
);
1072 qemu_put_be64(f
, cpu_clock_offset
);
1075 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1077 if (version_id
!= 1 && version_id
!= 2)
1079 if (cpu_ticks_enabled
) {
1082 cpu_ticks_offset
=qemu_get_be64(f
);
1083 ticks_per_sec
=qemu_get_be64(f
);
1084 if (version_id
== 2) {
1085 cpu_clock_offset
=qemu_get_be64(f
);
1090 static void qemu_event_increment(void);
1093 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1094 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1097 static void host_alarm_handler(int host_signum
)
1101 #define DISP_FREQ 1000
1103 static int64_t delta_min
= INT64_MAX
;
1104 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1106 ti
= qemu_get_clock(vm_clock
);
1107 if (last_clock
!= 0) {
1108 delta
= ti
- last_clock
;
1109 if (delta
< delta_min
)
1111 if (delta
> delta_max
)
1114 if (++count
== DISP_FREQ
) {
1115 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1116 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1117 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1118 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1119 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1121 delta_min
= INT64_MAX
;
1129 if (alarm_has_dynticks(alarm_timer
) ||
1131 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1132 qemu_get_clock(vm_clock
))) ||
1133 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1134 qemu_get_clock(rt_clock
))) {
1135 qemu_event_increment();
1136 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1138 #ifndef CONFIG_IOTHREAD
1140 /* stop the currently executing cpu because a timer occured */
1144 timer_alarm_pending
= 1;
1145 qemu_notify_event();
1149 static int64_t qemu_next_deadline(void)
1153 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1154 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1155 qemu_get_clock(vm_clock
);
1157 /* To avoid problems with overflow limit this to 2^32. */
1167 #if defined(__linux__) || defined(_WIN32)
1168 static uint64_t qemu_next_deadline_dyntick(void)
1176 delta
= (qemu_next_deadline() + 999) / 1000;
1178 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1179 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1180 qemu_get_clock(rt_clock
))*1000;
1181 if (rtdelta
< delta
)
1185 if (delta
< MIN_TIMER_REARM_US
)
1186 delta
= MIN_TIMER_REARM_US
;
1194 /* Sets a specific flag */
1195 static int fcntl_setfl(int fd
, int flag
)
1199 flags
= fcntl(fd
, F_GETFL
);
1203 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1209 #if defined(__linux__)
1211 #define RTC_FREQ 1024
1213 static void enable_sigio_timer(int fd
)
1215 struct sigaction act
;
1218 sigfillset(&act
.sa_mask
);
1220 act
.sa_handler
= host_alarm_handler
;
1222 sigaction(SIGIO
, &act
, NULL
);
1223 fcntl_setfl(fd
, O_ASYNC
);
1224 fcntl(fd
, F_SETOWN
, getpid());
1227 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1229 struct hpet_info info
;
1232 fd
= open("/dev/hpet", O_RDONLY
);
1237 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1239 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1240 "error, but for better emulation accuracy type:\n"
1241 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1245 /* Check capabilities */
1246 r
= ioctl(fd
, HPET_INFO
, &info
);
1250 /* Enable periodic mode */
1251 r
= ioctl(fd
, HPET_EPI
, 0);
1252 if (info
.hi_flags
&& (r
< 0))
1255 /* Enable interrupt */
1256 r
= ioctl(fd
, HPET_IE_ON
, 0);
1260 enable_sigio_timer(fd
);
1261 t
->priv
= (void *)(long)fd
;
1269 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1271 int fd
= (long)t
->priv
;
1276 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1279 unsigned long current_rtc_freq
= 0;
1281 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1284 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1285 if (current_rtc_freq
!= RTC_FREQ
&&
1286 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1287 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1288 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1289 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1292 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1298 enable_sigio_timer(rtc_fd
);
1300 t
->priv
= (void *)(long)rtc_fd
;
1305 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1307 int rtc_fd
= (long)t
->priv
;
1312 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1316 struct sigaction act
;
1318 sigfillset(&act
.sa_mask
);
1320 act
.sa_handler
= host_alarm_handler
;
1322 sigaction(SIGALRM
, &act
, NULL
);
1325 * Initialize ev struct to 0 to avoid valgrind complaining
1326 * about uninitialized data in timer_create call
1328 memset(&ev
, 0, sizeof(ev
));
1329 ev
.sigev_value
.sival_int
= 0;
1330 ev
.sigev_notify
= SIGEV_SIGNAL
;
1331 ev
.sigev_signo
= SIGALRM
;
1333 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1334 perror("timer_create");
1336 /* disable dynticks */
1337 fprintf(stderr
, "Dynamic Ticks disabled\n");
1342 t
->priv
= (void *)(long)host_timer
;
1347 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1349 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1351 timer_delete(host_timer
);
1354 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1356 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1357 struct itimerspec timeout
;
1358 int64_t nearest_delta_us
= INT64_MAX
;
1361 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1362 !active_timers
[QEMU_TIMER_VIRTUAL
])
1365 nearest_delta_us
= qemu_next_deadline_dyntick();
1367 /* check whether a timer is already running */
1368 if (timer_gettime(host_timer
, &timeout
)) {
1370 fprintf(stderr
, "Internal timer error: aborting\n");
1373 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1374 if (current_us
&& current_us
<= nearest_delta_us
)
1377 timeout
.it_interval
.tv_sec
= 0;
1378 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1379 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1380 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1381 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1383 fprintf(stderr
, "Internal timer error: aborting\n");
1388 #endif /* defined(__linux__) */
1390 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1392 struct sigaction act
;
1393 struct itimerval itv
;
1397 sigfillset(&act
.sa_mask
);
1399 act
.sa_handler
= host_alarm_handler
;
1401 sigaction(SIGALRM
, &act
, NULL
);
1403 itv
.it_interval
.tv_sec
= 0;
1404 /* for i386 kernel 2.6 to get 1 ms */
1405 itv
.it_interval
.tv_usec
= 999;
1406 itv
.it_value
.tv_sec
= 0;
1407 itv
.it_value
.tv_usec
= 10 * 1000;
1409 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1416 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1418 struct itimerval itv
;
1420 memset(&itv
, 0, sizeof(itv
));
1421 setitimer(ITIMER_REAL
, &itv
, NULL
);
1424 #endif /* !defined(_WIN32) */
1429 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1432 struct qemu_alarm_win32
*data
= t
->priv
;
1435 memset(&tc
, 0, sizeof(tc
));
1436 timeGetDevCaps(&tc
, sizeof(tc
));
1438 if (data
->period
< tc
.wPeriodMin
)
1439 data
->period
= tc
.wPeriodMin
;
1441 timeBeginPeriod(data
->period
);
1443 flags
= TIME_CALLBACK_FUNCTION
;
1444 if (alarm_has_dynticks(t
))
1445 flags
|= TIME_ONESHOT
;
1447 flags
|= TIME_PERIODIC
;
1449 data
->timerId
= timeSetEvent(1, // interval (ms)
1450 data
->period
, // resolution
1451 host_alarm_handler
, // function
1452 (DWORD
)t
, // parameter
1455 if (!data
->timerId
) {
1456 perror("Failed to initialize win32 alarm timer");
1457 timeEndPeriod(data
->period
);
1464 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1466 struct qemu_alarm_win32
*data
= t
->priv
;
1468 timeKillEvent(data
->timerId
);
1469 timeEndPeriod(data
->period
);
1472 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1474 struct qemu_alarm_win32
*data
= t
->priv
;
1475 uint64_t nearest_delta_us
;
1477 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1478 !active_timers
[QEMU_TIMER_VIRTUAL
])
1481 nearest_delta_us
= qemu_next_deadline_dyntick();
1482 nearest_delta_us
/= 1000;
1484 timeKillEvent(data
->timerId
);
1486 data
->timerId
= timeSetEvent(1,
1490 TIME_ONESHOT
| TIME_PERIODIC
);
1492 if (!data
->timerId
) {
1493 perror("Failed to re-arm win32 alarm timer");
1495 timeEndPeriod(data
->period
);
1502 static int init_timer_alarm(void)
1504 struct qemu_alarm_timer
*t
= NULL
;
1507 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1508 t
= &alarm_timers
[i
];
1528 static void quit_timers(void)
1530 alarm_timer
->stop(alarm_timer
);
1534 /***********************************************************/
1535 /* host time/date access */
1536 void qemu_get_timedate(struct tm
*tm
, int offset
)
1543 if (rtc_date_offset
== -1) {
1547 ret
= localtime(&ti
);
1549 ti
-= rtc_date_offset
;
1553 memcpy(tm
, ret
, sizeof(struct tm
));
1556 int qemu_timedate_diff(struct tm
*tm
)
1560 if (rtc_date_offset
== -1)
1562 seconds
= mktimegm(tm
);
1564 seconds
= mktime(tm
);
1566 seconds
= mktimegm(tm
) + rtc_date_offset
;
1568 return seconds
- time(NULL
);
1572 static void socket_cleanup(void)
1577 static int socket_init(void)
1582 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1584 err
= WSAGetLastError();
1585 fprintf(stderr
, "WSAStartup: %d\n", err
);
1588 atexit(socket_cleanup
);
1593 /***********************************************************/
1594 /* Bluetooth support */
1597 static struct HCIInfo
*hci_table
[MAX_NICS
];
1599 static struct bt_vlan_s
{
1600 struct bt_scatternet_s net
;
1602 struct bt_vlan_s
*next
;
1605 /* find or alloc a new bluetooth "VLAN" */
1606 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1608 struct bt_vlan_s
**pvlan
, *vlan
;
1609 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1613 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1615 pvlan
= &first_bt_vlan
;
1616 while (*pvlan
!= NULL
)
1617 pvlan
= &(*pvlan
)->next
;
1622 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1626 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1631 static struct HCIInfo null_hci
= {
1632 .cmd_send
= null_hci_send
,
1633 .sco_send
= null_hci_send
,
1634 .acl_send
= null_hci_send
,
1635 .bdaddr_set
= null_hci_addr_set
,
1638 struct HCIInfo
*qemu_next_hci(void)
1640 if (cur_hci
== nb_hcis
)
1643 return hci_table
[cur_hci
++];
1646 static struct HCIInfo
*hci_init(const char *str
)
1649 struct bt_scatternet_s
*vlan
= 0;
1651 if (!strcmp(str
, "null"))
1654 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1656 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1657 else if (!strncmp(str
, "hci", 3)) {
1660 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1661 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1666 vlan
= qemu_find_bt_vlan(0);
1668 return bt_new_hci(vlan
);
1671 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1676 static int bt_hci_parse(const char *str
)
1678 struct HCIInfo
*hci
;
1681 if (nb_hcis
>= MAX_NICS
) {
1682 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1686 hci
= hci_init(str
);
1695 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1696 hci
->bdaddr_set(hci
, bdaddr
.b
);
1698 hci_table
[nb_hcis
++] = hci
;
1703 static void bt_vhci_add(int vlan_id
)
1705 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1708 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1709 "an empty scatternet %i\n", vlan_id
);
1711 bt_vhci_init(bt_new_hci(vlan
));
1714 static struct bt_device_s
*bt_device_add(const char *opt
)
1716 struct bt_scatternet_s
*vlan
;
1718 char *endp
= strstr(opt
, ",vlan=");
1719 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1722 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1725 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1727 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1732 vlan
= qemu_find_bt_vlan(vlan_id
);
1735 fprintf(stderr
, "qemu: warning: adding a slave device to "
1736 "an empty scatternet %i\n", vlan_id
);
1738 if (!strcmp(devname
, "keyboard"))
1739 return bt_keyboard_init(vlan
);
1741 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1745 static int bt_parse(const char *opt
)
1747 const char *endp
, *p
;
1750 if (strstart(opt
, "hci", &endp
)) {
1751 if (!*endp
|| *endp
== ',') {
1753 if (!strstart(endp
, ",vlan=", 0))
1756 return bt_hci_parse(opt
);
1758 } else if (strstart(opt
, "vhci", &endp
)) {
1759 if (!*endp
|| *endp
== ',') {
1761 if (strstart(endp
, ",vlan=", &p
)) {
1762 vlan
= strtol(p
, (char **) &endp
, 0);
1764 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1768 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1777 } else if (strstart(opt
, "device:", &endp
))
1778 return !bt_device_add(endp
);
1780 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1784 /***********************************************************/
1785 /* QEMU Block devices */
1787 #define HD_ALIAS "index=%d,media=disk"
1788 #define CDROM_ALIAS "index=2,media=cdrom"
1789 #define FD_ALIAS "index=%d,if=floppy"
1790 #define PFLASH_ALIAS "if=pflash"
1791 #define MTD_ALIAS "if=mtd"
1792 #define SD_ALIAS "index=0,if=sd"
1794 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1801 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1804 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1806 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1807 __FUNCTION__
, optstr
);
1811 qemu_opt_set(opts
, "file", file
);
1815 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1819 /* seek interface, bus and unit */
1821 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1822 if (dinfo
->type
== type
&&
1823 dinfo
->bus
== bus
&&
1824 dinfo
->unit
== unit
)
1831 DriveInfo
*drive_get_by_id(const char *id
)
1835 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1836 if (strcmp(id
, dinfo
->id
))
1843 int drive_get_max_bus(BlockInterfaceType type
)
1849 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1850 if(dinfo
->type
== type
&&
1851 dinfo
->bus
> max_bus
)
1852 max_bus
= dinfo
->bus
;
1857 const char *drive_get_serial(BlockDriverState
*bdrv
)
1861 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1862 if (dinfo
->bdrv
== bdrv
)
1863 return dinfo
->serial
;
1869 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1873 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1874 if (dinfo
->bdrv
== bdrv
)
1875 return dinfo
->onerror
;
1878 return BLOCK_ERR_STOP_ENOSPC
;
1881 static void bdrv_format_print(void *opaque
, const char *name
)
1883 fprintf(stderr
, " %s", name
);
1886 void drive_uninit(BlockDriverState
*bdrv
)
1890 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1891 if (dinfo
->bdrv
!= bdrv
)
1893 qemu_opts_del(dinfo
->opts
);
1894 TAILQ_REMOVE(&drives
, dinfo
, next
);
1900 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1904 const char *file
= NULL
;
1907 const char *mediastr
= "";
1908 BlockInterfaceType type
;
1909 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1910 int bus_id
, unit_id
;
1911 int cyls
, heads
, secs
, translation
;
1912 BlockDriver
*drv
= NULL
;
1913 QEMUMachine
*machine
= opaque
;
1917 int bdrv_flags
, onerror
;
1918 const char *devaddr
;
1924 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1927 if (machine
->use_scsi
) {
1929 max_devs
= MAX_SCSI_DEVS
;
1930 pstrcpy(devname
, sizeof(devname
), "scsi");
1933 max_devs
= MAX_IDE_DEVS
;
1934 pstrcpy(devname
, sizeof(devname
), "ide");
1938 /* extract parameters */
1939 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1940 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1941 index
= qemu_opt_get_number(opts
, "index", -1);
1943 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1944 heads
= qemu_opt_get_number(opts
, "heads", 0);
1945 secs
= qemu_opt_get_number(opts
, "secs", 0);
1947 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1949 file
= qemu_opt_get(opts
, "file");
1950 serial
= qemu_opt_get(opts
, "serial");
1952 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1953 pstrcpy(devname
, sizeof(devname
), buf
);
1954 if (!strcmp(buf
, "ide")) {
1956 max_devs
= MAX_IDE_DEVS
;
1957 } else if (!strcmp(buf
, "scsi")) {
1959 max_devs
= MAX_SCSI_DEVS
;
1960 } else if (!strcmp(buf
, "floppy")) {
1963 } else if (!strcmp(buf
, "pflash")) {
1966 } else if (!strcmp(buf
, "mtd")) {
1969 } else if (!strcmp(buf
, "sd")) {
1972 } else if (!strcmp(buf
, "virtio")) {
1975 } else if (!strcmp(buf
, "xen")) {
1978 } else if (!strcmp(buf
, "none")) {
1982 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1987 if (cyls
|| heads
|| secs
) {
1988 if (cyls
< 1 || cyls
> 16383) {
1989 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1992 if (heads
< 1 || heads
> 16) {
1993 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1996 if (secs
< 1 || secs
> 63) {
1997 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2002 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2005 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2009 if (!strcmp(buf
, "none"))
2010 translation
= BIOS_ATA_TRANSLATION_NONE
;
2011 else if (!strcmp(buf
, "lba"))
2012 translation
= BIOS_ATA_TRANSLATION_LBA
;
2013 else if (!strcmp(buf
, "auto"))
2014 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2016 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2021 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2022 if (!strcmp(buf
, "disk")) {
2024 } else if (!strcmp(buf
, "cdrom")) {
2025 if (cyls
|| secs
|| heads
) {
2027 "qemu: '%s' invalid physical CHS format\n", buf
);
2030 media
= MEDIA_CDROM
;
2032 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2037 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2038 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2040 else if (!strcmp(buf
, "writethrough"))
2042 else if (!strcmp(buf
, "writeback"))
2045 fprintf(stderr
, "qemu: invalid cache option\n");
2050 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2051 if (strcmp(buf
, "?") == 0) {
2052 fprintf(stderr
, "qemu: Supported formats:");
2053 bdrv_iterate_format(bdrv_format_print
, NULL
);
2054 fprintf(stderr
, "\n");
2057 drv
= bdrv_find_format(buf
);
2059 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2064 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2065 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2066 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2067 fprintf(stderr
, "werror is no supported by this format\n");
2070 if (!strcmp(buf
, "ignore"))
2071 onerror
= BLOCK_ERR_IGNORE
;
2072 else if (!strcmp(buf
, "enospc"))
2073 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2074 else if (!strcmp(buf
, "stop"))
2075 onerror
= BLOCK_ERR_STOP_ANY
;
2076 else if (!strcmp(buf
, "report"))
2077 onerror
= BLOCK_ERR_REPORT
;
2079 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2084 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2085 if (type
!= IF_VIRTIO
) {
2086 fprintf(stderr
, "addr is not supported\n");
2091 /* compute bus and unit according index */
2094 if (bus_id
!= 0 || unit_id
!= -1) {
2096 "qemu: index cannot be used with bus and unit\n");
2104 unit_id
= index
% max_devs
;
2105 bus_id
= index
/ max_devs
;
2109 /* if user doesn't specify a unit_id,
2110 * try to find the first free
2113 if (unit_id
== -1) {
2115 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2117 if (max_devs
&& unit_id
>= max_devs
) {
2118 unit_id
-= max_devs
;
2126 if (max_devs
&& unit_id
>= max_devs
) {
2127 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2128 unit_id
, max_devs
- 1);
2133 * ignore multiple definitions
2136 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2143 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2144 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2145 dinfo
->id
= qemu_strdup(buf
);
2147 /* no id supplied -> create one */
2148 dinfo
->id
= qemu_mallocz(32);
2149 if (type
== IF_IDE
|| type
== IF_SCSI
)
2150 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2152 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2153 devname
, bus_id
, mediastr
, unit_id
);
2155 snprintf(dinfo
->id
, 32, "%s%s%i",
2156 devname
, mediastr
, unit_id
);
2158 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2159 dinfo
->devaddr
= devaddr
;
2161 dinfo
->bus
= bus_id
;
2162 dinfo
->unit
= unit_id
;
2163 dinfo
->onerror
= onerror
;
2166 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2167 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2176 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2177 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2181 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2186 /* FIXME: This isn't really a floppy, but it's a reasonable
2189 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2196 /* add virtio block device */
2197 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2198 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2199 qemu_opt_set(opts
, "drive", dinfo
->id
);
2201 qemu_opt_set(opts
, "addr", devaddr
);
2212 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2213 cache
= 2; /* always use write-back with snapshot */
2215 if (cache
== 0) /* no caching */
2216 bdrv_flags
|= BDRV_O_NOCACHE
;
2217 else if (cache
== 2) /* write-back */
2218 bdrv_flags
|= BDRV_O_CACHE_WB
;
2219 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2220 fprintf(stderr
, "qemu: could not open disk image %s\n",
2224 if (bdrv_key_required(dinfo
->bdrv
))
2230 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2232 QEMUMachine
*machine
= opaque
;
2233 int fatal_error
= 0;
2235 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2242 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2244 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2245 qemu_opt_set(opts
, "snapshot", "on");
2250 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2252 boot_set_handler
= func
;
2253 boot_set_opaque
= opaque
;
2256 int qemu_boot_set(const char *boot_devices
)
2258 if (!boot_set_handler
) {
2261 return boot_set_handler(boot_set_opaque
, boot_devices
);
2264 static int parse_bootdevices(char *devices
)
2266 /* We just do some generic consistency checks */
2270 for (p
= devices
; *p
!= '\0'; p
++) {
2271 /* Allowed boot devices are:
2272 * a-b: floppy disk drives
2273 * c-f: IDE disk drives
2274 * g-m: machine implementation dependant drives
2275 * n-p: network devices
2276 * It's up to each machine implementation to check if the given boot
2277 * devices match the actual hardware implementation and firmware
2280 if (*p
< 'a' || *p
> 'p') {
2281 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2284 if (bitmap
& (1 << (*p
- 'a'))) {
2285 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2288 bitmap
|= 1 << (*p
- 'a');
2293 static void restore_boot_devices(void *opaque
)
2295 char *standard_boot_devices
= opaque
;
2297 qemu_boot_set(standard_boot_devices
);
2299 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2300 qemu_free(standard_boot_devices
);
2303 static void numa_add(const char *optarg
)
2307 unsigned long long value
, endvalue
;
2310 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2311 if (!strcmp(option
, "node")) {
2312 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2313 nodenr
= nb_numa_nodes
;
2315 nodenr
= strtoull(option
, NULL
, 10);
2318 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2319 node_mem
[nodenr
] = 0;
2321 value
= strtoull(option
, &endptr
, 0);
2323 case 0: case 'M': case 'm':
2330 node_mem
[nodenr
] = value
;
2332 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2333 node_cpumask
[nodenr
] = 0;
2335 value
= strtoull(option
, &endptr
, 10);
2338 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2340 if (*endptr
== '-') {
2341 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2342 if (endvalue
>= 63) {
2345 "only 63 CPUs in NUMA mode supported.\n");
2347 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2352 node_cpumask
[nodenr
] = value
;
2359 /***********************************************************/
2362 static USBPort
*used_usb_ports
;
2363 static USBPort
*free_usb_ports
;
2365 /* ??? Maybe change this to register a hub to keep track of the topology. */
2366 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2367 usb_attachfn attach
)
2369 port
->opaque
= opaque
;
2370 port
->index
= index
;
2371 port
->attach
= attach
;
2372 port
->next
= free_usb_ports
;
2373 free_usb_ports
= port
;
2376 int usb_device_add_dev(USBDevice
*dev
)
2380 /* Find a USB port to add the device to. */
2381 port
= free_usb_ports
;
2385 /* Create a new hub and chain it on. */
2386 free_usb_ports
= NULL
;
2387 port
->next
= used_usb_ports
;
2388 used_usb_ports
= port
;
2390 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2391 usb_attach(port
, hub
);
2392 port
= free_usb_ports
;
2395 free_usb_ports
= port
->next
;
2396 port
->next
= used_usb_ports
;
2397 used_usb_ports
= port
;
2398 usb_attach(port
, dev
);
2402 static void usb_msd_password_cb(void *opaque
, int err
)
2404 USBDevice
*dev
= opaque
;
2407 usb_device_add_dev(dev
);
2409 dev
->handle_destroy(dev
);
2412 static int usb_device_add(const char *devname
, int is_hotplug
)
2417 if (!free_usb_ports
)
2420 if (strstart(devname
, "host:", &p
)) {
2421 dev
= usb_host_device_open(p
);
2422 } else if (!strcmp(devname
, "mouse")) {
2423 dev
= usb_mouse_init();
2424 } else if (!strcmp(devname
, "tablet")) {
2425 dev
= usb_tablet_init();
2426 } else if (!strcmp(devname
, "keyboard")) {
2427 dev
= usb_keyboard_init();
2428 } else if (strstart(devname
, "disk:", &p
)) {
2429 BlockDriverState
*bs
;
2431 dev
= usb_msd_init(p
);
2434 bs
= usb_msd_get_bdrv(dev
);
2435 if (bdrv_key_required(bs
)) {
2438 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2443 } else if (!strcmp(devname
, "wacom-tablet")) {
2444 dev
= usb_wacom_init();
2445 } else if (strstart(devname
, "serial:", &p
)) {
2446 dev
= usb_serial_init(p
);
2447 #ifdef CONFIG_BRLAPI
2448 } else if (!strcmp(devname
, "braille")) {
2449 dev
= usb_baum_init();
2451 } else if (strstart(devname
, "net:", &p
)) {
2454 if (net_client_init(NULL
, "nic", p
) < 0)
2456 nd_table
[nic
].model
= "usb";
2457 dev
= usb_net_init(&nd_table
[nic
]);
2458 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2459 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2460 bt_new_hci(qemu_find_bt_vlan(0)));
2467 return usb_device_add_dev(dev
);
2470 int usb_device_del_addr(int bus_num
, int addr
)
2476 if (!used_usb_ports
)
2482 lastp
= &used_usb_ports
;
2483 port
= used_usb_ports
;
2484 while (port
&& port
->dev
->addr
!= addr
) {
2485 lastp
= &port
->next
;
2493 *lastp
= port
->next
;
2494 usb_attach(port
, NULL
);
2495 dev
->handle_destroy(dev
);
2496 port
->next
= free_usb_ports
;
2497 free_usb_ports
= port
;
2501 static int usb_device_del(const char *devname
)
2506 if (strstart(devname
, "host:", &p
))
2507 return usb_host_device_close(p
);
2509 if (!used_usb_ports
)
2512 p
= strchr(devname
, '.');
2515 bus_num
= strtoul(devname
, NULL
, 0);
2516 addr
= strtoul(p
+ 1, NULL
, 0);
2518 return usb_device_del_addr(bus_num
, addr
);
2521 static int usb_parse(const char *cmdline
)
2523 return usb_device_add(cmdline
, 0);
2526 void do_usb_add(Monitor
*mon
, const char *devname
)
2528 usb_device_add(devname
, 1);
2531 void do_usb_del(Monitor
*mon
, const char *devname
)
2533 usb_device_del(devname
);
2536 void usb_info(Monitor
*mon
)
2540 const char *speed_str
;
2543 monitor_printf(mon
, "USB support not enabled\n");
2547 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2551 switch(dev
->speed
) {
2555 case USB_SPEED_FULL
:
2558 case USB_SPEED_HIGH
:
2565 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2566 0, dev
->addr
, speed_str
, dev
->devname
);
2570 /***********************************************************/
2571 /* PCMCIA/Cardbus */
2573 static struct pcmcia_socket_entry_s
{
2574 PCMCIASocket
*socket
;
2575 struct pcmcia_socket_entry_s
*next
;
2576 } *pcmcia_sockets
= 0;
2578 void pcmcia_socket_register(PCMCIASocket
*socket
)
2580 struct pcmcia_socket_entry_s
*entry
;
2582 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2583 entry
->socket
= socket
;
2584 entry
->next
= pcmcia_sockets
;
2585 pcmcia_sockets
= entry
;
2588 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2590 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2592 ptr
= &pcmcia_sockets
;
2593 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2594 if (entry
->socket
== socket
) {
2600 void pcmcia_info(Monitor
*mon
)
2602 struct pcmcia_socket_entry_s
*iter
;
2604 if (!pcmcia_sockets
)
2605 monitor_printf(mon
, "No PCMCIA sockets\n");
2607 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2608 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2609 iter
->socket
->attached
? iter
->socket
->card_string
:
2613 /***********************************************************/
2614 /* register display */
2616 struct DisplayAllocator default_allocator
= {
2617 defaultallocator_create_displaysurface
,
2618 defaultallocator_resize_displaysurface
,
2619 defaultallocator_free_displaysurface
2622 void register_displaystate(DisplayState
*ds
)
2632 DisplayState
*get_displaystate(void)
2634 return display_state
;
2637 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2639 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2640 return ds
->allocator
;
2645 static void dumb_display_init(void)
2647 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2648 ds
->allocator
= &default_allocator
;
2649 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2650 register_displaystate(ds
);
2653 /***********************************************************/
2656 typedef struct IOHandlerRecord
{
2658 IOCanRWHandler
*fd_read_poll
;
2660 IOHandler
*fd_write
;
2663 /* temporary data */
2665 struct IOHandlerRecord
*next
;
2668 static IOHandlerRecord
*first_io_handler
;
2670 /* XXX: fd_read_poll should be suppressed, but an API change is
2671 necessary in the character devices to suppress fd_can_read(). */
2672 int qemu_set_fd_handler2(int fd
,
2673 IOCanRWHandler
*fd_read_poll
,
2675 IOHandler
*fd_write
,
2678 IOHandlerRecord
**pioh
, *ioh
;
2680 if (!fd_read
&& !fd_write
) {
2681 pioh
= &first_io_handler
;
2686 if (ioh
->fd
== fd
) {
2693 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2697 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2698 ioh
->next
= first_io_handler
;
2699 first_io_handler
= ioh
;
2702 ioh
->fd_read_poll
= fd_read_poll
;
2703 ioh
->fd_read
= fd_read
;
2704 ioh
->fd_write
= fd_write
;
2705 ioh
->opaque
= opaque
;
2711 int qemu_set_fd_handler(int fd
,
2713 IOHandler
*fd_write
,
2716 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2720 /***********************************************************/
2721 /* Polling handling */
2723 typedef struct PollingEntry
{
2726 struct PollingEntry
*next
;
2729 static PollingEntry
*first_polling_entry
;
2731 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2733 PollingEntry
**ppe
, *pe
;
2734 pe
= qemu_mallocz(sizeof(PollingEntry
));
2736 pe
->opaque
= opaque
;
2737 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2742 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2744 PollingEntry
**ppe
, *pe
;
2745 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2747 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2755 /***********************************************************/
2756 /* Wait objects support */
2757 typedef struct WaitObjects
{
2759 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2760 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2761 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2764 static WaitObjects wait_objects
= {0};
2766 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2768 WaitObjects
*w
= &wait_objects
;
2770 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2772 w
->events
[w
->num
] = handle
;
2773 w
->func
[w
->num
] = func
;
2774 w
->opaque
[w
->num
] = opaque
;
2779 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2782 WaitObjects
*w
= &wait_objects
;
2785 for (i
= 0; i
< w
->num
; i
++) {
2786 if (w
->events
[i
] == handle
)
2789 w
->events
[i
] = w
->events
[i
+ 1];
2790 w
->func
[i
] = w
->func
[i
+ 1];
2791 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2799 /***********************************************************/
2800 /* ram save/restore */
2802 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2806 v
= qemu_get_byte(f
);
2809 if (qemu_get_buffer(f
, buf
, len
) != len
)
2813 v
= qemu_get_byte(f
);
2814 memset(buf
, v
, len
);
2820 if (qemu_file_has_error(f
))
2826 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2831 if (qemu_get_be32(f
) != last_ram_offset
)
2833 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2834 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2841 #define BDRV_HASH_BLOCK_SIZE 1024
2842 #define IOBUF_SIZE 4096
2843 #define RAM_CBLOCK_MAGIC 0xfabe
2845 typedef struct RamDecompressState
{
2848 uint8_t buf
[IOBUF_SIZE
];
2849 } RamDecompressState
;
2851 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2854 memset(s
, 0, sizeof(*s
));
2856 ret
= inflateInit(&s
->zstream
);
2862 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2866 s
->zstream
.avail_out
= len
;
2867 s
->zstream
.next_out
= buf
;
2868 while (s
->zstream
.avail_out
> 0) {
2869 if (s
->zstream
.avail_in
== 0) {
2870 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2872 clen
= qemu_get_be16(s
->f
);
2873 if (clen
> IOBUF_SIZE
)
2875 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2876 s
->zstream
.avail_in
= clen
;
2877 s
->zstream
.next_in
= s
->buf
;
2879 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2880 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2887 static void ram_decompress_close(RamDecompressState
*s
)
2889 inflateEnd(&s
->zstream
);
2892 #define RAM_SAVE_FLAG_FULL 0x01
2893 #define RAM_SAVE_FLAG_COMPRESS 0x02
2894 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2895 #define RAM_SAVE_FLAG_PAGE 0x08
2896 #define RAM_SAVE_FLAG_EOS 0x10
2898 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2900 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2901 uint32_t *array
= (uint32_t *)page
;
2904 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2905 if (array
[i
] != val
)
2912 static int ram_save_block(QEMUFile
*f
)
2914 static ram_addr_t current_addr
= 0;
2915 ram_addr_t saved_addr
= current_addr
;
2916 ram_addr_t addr
= 0;
2919 while (addr
< last_ram_offset
) {
2920 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2923 cpu_physical_memory_reset_dirty(current_addr
,
2924 current_addr
+ TARGET_PAGE_SIZE
,
2925 MIGRATION_DIRTY_FLAG
);
2927 p
= qemu_get_ram_ptr(current_addr
);
2929 if (is_dup_page(p
, *p
)) {
2930 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2931 qemu_put_byte(f
, *p
);
2933 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2934 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2940 addr
+= TARGET_PAGE_SIZE
;
2941 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2947 static uint64_t bytes_transferred
= 0;
2949 static ram_addr_t
ram_save_remaining(void)
2952 ram_addr_t count
= 0;
2954 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2955 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2962 uint64_t ram_bytes_remaining(void)
2964 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2967 uint64_t ram_bytes_transferred(void)
2969 return bytes_transferred
;
2972 uint64_t ram_bytes_total(void)
2974 return last_ram_offset
;
2977 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2980 uint64_t bytes_transferred_last
;
2982 uint64_t expected_time
= 0;
2984 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2985 qemu_file_set_error(f
);
2990 /* Make sure all dirty bits are set */
2991 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2992 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2993 cpu_physical_memory_set_dirty(addr
);
2996 /* Enable dirty memory tracking */
2997 cpu_physical_memory_set_dirty_tracking(1);
2999 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3002 bytes_transferred_last
= bytes_transferred
;
3003 bwidth
= get_clock();
3005 while (!qemu_file_rate_limit(f
)) {
3008 ret
= ram_save_block(f
);
3009 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3010 if (ret
== 0) /* no more blocks */
3014 bwidth
= get_clock() - bwidth
;
3015 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3017 /* if we haven't transferred anything this round, force expected_time to a
3018 * a very high value, but without crashing */
3022 /* try transferring iterative blocks of memory */
3026 /* flush all remaining blocks regardless of rate limiting */
3027 while (ram_save_block(f
) != 0) {
3028 bytes_transferred
+= TARGET_PAGE_SIZE
;
3030 cpu_physical_memory_set_dirty_tracking(0);
3033 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3035 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3037 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3040 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3042 RamDecompressState s1
, *s
= &s1
;
3046 if (ram_decompress_open(s
, f
) < 0)
3048 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3049 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3050 fprintf(stderr
, "Error while reading ram block header\n");
3054 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3055 BDRV_HASH_BLOCK_SIZE
) < 0) {
3056 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3061 printf("Error block header\n");
3065 ram_decompress_close(s
);
3070 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3075 if (version_id
== 1)
3076 return ram_load_v1(f
, opaque
);
3078 if (version_id
== 2) {
3079 if (qemu_get_be32(f
) != last_ram_offset
)
3081 return ram_load_dead(f
, opaque
);
3084 if (version_id
!= 3)
3088 addr
= qemu_get_be64(f
);
3090 flags
= addr
& ~TARGET_PAGE_MASK
;
3091 addr
&= TARGET_PAGE_MASK
;
3093 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3094 if (addr
!= last_ram_offset
)
3098 if (flags
& RAM_SAVE_FLAG_FULL
) {
3099 if (ram_load_dead(f
, opaque
) < 0)
3103 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3104 uint8_t ch
= qemu_get_byte(f
);
3105 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3108 (!kvm_enabled() || kvm_has_sync_mmu())) {
3109 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3112 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3113 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3114 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3119 void qemu_service_io(void)
3121 qemu_notify_event();
3124 /***********************************************************/
3125 /* bottom halves (can be seen as timers which expire ASAP) */
3136 static QEMUBH
*first_bh
= NULL
;
3138 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3141 bh
= qemu_mallocz(sizeof(QEMUBH
));
3143 bh
->opaque
= opaque
;
3144 bh
->next
= first_bh
;
3149 int qemu_bh_poll(void)
3155 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3156 if (!bh
->deleted
&& bh
->scheduled
) {
3165 /* remove deleted bhs */
3179 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3187 void qemu_bh_schedule(QEMUBH
*bh
)
3193 /* stop the currently executing CPU to execute the BH ASAP */
3194 qemu_notify_event();
3197 void qemu_bh_cancel(QEMUBH
*bh
)
3202 void qemu_bh_delete(QEMUBH
*bh
)
3208 static void qemu_bh_update_timeout(int *timeout
)
3212 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3213 if (!bh
->deleted
&& bh
->scheduled
) {
3215 /* idle bottom halves will be polled at least
3217 *timeout
= MIN(10, *timeout
);
3219 /* non-idle bottom halves will be executed
3228 /***********************************************************/
3229 /* machine registration */
3231 static QEMUMachine
*first_machine
= NULL
;
3232 QEMUMachine
*current_machine
= NULL
;
3234 int qemu_register_machine(QEMUMachine
*m
)
3237 pm
= &first_machine
;
3245 static QEMUMachine
*find_machine(const char *name
)
3249 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3250 if (!strcmp(m
->name
, name
))
3252 if (m
->alias
&& !strcmp(m
->alias
, name
))
3258 static QEMUMachine
*find_default_machine(void)
3262 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3263 if (m
->is_default
) {
3270 /***********************************************************/
3271 /* main execution loop */
3273 static void gui_update(void *opaque
)
3275 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3276 DisplayState
*ds
= opaque
;
3277 DisplayChangeListener
*dcl
= ds
->listeners
;
3281 while (dcl
!= NULL
) {
3282 if (dcl
->gui_timer_interval
&&
3283 dcl
->gui_timer_interval
< interval
)
3284 interval
= dcl
->gui_timer_interval
;
3287 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3290 static void nographic_update(void *opaque
)
3292 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3294 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3297 struct vm_change_state_entry
{
3298 VMChangeStateHandler
*cb
;
3300 LIST_ENTRY (vm_change_state_entry
) entries
;
3303 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3305 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3308 VMChangeStateEntry
*e
;
3310 e
= qemu_mallocz(sizeof (*e
));
3314 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3318 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3320 LIST_REMOVE (e
, entries
);
3324 static void vm_state_notify(int running
, int reason
)
3326 VMChangeStateEntry
*e
;
3328 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3329 e
->cb(e
->opaque
, running
, reason
);
3333 static void resume_all_vcpus(void);
3334 static void pause_all_vcpus(void);
3341 vm_state_notify(1, 0);
3342 qemu_rearm_alarm_timer(alarm_timer
);
3347 /* reset/shutdown handler */
3349 typedef struct QEMUResetEntry
{
3350 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3351 QEMUResetHandler
*func
;
3355 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3356 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3357 static int reset_requested
;
3358 static int shutdown_requested
;
3359 static int powerdown_requested
;
3360 static int debug_requested
;
3361 static int vmstop_requested
;
3363 int qemu_shutdown_requested(void)
3365 int r
= shutdown_requested
;
3366 shutdown_requested
= 0;
3370 int qemu_reset_requested(void)
3372 int r
= reset_requested
;
3373 reset_requested
= 0;
3377 int qemu_powerdown_requested(void)
3379 int r
= powerdown_requested
;
3380 powerdown_requested
= 0;
3384 static int qemu_debug_requested(void)
3386 int r
= debug_requested
;
3387 debug_requested
= 0;
3391 static int qemu_vmstop_requested(void)
3393 int r
= vmstop_requested
;
3394 vmstop_requested
= 0;
3398 static void do_vm_stop(int reason
)
3401 cpu_disable_ticks();
3404 vm_state_notify(0, reason
);
3408 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3410 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3413 re
->opaque
= opaque
;
3414 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3417 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3421 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3422 if (re
->func
== func
&& re
->opaque
== opaque
) {
3423 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3430 void qemu_system_reset(void)
3432 QEMUResetEntry
*re
, *nre
;
3434 /* reset all devices */
3435 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3436 re
->func(re
->opaque
);
3440 void qemu_system_reset_request(void)
3443 shutdown_requested
= 1;
3445 reset_requested
= 1;
3447 qemu_notify_event();
3450 void qemu_system_shutdown_request(void)
3452 shutdown_requested
= 1;
3453 qemu_notify_event();
3456 void qemu_system_powerdown_request(void)
3458 powerdown_requested
= 1;
3459 qemu_notify_event();
3462 #ifdef CONFIG_IOTHREAD
3463 static void qemu_system_vmstop_request(int reason
)
3465 vmstop_requested
= reason
;
3466 qemu_notify_event();
3471 static int io_thread_fd
= -1;
3473 static void qemu_event_increment(void)
3475 static const char byte
= 0;
3477 if (io_thread_fd
== -1)
3480 write(io_thread_fd
, &byte
, sizeof(byte
));
3483 static void qemu_event_read(void *opaque
)
3485 int fd
= (unsigned long)opaque
;
3488 /* Drain the notify pipe */
3491 len
= read(fd
, buffer
, sizeof(buffer
));
3492 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3495 static int qemu_event_init(void)
3504 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3508 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3512 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3513 (void *)(unsigned long)fds
[0]);
3515 io_thread_fd
= fds
[1];
3524 HANDLE qemu_event_handle
;
3526 static void dummy_event_handler(void *opaque
)
3530 static int qemu_event_init(void)
3532 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3533 if (!qemu_event_handle
) {
3534 perror("Failed CreateEvent");
3537 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3541 static void qemu_event_increment(void)
3543 SetEvent(qemu_event_handle
);
3547 static int cpu_can_run(CPUState
*env
)
3556 #ifndef CONFIG_IOTHREAD
3557 static int qemu_init_main_loop(void)
3559 return qemu_event_init();
3562 void qemu_init_vcpu(void *_env
)
3564 CPUState
*env
= _env
;
3571 int qemu_cpu_self(void *env
)
3576 static void resume_all_vcpus(void)
3580 static void pause_all_vcpus(void)
3584 void qemu_cpu_kick(void *env
)
3589 void qemu_notify_event(void)
3591 CPUState
*env
= cpu_single_env
;
3598 #define qemu_mutex_lock_iothread() do { } while (0)
3599 #define qemu_mutex_unlock_iothread() do { } while (0)
3601 void vm_stop(int reason
)
3606 #else /* CONFIG_IOTHREAD */
3608 #include "qemu-thread.h"
3610 QemuMutex qemu_global_mutex
;
3611 static QemuMutex qemu_fair_mutex
;
3613 static QemuThread io_thread
;
3615 static QemuThread
*tcg_cpu_thread
;
3616 static QemuCond
*tcg_halt_cond
;
3618 static int qemu_system_ready
;
3620 static QemuCond qemu_cpu_cond
;
3622 static QemuCond qemu_system_cond
;
3623 static QemuCond qemu_pause_cond
;
3625 static void block_io_signals(void);
3626 static void unblock_io_signals(void);
3627 static int tcg_has_work(void);
3629 static int qemu_init_main_loop(void)
3633 ret
= qemu_event_init();
3637 qemu_cond_init(&qemu_pause_cond
);
3638 qemu_mutex_init(&qemu_fair_mutex
);
3639 qemu_mutex_init(&qemu_global_mutex
);
3640 qemu_mutex_lock(&qemu_global_mutex
);
3642 unblock_io_signals();
3643 qemu_thread_self(&io_thread
);
3648 static void qemu_wait_io_event(CPUState
*env
)
3650 while (!tcg_has_work())
3651 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3653 qemu_mutex_unlock(&qemu_global_mutex
);
3656 * Users of qemu_global_mutex can be starved, having no chance
3657 * to acquire it since this path will get to it first.
3658 * So use another lock to provide fairness.
3660 qemu_mutex_lock(&qemu_fair_mutex
);
3661 qemu_mutex_unlock(&qemu_fair_mutex
);
3663 qemu_mutex_lock(&qemu_global_mutex
);
3667 qemu_cond_signal(&qemu_pause_cond
);
3671 static int qemu_cpu_exec(CPUState
*env
);
3673 static void *kvm_cpu_thread_fn(void *arg
)
3675 CPUState
*env
= arg
;
3678 qemu_thread_self(env
->thread
);
3680 /* signal CPU creation */
3681 qemu_mutex_lock(&qemu_global_mutex
);
3683 qemu_cond_signal(&qemu_cpu_cond
);
3685 /* and wait for machine initialization */
3686 while (!qemu_system_ready
)
3687 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3690 if (cpu_can_run(env
))
3692 qemu_wait_io_event(env
);
3698 static void tcg_cpu_exec(void);
3700 static void *tcg_cpu_thread_fn(void *arg
)
3702 CPUState
*env
= arg
;
3705 qemu_thread_self(env
->thread
);
3707 /* signal CPU creation */
3708 qemu_mutex_lock(&qemu_global_mutex
);
3709 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3711 qemu_cond_signal(&qemu_cpu_cond
);
3713 /* and wait for machine initialization */
3714 while (!qemu_system_ready
)
3715 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3719 qemu_wait_io_event(cur_cpu
);
3725 void qemu_cpu_kick(void *_env
)
3727 CPUState
*env
= _env
;
3728 qemu_cond_broadcast(env
->halt_cond
);
3730 qemu_thread_signal(env
->thread
, SIGUSR1
);
3733 int qemu_cpu_self(void *env
)
3735 return (cpu_single_env
!= NULL
);
3738 static void cpu_signal(int sig
)
3741 cpu_exit(cpu_single_env
);
3744 static void block_io_signals(void)
3747 struct sigaction sigact
;
3750 sigaddset(&set
, SIGUSR2
);
3751 sigaddset(&set
, SIGIO
);
3752 sigaddset(&set
, SIGALRM
);
3753 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3756 sigaddset(&set
, SIGUSR1
);
3757 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3759 memset(&sigact
, 0, sizeof(sigact
));
3760 sigact
.sa_handler
= cpu_signal
;
3761 sigaction(SIGUSR1
, &sigact
, NULL
);
3764 static void unblock_io_signals(void)
3769 sigaddset(&set
, SIGUSR2
);
3770 sigaddset(&set
, SIGIO
);
3771 sigaddset(&set
, SIGALRM
);
3772 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3775 sigaddset(&set
, SIGUSR1
);
3776 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3779 static void qemu_signal_lock(unsigned int msecs
)
3781 qemu_mutex_lock(&qemu_fair_mutex
);
3783 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3784 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3785 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3788 qemu_mutex_unlock(&qemu_fair_mutex
);
3791 static void qemu_mutex_lock_iothread(void)
3793 if (kvm_enabled()) {
3794 qemu_mutex_lock(&qemu_fair_mutex
);
3795 qemu_mutex_lock(&qemu_global_mutex
);
3796 qemu_mutex_unlock(&qemu_fair_mutex
);
3798 qemu_signal_lock(100);
3801 static void qemu_mutex_unlock_iothread(void)
3803 qemu_mutex_unlock(&qemu_global_mutex
);
3806 static int all_vcpus_paused(void)
3808 CPUState
*penv
= first_cpu
;
3813 penv
= (CPUState
*)penv
->next_cpu
;
3819 static void pause_all_vcpus(void)
3821 CPUState
*penv
= first_cpu
;
3825 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3826 qemu_cpu_kick(penv
);
3827 penv
= (CPUState
*)penv
->next_cpu
;
3830 while (!all_vcpus_paused()) {
3831 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3834 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3835 penv
= (CPUState
*)penv
->next_cpu
;
3840 static void resume_all_vcpus(void)
3842 CPUState
*penv
= first_cpu
;
3847 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3848 qemu_cpu_kick(penv
);
3849 penv
= (CPUState
*)penv
->next_cpu
;
3853 static void tcg_init_vcpu(void *_env
)
3855 CPUState
*env
= _env
;
3856 /* share a single thread for all cpus with TCG */
3857 if (!tcg_cpu_thread
) {
3858 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3859 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3860 qemu_cond_init(env
->halt_cond
);
3861 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3862 while (env
->created
== 0)
3863 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3864 tcg_cpu_thread
= env
->thread
;
3865 tcg_halt_cond
= env
->halt_cond
;
3867 env
->thread
= tcg_cpu_thread
;
3868 env
->halt_cond
= tcg_halt_cond
;
3872 static void kvm_start_vcpu(CPUState
*env
)
3875 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3876 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3877 qemu_cond_init(env
->halt_cond
);
3878 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3879 while (env
->created
== 0)
3880 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3883 void qemu_init_vcpu(void *_env
)
3885 CPUState
*env
= _env
;
3888 kvm_start_vcpu(env
);
3893 void qemu_notify_event(void)
3895 qemu_event_increment();
3898 void vm_stop(int reason
)
3901 qemu_thread_self(&me
);
3903 if (!qemu_thread_equal(&me
, &io_thread
)) {
3904 qemu_system_vmstop_request(reason
);
3906 * FIXME: should not return to device code in case
3907 * vm_stop() has been requested.
3909 if (cpu_single_env
) {
3910 cpu_exit(cpu_single_env
);
3911 cpu_single_env
->stop
= 1;
3922 static void host_main_loop_wait(int *timeout
)
3928 /* XXX: need to suppress polling by better using win32 events */
3930 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3931 ret
|= pe
->func(pe
->opaque
);
3935 WaitObjects
*w
= &wait_objects
;
3937 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3938 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3939 if (w
->func
[ret
- WAIT_OBJECT_0
])
3940 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3942 /* Check for additional signaled events */
3943 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3945 /* Check if event is signaled */
3946 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3947 if(ret2
== WAIT_OBJECT_0
) {
3949 w
->func
[i
](w
->opaque
[i
]);
3950 } else if (ret2
== WAIT_TIMEOUT
) {
3952 err
= GetLastError();
3953 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3956 } else if (ret
== WAIT_TIMEOUT
) {
3958 err
= GetLastError();
3959 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3966 static void host_main_loop_wait(int *timeout
)
3971 void main_loop_wait(int timeout
)
3973 IOHandlerRecord
*ioh
;
3974 fd_set rfds
, wfds
, xfds
;
3978 qemu_bh_update_timeout(&timeout
);
3980 host_main_loop_wait(&timeout
);
3982 /* poll any events */
3983 /* XXX: separate device handlers from system ones */
3988 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3992 (!ioh
->fd_read_poll
||
3993 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3994 FD_SET(ioh
->fd
, &rfds
);
3998 if (ioh
->fd_write
) {
3999 FD_SET(ioh
->fd
, &wfds
);
4005 tv
.tv_sec
= timeout
/ 1000;
4006 tv
.tv_usec
= (timeout
% 1000) * 1000;
4008 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4010 qemu_mutex_unlock_iothread();
4011 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4012 qemu_mutex_lock_iothread();
4014 IOHandlerRecord
**pioh
;
4016 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4017 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4018 ioh
->fd_read(ioh
->opaque
);
4020 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4021 ioh
->fd_write(ioh
->opaque
);
4025 /* remove deleted IO handlers */
4026 pioh
= &first_io_handler
;
4037 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4039 /* rearm timer, if not periodic */
4040 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4041 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4042 qemu_rearm_alarm_timer(alarm_timer
);
4045 /* vm time timers */
4047 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4048 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4049 qemu_get_clock(vm_clock
));
4052 /* real time timers */
4053 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4054 qemu_get_clock(rt_clock
));
4056 /* Check bottom-halves last in case any of the earlier events triggered
4062 static int qemu_cpu_exec(CPUState
*env
)
4065 #ifdef CONFIG_PROFILER
4069 #ifdef CONFIG_PROFILER
4070 ti
= profile_getclock();
4075 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4076 env
->icount_decr
.u16
.low
= 0;
4077 env
->icount_extra
= 0;
4078 count
= qemu_next_deadline();
4079 count
= (count
+ (1 << icount_time_shift
) - 1)
4080 >> icount_time_shift
;
4081 qemu_icount
+= count
;
4082 decr
= (count
> 0xffff) ? 0xffff : count
;
4084 env
->icount_decr
.u16
.low
= decr
;
4085 env
->icount_extra
= count
;
4087 ret
= cpu_exec(env
);
4088 #ifdef CONFIG_PROFILER
4089 qemu_time
+= profile_getclock() - ti
;
4092 /* Fold pending instructions back into the
4093 instruction counter, and clear the interrupt flag. */
4094 qemu_icount
-= (env
->icount_decr
.u16
.low
4095 + env
->icount_extra
);
4096 env
->icount_decr
.u32
= 0;
4097 env
->icount_extra
= 0;
4102 static void tcg_cpu_exec(void)
4106 if (next_cpu
== NULL
)
4107 next_cpu
= first_cpu
;
4108 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4109 CPUState
*env
= cur_cpu
= next_cpu
;
4113 if (timer_alarm_pending
) {
4114 timer_alarm_pending
= 0;
4117 if (cpu_can_run(env
))
4118 ret
= qemu_cpu_exec(env
);
4119 if (ret
== EXCP_DEBUG
) {
4120 gdb_set_stop_cpu(env
);
4121 debug_requested
= 1;
4127 static int cpu_has_work(CPUState
*env
)
4135 if (qemu_cpu_has_work(env
))
4140 static int tcg_has_work(void)
4144 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4145 if (cpu_has_work(env
))
4150 static int qemu_calculate_timeout(void)
4152 #ifndef CONFIG_IOTHREAD
4157 else if (tcg_has_work())
4159 else if (!use_icount
)
4162 /* XXX: use timeout computed from timers */
4165 /* Advance virtual time to the next event. */
4166 if (use_icount
== 1) {
4167 /* When not using an adaptive execution frequency
4168 we tend to get badly out of sync with real time,
4169 so just delay for a reasonable amount of time. */
4172 delta
= cpu_get_icount() - cpu_get_clock();
4175 /* If virtual time is ahead of real time then just
4177 timeout
= (delta
/ 1000000) + 1;
4179 /* Wait for either IO to occur or the next
4181 add
= qemu_next_deadline();
4182 /* We advance the timer before checking for IO.
4183 Limit the amount we advance so that early IO
4184 activity won't get the guest too far ahead. */
4188 add
= (add
+ (1 << icount_time_shift
) - 1)
4189 >> icount_time_shift
;
4191 timeout
= delta
/ 1000000;
4198 #else /* CONFIG_IOTHREAD */
4203 static int vm_can_run(void)
4205 if (powerdown_requested
)
4207 if (reset_requested
)
4209 if (shutdown_requested
)
4211 if (debug_requested
)
4216 qemu_irq qemu_system_powerdown
;
4218 static void main_loop(void)
4222 #ifdef CONFIG_IOTHREAD
4223 qemu_system_ready
= 1;
4224 qemu_cond_broadcast(&qemu_system_cond
);
4229 #ifdef CONFIG_PROFILER
4232 #ifndef CONFIG_IOTHREAD
4235 #ifdef CONFIG_PROFILER
4236 ti
= profile_getclock();
4238 main_loop_wait(qemu_calculate_timeout());
4239 #ifdef CONFIG_PROFILER
4240 dev_time
+= profile_getclock() - ti
;
4242 } while (vm_can_run());
4244 if (qemu_debug_requested())
4245 vm_stop(EXCP_DEBUG
);
4246 if (qemu_shutdown_requested()) {
4253 if (qemu_reset_requested()) {
4255 qemu_system_reset();
4258 if (qemu_powerdown_requested()) {
4259 qemu_irq_raise(qemu_system_powerdown
);
4261 if ((r
= qemu_vmstop_requested()))
4267 static void version(void)
4269 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4272 static void help(int exitcode
)
4275 printf("usage: %s [options] [disk_image]\n"
4277 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4279 #define DEF(option, opt_arg, opt_enum, opt_help) \
4281 #define DEFHEADING(text) stringify(text) "\n"
4282 #include "qemu-options.h"
4287 "During emulation, the following keys are useful:\n"
4288 "ctrl-alt-f toggle full screen\n"
4289 "ctrl-alt-n switch to virtual console 'n'\n"
4290 "ctrl-alt toggle mouse and keyboard grab\n"
4292 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4297 DEFAULT_NETWORK_SCRIPT
,
4298 DEFAULT_NETWORK_DOWN_SCRIPT
,
4300 DEFAULT_GDBSTUB_PORT
,
4305 #define HAS_ARG 0x0001
4308 #define DEF(option, opt_arg, opt_enum, opt_help) \
4310 #define DEFHEADING(text)
4311 #include "qemu-options.h"
4317 typedef struct QEMUOption
{
4323 static const QEMUOption qemu_options
[] = {
4324 { "h", 0, QEMU_OPTION_h
},
4325 #define DEF(option, opt_arg, opt_enum, opt_help) \
4326 { option, opt_arg, opt_enum },
4327 #define DEFHEADING(text)
4328 #include "qemu-options.h"
4336 struct soundhw soundhw
[] = {
4337 #ifdef HAS_AUDIO_CHOICE
4338 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4344 { .init_isa
= pcspk_audio_init
}
4351 "Creative Sound Blaster 16",
4354 { .init_isa
= SB16_init
}
4358 #ifdef CONFIG_CS4231A
4364 { .init_isa
= cs4231a_init
}
4372 "Yamaha YMF262 (OPL3)",
4374 "Yamaha YM3812 (OPL2)",
4378 { .init_isa
= Adlib_init
}
4385 "Gravis Ultrasound GF1",
4388 { .init_isa
= GUS_init
}
4395 "Intel 82801AA AC97 Audio",
4398 { .init_pci
= ac97_init
}
4402 #ifdef CONFIG_ES1370
4405 "ENSONIQ AudioPCI ES1370",
4408 { .init_pci
= es1370_init
}
4412 #endif /* HAS_AUDIO_CHOICE */
4414 { NULL
, NULL
, 0, 0, { NULL
} }
4417 static void select_soundhw (const char *optarg
)
4421 if (*optarg
== '?') {
4424 printf ("Valid sound card names (comma separated):\n");
4425 for (c
= soundhw
; c
->name
; ++c
) {
4426 printf ("%-11s %s\n", c
->name
, c
->descr
);
4428 printf ("\n-soundhw all will enable all of the above\n");
4429 exit (*optarg
!= '?');
4437 if (!strcmp (optarg
, "all")) {
4438 for (c
= soundhw
; c
->name
; ++c
) {
4446 e
= strchr (p
, ',');
4447 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4449 for (c
= soundhw
; c
->name
; ++c
) {
4450 if (!strncmp (c
->name
, p
, l
)) {
4459 "Unknown sound card name (too big to show)\n");
4462 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4467 p
+= l
+ (e
!= NULL
);
4471 goto show_valid_cards
;
4476 static void select_vgahw (const char *p
)
4480 vga_interface_type
= VGA_NONE
;
4481 if (strstart(p
, "std", &opts
)) {
4482 vga_interface_type
= VGA_STD
;
4483 } else if (strstart(p
, "cirrus", &opts
)) {
4484 vga_interface_type
= VGA_CIRRUS
;
4485 } else if (strstart(p
, "vmware", &opts
)) {
4486 vga_interface_type
= VGA_VMWARE
;
4487 } else if (strstart(p
, "xenfb", &opts
)) {
4488 vga_interface_type
= VGA_XENFB
;
4489 } else if (!strstart(p
, "none", &opts
)) {
4491 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4495 const char *nextopt
;
4497 if (strstart(opts
, ",retrace=", &nextopt
)) {
4499 if (strstart(opts
, "dumb", &nextopt
))
4500 vga_retrace_method
= VGA_RETRACE_DUMB
;
4501 else if (strstart(opts
, "precise", &nextopt
))
4502 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4503 else goto invalid_vga
;
4504 } else goto invalid_vga
;
4510 static int balloon_parse(const char *arg
)
4515 if (!strcmp(arg
, "none")) {
4517 } else if (!strncmp(arg
, "virtio", 6)) {
4519 if (arg
[6] == ',') {
4521 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4522 virtio_balloon_devaddr
= strdup(buf
);
4533 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4535 exit(STATUS_CONTROL_C_EXIT
);
4540 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4544 if(strlen(str
) != 36)
4547 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4548 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4549 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4555 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4561 #define MAX_NET_CLIENTS 32
4565 static void termsig_handler(int signal
)
4567 qemu_system_shutdown_request();
4570 static void sigchld_handler(int signal
)
4572 waitpid(-1, NULL
, WNOHANG
);
4575 static void sighandler_setup(void)
4577 struct sigaction act
;
4579 memset(&act
, 0, sizeof(act
));
4580 act
.sa_handler
= termsig_handler
;
4581 sigaction(SIGINT
, &act
, NULL
);
4582 sigaction(SIGHUP
, &act
, NULL
);
4583 sigaction(SIGTERM
, &act
, NULL
);
4585 act
.sa_handler
= sigchld_handler
;
4586 act
.sa_flags
= SA_NOCLDSTOP
;
4587 sigaction(SIGCHLD
, &act
, NULL
);
4593 /* Look for support files in the same directory as the executable. */
4594 static char *find_datadir(const char *argv0
)
4600 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4607 while (p
!= buf
&& *p
!= '\\')
4610 if (access(buf
, R_OK
) == 0) {
4611 return qemu_strdup(buf
);
4617 /* Find a likely location for support files using the location of the binary.
4618 For installed binaries this will be "$bindir/../share/qemu". When
4619 running from the build tree this will be "$bindir/../pc-bios". */
4620 #define SHARE_SUFFIX "/share/qemu"
4621 #define BUILD_SUFFIX "/pc-bios"
4622 static char *find_datadir(const char *argv0
)
4632 #if defined(__linux__)
4635 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4641 #elif defined(__FreeBSD__)
4644 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4651 /* If we don't have any way of figuring out the actual executable
4652 location then try argv[0]. */
4657 p
= realpath(argv0
, p
);
4665 max_len
= strlen(dir
) +
4666 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4667 res
= qemu_mallocz(max_len
);
4668 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4669 if (access(res
, R_OK
)) {
4670 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4671 if (access(res
, R_OK
)) {
4685 char *qemu_find_file(int type
, const char *name
)
4691 /* If name contains path separators then try it as a straight path. */
4692 if ((strchr(name
, '/') || strchr(name
, '\\'))
4693 && access(name
, R_OK
) == 0) {
4694 return strdup(name
);
4697 case QEMU_FILE_TYPE_BIOS
:
4700 case QEMU_FILE_TYPE_KEYMAP
:
4701 subdir
= "keymaps/";
4706 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4707 buf
= qemu_mallocz(len
);
4708 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4709 if (access(buf
, R_OK
)) {
4716 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4720 dev
= qdev_device_add(opts
);
4726 struct device_config
{
4728 DEV_USB
, /* -usbdevice */
4731 const char *cmdline
;
4732 TAILQ_ENTRY(device_config
) next
;
4734 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4736 static void add_device_config(int type
, const char *cmdline
)
4738 struct device_config
*conf
;
4740 conf
= qemu_mallocz(sizeof(*conf
));
4742 conf
->cmdline
= cmdline
;
4743 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4746 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4748 struct device_config
*conf
;
4751 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4752 if (conf
->type
!= type
)
4754 rc
= func(conf
->cmdline
);
4761 int main(int argc
, char **argv
, char **envp
)
4763 const char *gdbstub_dev
= NULL
;
4764 uint32_t boot_devices_bitmap
= 0;
4766 int snapshot
, linux_boot
, net_boot
;
4767 const char *initrd_filename
;
4768 const char *kernel_filename
, *kernel_cmdline
;
4769 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4771 DisplayChangeListener
*dcl
;
4772 int cyls
, heads
, secs
, translation
;
4773 const char *net_clients
[MAX_NET_CLIENTS
];
4775 QemuOpts
*hda_opts
= NULL
, *opts
;
4777 const char *r
, *optarg
;
4778 CharDriverState
*monitor_hd
= NULL
;
4779 const char *monitor_device
;
4780 const char *serial_devices
[MAX_SERIAL_PORTS
];
4781 int serial_device_index
;
4782 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4783 int parallel_device_index
;
4784 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4785 int virtio_console_index
;
4786 const char *loadvm
= NULL
;
4787 QEMUMachine
*machine
;
4788 const char *cpu_model
;
4793 const char *pid_file
= NULL
;
4794 const char *incoming
= NULL
;
4797 struct passwd
*pwd
= NULL
;
4798 const char *chroot_dir
= NULL
;
4799 const char *run_as
= NULL
;
4802 int show_vnc_port
= 0;
4804 qemu_cache_utils_init(envp
);
4806 LIST_INIT (&vm_change_state_head
);
4809 struct sigaction act
;
4810 sigfillset(&act
.sa_mask
);
4812 act
.sa_handler
= SIG_IGN
;
4813 sigaction(SIGPIPE
, &act
, NULL
);
4816 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4817 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4818 QEMU to run on a single CPU */
4823 h
= GetCurrentProcess();
4824 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4825 for(i
= 0; i
< 32; i
++) {
4826 if (mask
& (1 << i
))
4831 SetProcessAffinityMask(h
, mask
);
4837 module_call_init(MODULE_INIT_MACHINE
);
4838 machine
= find_default_machine();
4840 initrd_filename
= NULL
;
4843 kernel_filename
= NULL
;
4844 kernel_cmdline
= "";
4845 cyls
= heads
= secs
= 0;
4846 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4847 monitor_device
= "vc:80Cx24C";
4849 serial_devices
[0] = "vc:80Cx24C";
4850 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4851 serial_devices
[i
] = NULL
;
4852 serial_device_index
= 0;
4854 parallel_devices
[0] = "vc:80Cx24C";
4855 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4856 parallel_devices
[i
] = NULL
;
4857 parallel_device_index
= 0;
4859 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4860 virtio_consoles
[i
] = NULL
;
4861 virtio_console_index
= 0;
4863 for (i
= 0; i
< MAX_NODES
; i
++) {
4865 node_cpumask
[i
] = 0;
4875 register_watchdogs();
4883 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4885 const QEMUOption
*popt
;
4888 /* Treat --foo the same as -foo. */
4891 popt
= qemu_options
;
4894 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4898 if (!strcmp(popt
->name
, r
+ 1))
4902 if (popt
->flags
& HAS_ARG
) {
4903 if (optind
>= argc
) {
4904 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4908 optarg
= argv
[optind
++];
4913 switch(popt
->index
) {
4915 machine
= find_machine(optarg
);
4918 printf("Supported machines are:\n");
4919 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4921 printf("%-10s %s (alias of %s)\n",
4922 m
->alias
, m
->desc
, m
->name
);
4923 printf("%-10s %s%s\n",
4925 m
->is_default
? " (default)" : "");
4927 exit(*optarg
!= '?');
4930 case QEMU_OPTION_cpu
:
4931 /* hw initialization will check this */
4932 if (*optarg
== '?') {
4933 /* XXX: implement xxx_cpu_list for targets that still miss it */
4934 #if defined(cpu_list)
4935 cpu_list(stdout
, &fprintf
);
4942 case QEMU_OPTION_initrd
:
4943 initrd_filename
= optarg
;
4945 case QEMU_OPTION_hda
:
4947 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4949 hda_opts
= drive_add(optarg
, HD_ALIAS
4950 ",cyls=%d,heads=%d,secs=%d%s",
4951 0, cyls
, heads
, secs
,
4952 translation
== BIOS_ATA_TRANSLATION_LBA
?
4954 translation
== BIOS_ATA_TRANSLATION_NONE
?
4955 ",trans=none" : "");
4957 case QEMU_OPTION_hdb
:
4958 case QEMU_OPTION_hdc
:
4959 case QEMU_OPTION_hdd
:
4960 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4962 case QEMU_OPTION_drive
:
4963 drive_add(NULL
, "%s", optarg
);
4965 case QEMU_OPTION_set
:
4966 if (qemu_set_option(optarg
) != 0)
4969 case QEMU_OPTION_mtdblock
:
4970 drive_add(optarg
, MTD_ALIAS
);
4972 case QEMU_OPTION_sd
:
4973 drive_add(optarg
, SD_ALIAS
);
4975 case QEMU_OPTION_pflash
:
4976 drive_add(optarg
, PFLASH_ALIAS
);
4978 case QEMU_OPTION_snapshot
:
4981 case QEMU_OPTION_hdachs
:
4985 cyls
= strtol(p
, (char **)&p
, 0);
4986 if (cyls
< 1 || cyls
> 16383)
4991 heads
= strtol(p
, (char **)&p
, 0);
4992 if (heads
< 1 || heads
> 16)
4997 secs
= strtol(p
, (char **)&p
, 0);
4998 if (secs
< 1 || secs
> 63)
5002 if (!strcmp(p
, "none"))
5003 translation
= BIOS_ATA_TRANSLATION_NONE
;
5004 else if (!strcmp(p
, "lba"))
5005 translation
= BIOS_ATA_TRANSLATION_LBA
;
5006 else if (!strcmp(p
, "auto"))
5007 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5010 } else if (*p
!= '\0') {
5012 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5015 if (hda_opts
!= NULL
) {
5017 snprintf(num
, sizeof(num
), "%d", cyls
);
5018 qemu_opt_set(hda_opts
, "cyls", num
);
5019 snprintf(num
, sizeof(num
), "%d", heads
);
5020 qemu_opt_set(hda_opts
, "heads", num
);
5021 snprintf(num
, sizeof(num
), "%d", secs
);
5022 qemu_opt_set(hda_opts
, "secs", num
);
5023 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5024 qemu_opt_set(hda_opts
, "trans", "lba");
5025 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5026 qemu_opt_set(hda_opts
, "trans", "none");
5030 case QEMU_OPTION_numa
:
5031 if (nb_numa_nodes
>= MAX_NODES
) {
5032 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5037 case QEMU_OPTION_nographic
:
5038 display_type
= DT_NOGRAPHIC
;
5040 #ifdef CONFIG_CURSES
5041 case QEMU_OPTION_curses
:
5042 display_type
= DT_CURSES
;
5045 case QEMU_OPTION_portrait
:
5048 case QEMU_OPTION_kernel
:
5049 kernel_filename
= optarg
;
5051 case QEMU_OPTION_append
:
5052 kernel_cmdline
= optarg
;
5054 case QEMU_OPTION_cdrom
:
5055 drive_add(optarg
, CDROM_ALIAS
);
5057 case QEMU_OPTION_boot
:
5059 static const char * const params
[] = {
5060 "order", "once", "menu", NULL
5062 char buf
[sizeof(boot_devices
)];
5063 char *standard_boot_devices
;
5066 if (!strchr(optarg
, '=')) {
5068 pstrcpy(buf
, sizeof(buf
), optarg
);
5069 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5071 "qemu: unknown boot parameter '%s' in '%s'\n",
5077 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5078 boot_devices_bitmap
= parse_bootdevices(buf
);
5079 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5082 if (get_param_value(buf
, sizeof(buf
),
5084 boot_devices_bitmap
|= parse_bootdevices(buf
);
5085 standard_boot_devices
= qemu_strdup(boot_devices
);
5086 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5087 qemu_register_reset(restore_boot_devices
,
5088 standard_boot_devices
);
5090 if (get_param_value(buf
, sizeof(buf
),
5092 if (!strcmp(buf
, "on")) {
5094 } else if (!strcmp(buf
, "off")) {
5098 "qemu: invalid option value '%s'\n",
5106 case QEMU_OPTION_fda
:
5107 case QEMU_OPTION_fdb
:
5108 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5111 case QEMU_OPTION_no_fd_bootchk
:
5115 case QEMU_OPTION_net
:
5116 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5117 fprintf(stderr
, "qemu: too many network clients\n");
5120 net_clients
[nb_net_clients
] = optarg
;
5124 case QEMU_OPTION_tftp
:
5125 legacy_tftp_prefix
= optarg
;
5127 case QEMU_OPTION_bootp
:
5128 legacy_bootp_filename
= optarg
;
5131 case QEMU_OPTION_smb
:
5132 net_slirp_smb(optarg
);
5135 case QEMU_OPTION_redir
:
5136 net_slirp_redir(optarg
);
5139 case QEMU_OPTION_bt
:
5140 add_device_config(DEV_BT
, optarg
);
5143 case QEMU_OPTION_audio_help
:
5147 case QEMU_OPTION_soundhw
:
5148 select_soundhw (optarg
);
5154 case QEMU_OPTION_version
:
5158 case QEMU_OPTION_m
: {
5162 value
= strtoul(optarg
, &ptr
, 10);
5164 case 0: case 'M': case 'm':
5171 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5175 /* On 32-bit hosts, QEMU is limited by virtual address space */
5176 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5177 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5180 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5181 fprintf(stderr
, "qemu: ram size too large\n");
5190 const CPULogItem
*item
;
5192 mask
= cpu_str_to_log_mask(optarg
);
5194 printf("Log items (comma separated):\n");
5195 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5196 printf("%-10s %s\n", item
->name
, item
->help
);
5204 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5206 case QEMU_OPTION_gdb
:
5207 gdbstub_dev
= optarg
;
5212 case QEMU_OPTION_bios
:
5215 case QEMU_OPTION_singlestep
:
5223 keyboard_layout
= optarg
;
5226 case QEMU_OPTION_localtime
:
5229 case QEMU_OPTION_vga
:
5230 select_vgahw (optarg
);
5232 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5238 w
= strtol(p
, (char **)&p
, 10);
5241 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5247 h
= strtol(p
, (char **)&p
, 10);
5252 depth
= strtol(p
, (char **)&p
, 10);
5253 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5254 depth
!= 24 && depth
!= 32)
5256 } else if (*p
== '\0') {
5257 depth
= graphic_depth
;
5264 graphic_depth
= depth
;
5268 case QEMU_OPTION_echr
:
5271 term_escape_char
= strtol(optarg
, &r
, 0);
5273 printf("Bad argument to echr\n");
5276 case QEMU_OPTION_monitor
:
5277 monitor_device
= optarg
;
5279 case QEMU_OPTION_serial
:
5280 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5281 fprintf(stderr
, "qemu: too many serial ports\n");
5284 serial_devices
[serial_device_index
] = optarg
;
5285 serial_device_index
++;
5287 case QEMU_OPTION_watchdog
:
5288 i
= select_watchdog(optarg
);
5290 exit (i
== 1 ? 1 : 0);
5292 case QEMU_OPTION_watchdog_action
:
5293 if (select_watchdog_action(optarg
) == -1) {
5294 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5298 case QEMU_OPTION_virtiocon
:
5299 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5300 fprintf(stderr
, "qemu: too many virtio consoles\n");
5303 virtio_consoles
[virtio_console_index
] = optarg
;
5304 virtio_console_index
++;
5306 case QEMU_OPTION_parallel
:
5307 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5308 fprintf(stderr
, "qemu: too many parallel ports\n");
5311 parallel_devices
[parallel_device_index
] = optarg
;
5312 parallel_device_index
++;
5314 case QEMU_OPTION_loadvm
:
5317 case QEMU_OPTION_full_screen
:
5321 case QEMU_OPTION_no_frame
:
5324 case QEMU_OPTION_alt_grab
:
5327 case QEMU_OPTION_no_quit
:
5330 case QEMU_OPTION_sdl
:
5331 display_type
= DT_SDL
;
5334 case QEMU_OPTION_pidfile
:
5338 case QEMU_OPTION_win2k_hack
:
5339 win2k_install_hack
= 1;
5341 case QEMU_OPTION_rtc_td_hack
:
5344 case QEMU_OPTION_acpitable
:
5345 if(acpi_table_add(optarg
) < 0) {
5346 fprintf(stderr
, "Wrong acpi table provided\n");
5350 case QEMU_OPTION_smbios
:
5351 if(smbios_entry_add(optarg
) < 0) {
5352 fprintf(stderr
, "Wrong smbios provided\n");
5358 case QEMU_OPTION_enable_kvm
:
5362 case QEMU_OPTION_usb
:
5365 case QEMU_OPTION_usbdevice
:
5367 add_device_config(DEV_USB
, optarg
);
5369 case QEMU_OPTION_device
:
5370 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5372 fprintf(stderr
, "parse error: %s\n", optarg
);
5376 case QEMU_OPTION_smp
:
5380 smp_cpus
= strtol(optarg
, &p
, 10);
5382 fprintf(stderr
, "Invalid number of CPUs\n");
5387 if (get_param_value(option
, 128, "maxcpus", p
))
5388 max_cpus
= strtol(option
, NULL
, 0);
5389 if (max_cpus
< smp_cpus
) {
5390 fprintf(stderr
, "maxcpus must be equal to or greater than "
5394 if (max_cpus
> 255) {
5395 fprintf(stderr
, "Unsupported number of maxcpus\n");
5400 case QEMU_OPTION_vnc
:
5401 display_type
= DT_VNC
;
5402 vnc_display
= optarg
;
5405 case QEMU_OPTION_no_acpi
:
5408 case QEMU_OPTION_no_hpet
:
5411 case QEMU_OPTION_balloon
:
5412 if (balloon_parse(optarg
) < 0) {
5413 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5418 case QEMU_OPTION_no_reboot
:
5421 case QEMU_OPTION_no_shutdown
:
5424 case QEMU_OPTION_show_cursor
:
5427 case QEMU_OPTION_uuid
:
5428 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5429 fprintf(stderr
, "Fail to parse UUID string."
5430 " Wrong format.\n");
5435 case QEMU_OPTION_daemonize
:
5439 case QEMU_OPTION_option_rom
:
5440 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5441 fprintf(stderr
, "Too many option ROMs\n");
5444 option_rom
[nb_option_roms
] = optarg
;
5447 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5448 case QEMU_OPTION_semihosting
:
5449 semihosting_enabled
= 1;
5452 case QEMU_OPTION_name
:
5453 qemu_name
= qemu_strdup(optarg
);
5455 char *p
= strchr(qemu_name
, ',');
5458 if (strncmp(p
, "process=", 8)) {
5459 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5467 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5468 case QEMU_OPTION_prom_env
:
5469 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5470 fprintf(stderr
, "Too many prom variables\n");
5473 prom_envs
[nb_prom_envs
] = optarg
;
5478 case QEMU_OPTION_old_param
:
5482 case QEMU_OPTION_clock
:
5483 configure_alarms(optarg
);
5485 case QEMU_OPTION_startdate
:
5488 time_t rtc_start_date
;
5489 if (!strcmp(optarg
, "now")) {
5490 rtc_date_offset
= -1;
5492 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5500 } else if (sscanf(optarg
, "%d-%d-%d",
5503 &tm
.tm_mday
) == 3) {
5512 rtc_start_date
= mktimegm(&tm
);
5513 if (rtc_start_date
== -1) {
5515 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5516 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5519 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5523 case QEMU_OPTION_tb_size
:
5524 tb_size
= strtol(optarg
, NULL
, 0);
5528 case QEMU_OPTION_icount
:
5530 if (strcmp(optarg
, "auto") == 0) {
5531 icount_time_shift
= -1;
5533 icount_time_shift
= strtol(optarg
, NULL
, 0);
5536 case QEMU_OPTION_incoming
:
5540 case QEMU_OPTION_chroot
:
5541 chroot_dir
= optarg
;
5543 case QEMU_OPTION_runas
:
5548 case QEMU_OPTION_xen_domid
:
5549 xen_domid
= atoi(optarg
);
5551 case QEMU_OPTION_xen_create
:
5552 xen_mode
= XEN_CREATE
;
5554 case QEMU_OPTION_xen_attach
:
5555 xen_mode
= XEN_ATTACH
;
5562 /* If no data_dir is specified then try to find it relative to the
5565 data_dir
= find_datadir(argv
[0]);
5567 /* If all else fails use the install patch specified when building. */
5569 data_dir
= CONFIG_QEMU_SHAREDIR
;
5573 * Default to max_cpus = smp_cpus, in case the user doesn't
5574 * specify a max_cpus value.
5577 max_cpus
= smp_cpus
;
5579 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5580 if (smp_cpus
> machine
->max_cpus
) {
5581 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5582 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5587 if (display_type
== DT_NOGRAPHIC
) {
5588 if (serial_device_index
== 0)
5589 serial_devices
[0] = "stdio";
5590 if (parallel_device_index
== 0)
5591 parallel_devices
[0] = "null";
5592 if (strncmp(monitor_device
, "vc", 2) == 0)
5593 monitor_device
= "stdio";
5600 if (pipe(fds
) == -1)
5611 len
= read(fds
[0], &status
, 1);
5612 if (len
== -1 && (errno
== EINTR
))
5617 else if (status
== 1) {
5618 fprintf(stderr
, "Could not acquire pidfile\n");
5635 signal(SIGTSTP
, SIG_IGN
);
5636 signal(SIGTTOU
, SIG_IGN
);
5637 signal(SIGTTIN
, SIG_IGN
);
5640 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5643 write(fds
[1], &status
, 1);
5645 fprintf(stderr
, "Could not acquire pid file\n");
5650 if (qemu_init_main_loop()) {
5651 fprintf(stderr
, "qemu_init_main_loop failed\n");
5654 linux_boot
= (kernel_filename
!= NULL
);
5656 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5657 fprintf(stderr
, "-append only allowed with -kernel option\n");
5661 if (!linux_boot
&& initrd_filename
!= NULL
) {
5662 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5667 /* Win32 doesn't support line-buffering and requires size >= 2 */
5668 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5672 if (init_timer_alarm() < 0) {
5673 fprintf(stderr
, "could not initialize alarm timer\n");
5676 if (use_icount
&& icount_time_shift
< 0) {
5678 /* 125MIPS seems a reasonable initial guess at the guest speed.
5679 It will be corrected fairly quickly anyway. */
5680 icount_time_shift
= 3;
5681 init_icount_adjust();
5688 /* init network clients */
5689 if (nb_net_clients
== 0) {
5690 /* if no clients, we use a default config */
5691 net_clients
[nb_net_clients
++] = "nic";
5693 net_clients
[nb_net_clients
++] = "user";
5697 for(i
= 0;i
< nb_net_clients
; i
++) {
5698 if (net_client_parse(net_clients
[i
]) < 0)
5702 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5703 net_set_boot_mask(net_boot
);
5707 /* init the bluetooth world */
5708 if (foreach_device_config(DEV_BT
, bt_parse
))
5711 /* init the memory */
5713 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5715 /* init the dynamic translator */
5716 cpu_exec_init_all(tb_size
* 1024 * 1024);
5720 /* we always create the cdrom drive, even if no disk is there */
5721 drive_add(NULL
, CDROM_ALIAS
);
5723 /* we always create at least one floppy */
5724 drive_add(NULL
, FD_ALIAS
, 0);
5726 /* we always create one sd slot, even if no card is in it */
5727 drive_add(NULL
, SD_ALIAS
);
5729 /* open the virtual block devices */
5731 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5732 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5735 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5736 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5739 /* must be after terminal init, SDL library changes signal handlers */
5743 /* Maintain compatibility with multiple stdio monitors */
5744 if (!strcmp(monitor_device
,"stdio")) {
5745 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5746 const char *devname
= serial_devices
[i
];
5747 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5748 monitor_device
= NULL
;
5750 } else if (devname
&& !strcmp(devname
,"stdio")) {
5751 monitor_device
= NULL
;
5752 serial_devices
[i
] = "mon:stdio";
5758 if (nb_numa_nodes
> 0) {
5761 if (nb_numa_nodes
> smp_cpus
) {
5762 nb_numa_nodes
= smp_cpus
;
5765 /* If no memory size if given for any node, assume the default case
5766 * and distribute the available memory equally across all nodes
5768 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5769 if (node_mem
[i
] != 0)
5772 if (i
== nb_numa_nodes
) {
5773 uint64_t usedmem
= 0;
5775 /* On Linux, the each node's border has to be 8MB aligned,
5776 * the final node gets the rest.
5778 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5779 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5780 usedmem
+= node_mem
[i
];
5782 node_mem
[i
] = ram_size
- usedmem
;
5785 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5786 if (node_cpumask
[i
] != 0)
5789 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5790 * must cope with this anyway, because there are BIOSes out there in
5791 * real machines which also use this scheme.
5793 if (i
== nb_numa_nodes
) {
5794 for (i
= 0; i
< smp_cpus
; i
++) {
5795 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5800 if (kvm_enabled()) {
5803 ret
= kvm_init(smp_cpus
);
5805 fprintf(stderr
, "failed to initialize KVM\n");
5810 if (monitor_device
) {
5811 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5813 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5818 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5819 const char *devname
= serial_devices
[i
];
5820 if (devname
&& strcmp(devname
, "none")) {
5822 snprintf(label
, sizeof(label
), "serial%d", i
);
5823 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5824 if (!serial_hds
[i
]) {
5825 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5832 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5833 const char *devname
= parallel_devices
[i
];
5834 if (devname
&& strcmp(devname
, "none")) {
5836 snprintf(label
, sizeof(label
), "parallel%d", i
);
5837 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5838 if (!parallel_hds
[i
]) {
5839 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5846 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5847 const char *devname
= virtio_consoles
[i
];
5848 if (devname
&& strcmp(devname
, "none")) {
5850 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5851 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5852 if (!virtcon_hds
[i
]) {
5853 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5860 module_call_init(MODULE_INIT_DEVICE
);
5862 if (machine
->compat_props
) {
5863 qdev_prop_register_compat(machine
->compat_props
);
5865 machine
->init(ram_size
, boot_devices
,
5866 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5869 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5870 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5871 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5877 current_machine
= machine
;
5879 /* init USB devices */
5881 foreach_device_config(DEV_USB
, usb_parse
);
5884 /* init generic devices */
5885 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5889 dumb_display_init();
5890 /* just use the first displaystate for the moment */
5893 if (display_type
== DT_DEFAULT
) {
5894 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5895 display_type
= DT_SDL
;
5897 display_type
= DT_VNC
;
5898 vnc_display
= "localhost:0,to=99";
5904 switch (display_type
) {
5907 #if defined(CONFIG_CURSES)
5909 curses_display_init(ds
, full_screen
);
5912 #if defined(CONFIG_SDL)
5914 sdl_display_init(ds
, full_screen
, no_frame
);
5916 #elif defined(CONFIG_COCOA)
5918 cocoa_display_init(ds
, full_screen
);
5922 vnc_display_init(ds
);
5923 if (vnc_display_open(ds
, vnc_display
) < 0)
5926 if (show_vnc_port
) {
5927 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5935 dcl
= ds
->listeners
;
5936 while (dcl
!= NULL
) {
5937 if (dcl
->dpy_refresh
!= NULL
) {
5938 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5939 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5944 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5945 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5946 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5949 text_consoles_set_display(display_state
);
5950 qemu_chr_initial_reset();
5952 if (monitor_device
&& monitor_hd
)
5953 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5955 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5956 const char *devname
= serial_devices
[i
];
5957 if (devname
&& strcmp(devname
, "none")) {
5958 if (strstart(devname
, "vc", 0))
5959 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5963 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5964 const char *devname
= parallel_devices
[i
];
5965 if (devname
&& strcmp(devname
, "none")) {
5966 if (strstart(devname
, "vc", 0))
5967 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5971 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5972 const char *devname
= virtio_consoles
[i
];
5973 if (virtcon_hds
[i
] && devname
) {
5974 if (strstart(devname
, "vc", 0))
5975 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5979 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5980 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5986 do_loadvm(cur_mon
, loadvm
);
5989 qemu_start_incoming_migration(incoming
);
5990 } else if (autostart
) {
6000 len
= write(fds
[1], &status
, 1);
6001 if (len
== -1 && (errno
== EINTR
))
6008 TFR(fd
= open("/dev/null", O_RDWR
));
6014 pwd
= getpwnam(run_as
);
6016 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6022 if (chroot(chroot_dir
) < 0) {
6023 fprintf(stderr
, "chroot failed\n");
6030 if (setgid(pwd
->pw_gid
) < 0) {
6031 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6034 if (setuid(pwd
->pw_uid
) < 0) {
6035 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6038 if (setuid(0) != -1) {
6039 fprintf(stderr
, "Dropping privileges failed\n");