4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <linux/rtc.h>
63 #include <sys/prctl.h>
65 /* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67 /* #include <linux/hpet.h> */
70 #include <linux/ppdev.h>
71 #include <linux/parport.h>
75 #include <sys/ethernet.h>
76 #include <sys/sockio.h>
77 #include <netinet/arp.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> // must come after ip.h
82 #include <netinet/udp.h>
83 #include <netinet/tcp.h>
87 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89 extern int madvise(caddr_t
, size_t, int);
94 #if defined(__OpenBSD__)
98 #if defined(CONFIG_VDE)
99 #include <libvdeplug.h>
104 #include <mmsystem.h>
108 #if defined(__APPLE__) || defined(main)
110 int qemu_main(int argc
, char **argv
, char **envp
);
111 int main(int argc
, char **argv
)
113 return qemu_main(argc
, argv
, NULL
);
116 #define main qemu_main
118 #endif /* CONFIG_SDL */
122 #define main qemu_main
123 #endif /* CONFIG_COCOA */
126 #include "hw/boards.h"
128 #include "hw/pcmcia.h"
130 #include "hw/audiodev.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "block_int.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
157 #include "qemu-option.h"
158 #include "qemu-config.h"
162 #include "exec-all.h"
164 #include "qemu_socket.h"
166 #include "slirp/libslirp.h"
168 #include "qemu-queue.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 /* Maximum number of monitor devices */
176 #define MAX_MONITOR_DEVICES 10
178 static const char *data_dir
;
179 const char *bios_name
= NULL
;
180 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
181 to store the VM snapshots */
182 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
183 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
184 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
185 static DisplayState
*display_state
;
186 DisplayType display_type
= DT_DEFAULT
;
187 const char* keyboard_layout
= NULL
;
190 NICInfo nd_table
[MAX_NICS
];
193 static int rtc_utc
= 1;
194 static int rtc_date_offset
= -1; /* -1 means no change */
195 QEMUClock
*rtc_clock
;
196 int vga_interface_type
= VGA_CIRRUS
;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static int timer_alarm_pending
= 1;
259 /* Conversion factor from emulated instructions to virtual clock ticks. */
260 static int icount_time_shift
;
261 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
262 #define MAX_ICOUNT_SHIFT 10
263 /* Compensate for varying guest execution speed. */
264 static int64_t qemu_icount_bias
;
265 static QEMUTimer
*icount_rt_timer
;
266 static QEMUTimer
*icount_vm_timer
;
267 static QEMUTimer
*nographic_timer
;
269 uint8_t qemu_uuid
[16];
271 static QEMUBootSetHandler
*boot_set_handler
;
272 static void *boot_set_opaque
;
274 /***********************************************************/
275 /* x86 ISA bus support */
277 target_phys_addr_t isa_mem_base
= 0;
280 /***********************************************************/
281 void hw_error(const char *fmt
, ...)
287 fprintf(stderr
, "qemu: hardware error: ");
288 vfprintf(stderr
, fmt
, ap
);
289 fprintf(stderr
, "\n");
290 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
291 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
293 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
295 cpu_dump_state(env
, stderr
, fprintf
, 0);
302 static void set_proc_name(const char *s
)
304 #if defined(__linux__) && defined(PR_SET_NAME)
308 name
[sizeof(name
) - 1] = 0;
309 strncpy(name
, s
, sizeof(name
));
310 /* Could rewrite argv[0] too, but that's a bit more complicated.
311 This simple way is enough for `top'. */
312 prctl(PR_SET_NAME
, name
);
319 static QEMUBalloonEvent
*qemu_balloon_event
;
320 void *qemu_balloon_event_opaque
;
322 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
324 qemu_balloon_event
= func
;
325 qemu_balloon_event_opaque
= opaque
;
328 void qemu_balloon(ram_addr_t target
)
330 if (qemu_balloon_event
)
331 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
334 ram_addr_t
qemu_balloon_status(void)
336 if (qemu_balloon_event
)
337 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
341 /***********************************************************/
344 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
345 static void *qemu_put_kbd_event_opaque
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
347 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
349 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
351 qemu_put_kbd_event_opaque
= opaque
;
352 qemu_put_kbd_event
= func
;
355 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
356 void *opaque
, int absolute
,
359 QEMUPutMouseEntry
*s
, *cursor
;
361 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
363 s
->qemu_put_mouse_event
= func
;
364 s
->qemu_put_mouse_event_opaque
= opaque
;
365 s
->qemu_put_mouse_event_absolute
= absolute
;
366 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
369 if (!qemu_put_mouse_event_head
) {
370 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
374 cursor
= qemu_put_mouse_event_head
;
375 while (cursor
->next
!= NULL
)
376 cursor
= cursor
->next
;
379 qemu_put_mouse_event_current
= s
;
384 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
386 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
388 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
391 cursor
= qemu_put_mouse_event_head
;
392 while (cursor
!= NULL
&& cursor
!= entry
) {
394 cursor
= cursor
->next
;
397 if (cursor
== NULL
) // does not exist or list empty
399 else if (prev
== NULL
) { // entry is head
400 qemu_put_mouse_event_head
= cursor
->next
;
401 if (qemu_put_mouse_event_current
== entry
)
402 qemu_put_mouse_event_current
= cursor
->next
;
403 qemu_free(entry
->qemu_put_mouse_event_name
);
408 prev
->next
= entry
->next
;
410 if (qemu_put_mouse_event_current
== entry
)
411 qemu_put_mouse_event_current
= prev
;
413 qemu_free(entry
->qemu_put_mouse_event_name
);
417 void kbd_put_keycode(int keycode
)
419 if (qemu_put_kbd_event
) {
420 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
424 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
426 QEMUPutMouseEvent
*mouse_event
;
427 void *mouse_event_opaque
;
430 if (!qemu_put_mouse_event_current
) {
435 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
437 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
440 if (graphic_rotate
) {
441 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
444 width
= graphic_width
- 1;
445 mouse_event(mouse_event_opaque
,
446 width
- dy
, dx
, dz
, buttons_state
);
448 mouse_event(mouse_event_opaque
,
449 dx
, dy
, dz
, buttons_state
);
453 int kbd_mouse_is_absolute(void)
455 if (!qemu_put_mouse_event_current
)
458 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
461 void do_info_mice(Monitor
*mon
)
463 QEMUPutMouseEntry
*cursor
;
466 if (!qemu_put_mouse_event_head
) {
467 monitor_printf(mon
, "No mouse devices connected\n");
471 monitor_printf(mon
, "Mouse devices available:\n");
472 cursor
= qemu_put_mouse_event_head
;
473 while (cursor
!= NULL
) {
474 monitor_printf(mon
, "%c Mouse #%d: %s\n",
475 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
476 index
, cursor
->qemu_put_mouse_event_name
);
478 cursor
= cursor
->next
;
482 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
484 QEMUPutMouseEntry
*cursor
;
486 int index
= qdict_get_int(qdict
, "index");
488 if (!qemu_put_mouse_event_head
) {
489 monitor_printf(mon
, "No mouse devices connected\n");
493 cursor
= qemu_put_mouse_event_head
;
494 while (cursor
!= NULL
&& index
!= i
) {
496 cursor
= cursor
->next
;
500 qemu_put_mouse_event_current
= cursor
;
502 monitor_printf(mon
, "Mouse at given index not found\n");
505 /* compute with 96 bit intermediate result: (a*b)/c */
506 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
511 #ifdef HOST_WORDS_BIGENDIAN
521 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
522 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
525 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
529 /***********************************************************/
530 /* real time host monotonic timer */
532 static int64_t get_clock_realtime(void)
536 gettimeofday(&tv
, NULL
);
537 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
542 static int64_t clock_freq
;
544 static void init_get_clock(void)
548 ret
= QueryPerformanceFrequency(&freq
);
550 fprintf(stderr
, "Could not calibrate ticks\n");
553 clock_freq
= freq
.QuadPart
;
556 static int64_t get_clock(void)
559 QueryPerformanceCounter(&ti
);
560 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
565 static int use_rt_clock
;
567 static void init_get_clock(void)
570 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
571 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
574 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
581 static int64_t get_clock(void)
583 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
584 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
587 clock_gettime(CLOCK_MONOTONIC
, &ts
);
588 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
592 /* XXX: using gettimeofday leads to problems if the date
593 changes, so it should be avoided. */
594 return get_clock_realtime();
599 /* Return the virtual CPU time, based on the instruction counter. */
600 static int64_t cpu_get_icount(void)
603 CPUState
*env
= cpu_single_env
;;
604 icount
= qemu_icount
;
607 fprintf(stderr
, "Bad clock read\n");
608 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
610 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
613 /***********************************************************/
614 /* guest cycle counter */
616 typedef struct TimersState
{
617 int64_t cpu_ticks_prev
;
618 int64_t cpu_ticks_offset
;
619 int64_t cpu_clock_offset
;
620 int32_t cpu_ticks_enabled
;
624 TimersState timers_state
;
626 /* return the host CPU cycle counter and handle stop/restart */
627 int64_t cpu_get_ticks(void)
630 return cpu_get_icount();
632 if (!timers_state
.cpu_ticks_enabled
) {
633 return timers_state
.cpu_ticks_offset
;
636 ticks
= cpu_get_real_ticks();
637 if (timers_state
.cpu_ticks_prev
> ticks
) {
638 /* Note: non increasing ticks may happen if the host uses
640 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
642 timers_state
.cpu_ticks_prev
= ticks
;
643 return ticks
+ timers_state
.cpu_ticks_offset
;
647 /* return the host CPU monotonic timer and handle stop/restart */
648 static int64_t cpu_get_clock(void)
651 if (!timers_state
.cpu_ticks_enabled
) {
652 return timers_state
.cpu_clock_offset
;
655 return ti
+ timers_state
.cpu_clock_offset
;
659 /* enable cpu_get_ticks() */
660 void cpu_enable_ticks(void)
662 if (!timers_state
.cpu_ticks_enabled
) {
663 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
664 timers_state
.cpu_clock_offset
-= get_clock();
665 timers_state
.cpu_ticks_enabled
= 1;
669 /* disable cpu_get_ticks() : the clock is stopped. You must not call
670 cpu_get_ticks() after that. */
671 void cpu_disable_ticks(void)
673 if (timers_state
.cpu_ticks_enabled
) {
674 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
675 timers_state
.cpu_clock_offset
= cpu_get_clock();
676 timers_state
.cpu_ticks_enabled
= 0;
680 /***********************************************************/
683 #define QEMU_CLOCK_REALTIME 0
684 #define QEMU_CLOCK_VIRTUAL 1
685 #define QEMU_CLOCK_HOST 2
689 /* XXX: add frequency */
697 struct QEMUTimer
*next
;
700 struct qemu_alarm_timer
{
704 int (*start
)(struct qemu_alarm_timer
*t
);
705 void (*stop
)(struct qemu_alarm_timer
*t
);
706 void (*rearm
)(struct qemu_alarm_timer
*t
);
710 #define ALARM_FLAG_DYNTICKS 0x1
711 #define ALARM_FLAG_EXPIRED 0x2
713 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
715 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
718 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
720 if (!alarm_has_dynticks(t
))
726 /* TODO: MIN_TIMER_REARM_US should be optimized */
727 #define MIN_TIMER_REARM_US 250
729 static struct qemu_alarm_timer
*alarm_timer
;
733 struct qemu_alarm_win32
{
736 } alarm_win32_data
= {0, -1};
738 static int win32_start_timer(struct qemu_alarm_timer
*t
);
739 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
740 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
744 static int unix_start_timer(struct qemu_alarm_timer
*t
);
745 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
749 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
750 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
751 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
753 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
754 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
756 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
757 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
759 #endif /* __linux__ */
763 /* Correlation between real and virtual time is always going to be
764 fairly approximate, so ignore small variation.
765 When the guest is idle real and virtual time will be aligned in
767 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
769 static void icount_adjust(void)
774 static int64_t last_delta
;
775 /* If the VM is not running, then do nothing. */
779 cur_time
= cpu_get_clock();
780 cur_icount
= qemu_get_clock(vm_clock
);
781 delta
= cur_icount
- cur_time
;
782 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
784 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
785 && icount_time_shift
> 0) {
786 /* The guest is getting too far ahead. Slow time down. */
790 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
791 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
792 /* The guest is getting too far behind. Speed time up. */
796 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
799 static void icount_adjust_rt(void * opaque
)
801 qemu_mod_timer(icount_rt_timer
,
802 qemu_get_clock(rt_clock
) + 1000);
806 static void icount_adjust_vm(void * opaque
)
808 qemu_mod_timer(icount_vm_timer
,
809 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
813 static void init_icount_adjust(void)
815 /* Have both realtime and virtual time triggers for speed adjustment.
816 The realtime trigger catches emulated time passing too slowly,
817 the virtual time trigger catches emulated time passing too fast.
818 Realtime triggers occur even when idle, so use them less frequently
820 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
821 qemu_mod_timer(icount_rt_timer
,
822 qemu_get_clock(rt_clock
) + 1000);
823 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
824 qemu_mod_timer(icount_vm_timer
,
825 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
828 static struct qemu_alarm_timer alarm_timers
[] = {
831 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
832 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
833 /* HPET - if available - is preferred */
834 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
835 /* ...otherwise try RTC */
836 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
838 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
840 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
841 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
842 {"win32", 0, win32_start_timer
,
843 win32_stop_timer
, NULL
, &alarm_win32_data
},
848 static void show_available_alarms(void)
852 printf("Available alarm timers, in order of precedence:\n");
853 for (i
= 0; alarm_timers
[i
].name
; i
++)
854 printf("%s\n", alarm_timers
[i
].name
);
857 static void configure_alarms(char const *opt
)
861 int count
= ARRAY_SIZE(alarm_timers
) - 1;
864 struct qemu_alarm_timer tmp
;
866 if (!strcmp(opt
, "?")) {
867 show_available_alarms();
871 arg
= qemu_strdup(opt
);
873 /* Reorder the array */
874 name
= strtok(arg
, ",");
876 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
877 if (!strcmp(alarm_timers
[i
].name
, name
))
882 fprintf(stderr
, "Unknown clock %s\n", name
);
891 tmp
= alarm_timers
[i
];
892 alarm_timers
[i
] = alarm_timers
[cur
];
893 alarm_timers
[cur
] = tmp
;
897 name
= strtok(NULL
, ",");
903 /* Disable remaining timers */
904 for (i
= cur
; i
< count
; i
++)
905 alarm_timers
[i
].name
= NULL
;
907 show_available_alarms();
912 #define QEMU_NUM_CLOCKS 3
916 QEMUClock
*host_clock
;
918 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
920 static QEMUClock
*qemu_new_clock(int type
)
923 clock
= qemu_mallocz(sizeof(QEMUClock
));
928 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
932 ts
= qemu_mallocz(sizeof(QEMUTimer
));
939 void qemu_free_timer(QEMUTimer
*ts
)
944 /* stop a timer, but do not dealloc it */
945 void qemu_del_timer(QEMUTimer
*ts
)
949 /* NOTE: this code must be signal safe because
950 qemu_timer_expired() can be called from a signal. */
951 pt
= &active_timers
[ts
->clock
->type
];
964 /* modify the current timer so that it will be fired when current_time
965 >= expire_time. The corresponding callback will be called. */
966 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
972 /* add the timer in the sorted list */
973 /* NOTE: this code must be signal safe because
974 qemu_timer_expired() can be called from a signal. */
975 pt
= &active_timers
[ts
->clock
->type
];
980 if (t
->expire_time
> expire_time
)
984 ts
->expire_time
= expire_time
;
988 /* Rearm if necessary */
989 if (pt
== &active_timers
[ts
->clock
->type
]) {
990 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
991 qemu_rearm_alarm_timer(alarm_timer
);
993 /* Interrupt execution to force deadline recalculation. */
999 int qemu_timer_pending(QEMUTimer
*ts
)
1002 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1009 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1013 return (timer_head
->expire_time
<= current_time
);
1016 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1022 if (!ts
|| ts
->expire_time
> current_time
)
1024 /* remove timer from the list before calling the callback */
1025 *ptimer_head
= ts
->next
;
1028 /* run the callback (the timer list can be modified) */
1033 int64_t qemu_get_clock(QEMUClock
*clock
)
1035 switch(clock
->type
) {
1036 case QEMU_CLOCK_REALTIME
:
1037 return get_clock() / 1000000;
1039 case QEMU_CLOCK_VIRTUAL
:
1041 return cpu_get_icount();
1043 return cpu_get_clock();
1045 case QEMU_CLOCK_HOST
:
1046 return get_clock_realtime();
1050 static void init_clocks(void)
1053 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1054 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1055 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1057 rtc_clock
= host_clock
;
1061 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1063 uint64_t expire_time
;
1065 if (qemu_timer_pending(ts
)) {
1066 expire_time
= ts
->expire_time
;
1070 qemu_put_be64(f
, expire_time
);
1073 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1075 uint64_t expire_time
;
1077 expire_time
= qemu_get_be64(f
);
1078 if (expire_time
!= -1) {
1079 qemu_mod_timer(ts
, expire_time
);
1085 static const VMStateDescription vmstate_timers
= {
1088 .minimum_version_id
= 1,
1089 .minimum_version_id_old
= 1,
1090 .fields
= (VMStateField
[]) {
1091 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1092 VMSTATE_INT64(dummy
, TimersState
),
1093 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1094 VMSTATE_END_OF_LIST()
1098 static void qemu_event_increment(void);
1101 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1102 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1105 static void host_alarm_handler(int host_signum
)
1109 #define DISP_FREQ 1000
1111 static int64_t delta_min
= INT64_MAX
;
1112 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1114 ti
= qemu_get_clock(vm_clock
);
1115 if (last_clock
!= 0) {
1116 delta
= ti
- last_clock
;
1117 if (delta
< delta_min
)
1119 if (delta
> delta_max
)
1122 if (++count
== DISP_FREQ
) {
1123 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1124 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1125 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1126 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1127 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1129 delta_min
= INT64_MAX
;
1137 if (alarm_has_dynticks(alarm_timer
) ||
1139 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1140 qemu_get_clock(vm_clock
))) ||
1141 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1142 qemu_get_clock(rt_clock
)) ||
1143 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1144 qemu_get_clock(host_clock
))) {
1145 qemu_event_increment();
1146 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1148 #ifndef CONFIG_IOTHREAD
1150 /* stop the currently executing cpu because a timer occured */
1154 timer_alarm_pending
= 1;
1155 qemu_notify_event();
1159 static int64_t qemu_next_deadline(void)
1161 /* To avoid problems with overflow limit this to 2^32. */
1162 int64_t delta
= INT32_MAX
;
1164 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1165 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1166 qemu_get_clock(vm_clock
);
1168 if (active_timers
[QEMU_CLOCK_HOST
]) {
1169 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1170 qemu_get_clock(host_clock
);
1181 #if defined(__linux__)
1182 static uint64_t qemu_next_deadline_dyntick(void)
1190 delta
= (qemu_next_deadline() + 999) / 1000;
1192 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1193 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1194 qemu_get_clock(rt_clock
))*1000;
1195 if (rtdelta
< delta
)
1199 if (delta
< MIN_TIMER_REARM_US
)
1200 delta
= MIN_TIMER_REARM_US
;
1208 /* Sets a specific flag */
1209 static int fcntl_setfl(int fd
, int flag
)
1213 flags
= fcntl(fd
, F_GETFL
);
1217 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1223 #if defined(__linux__)
1225 #define RTC_FREQ 1024
1227 static void enable_sigio_timer(int fd
)
1229 struct sigaction act
;
1232 sigfillset(&act
.sa_mask
);
1234 act
.sa_handler
= host_alarm_handler
;
1236 sigaction(SIGIO
, &act
, NULL
);
1237 fcntl_setfl(fd
, O_ASYNC
);
1238 fcntl(fd
, F_SETOWN
, getpid());
1241 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1243 struct hpet_info info
;
1246 fd
= open("/dev/hpet", O_RDONLY
);
1251 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1253 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1254 "error, but for better emulation accuracy type:\n"
1255 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1259 /* Check capabilities */
1260 r
= ioctl(fd
, HPET_INFO
, &info
);
1264 /* Enable periodic mode */
1265 r
= ioctl(fd
, HPET_EPI
, 0);
1266 if (info
.hi_flags
&& (r
< 0))
1269 /* Enable interrupt */
1270 r
= ioctl(fd
, HPET_IE_ON
, 0);
1274 enable_sigio_timer(fd
);
1275 t
->priv
= (void *)(long)fd
;
1283 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1285 int fd
= (long)t
->priv
;
1290 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1293 unsigned long current_rtc_freq
= 0;
1295 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1298 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1299 if (current_rtc_freq
!= RTC_FREQ
&&
1300 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1301 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1302 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1303 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1306 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1312 enable_sigio_timer(rtc_fd
);
1314 t
->priv
= (void *)(long)rtc_fd
;
1319 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1321 int rtc_fd
= (long)t
->priv
;
1326 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1330 struct sigaction act
;
1332 sigfillset(&act
.sa_mask
);
1334 act
.sa_handler
= host_alarm_handler
;
1336 sigaction(SIGALRM
, &act
, NULL
);
1339 * Initialize ev struct to 0 to avoid valgrind complaining
1340 * about uninitialized data in timer_create call
1342 memset(&ev
, 0, sizeof(ev
));
1343 ev
.sigev_value
.sival_int
= 0;
1344 ev
.sigev_notify
= SIGEV_SIGNAL
;
1345 ev
.sigev_signo
= SIGALRM
;
1347 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1348 perror("timer_create");
1350 /* disable dynticks */
1351 fprintf(stderr
, "Dynamic Ticks disabled\n");
1356 t
->priv
= (void *)(long)host_timer
;
1361 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1363 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1365 timer_delete(host_timer
);
1368 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1370 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1371 struct itimerspec timeout
;
1372 int64_t nearest_delta_us
= INT64_MAX
;
1375 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1376 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1377 !active_timers
[QEMU_CLOCK_HOST
])
1380 nearest_delta_us
= qemu_next_deadline_dyntick();
1382 /* check whether a timer is already running */
1383 if (timer_gettime(host_timer
, &timeout
)) {
1385 fprintf(stderr
, "Internal timer error: aborting\n");
1388 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1389 if (current_us
&& current_us
<= nearest_delta_us
)
1392 timeout
.it_interval
.tv_sec
= 0;
1393 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1394 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1395 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1396 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1398 fprintf(stderr
, "Internal timer error: aborting\n");
1403 #endif /* defined(__linux__) */
1405 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1407 struct sigaction act
;
1408 struct itimerval itv
;
1412 sigfillset(&act
.sa_mask
);
1414 act
.sa_handler
= host_alarm_handler
;
1416 sigaction(SIGALRM
, &act
, NULL
);
1418 itv
.it_interval
.tv_sec
= 0;
1419 /* for i386 kernel 2.6 to get 1 ms */
1420 itv
.it_interval
.tv_usec
= 999;
1421 itv
.it_value
.tv_sec
= 0;
1422 itv
.it_value
.tv_usec
= 10 * 1000;
1424 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1431 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1433 struct itimerval itv
;
1435 memset(&itv
, 0, sizeof(itv
));
1436 setitimer(ITIMER_REAL
, &itv
, NULL
);
1439 #endif /* !defined(_WIN32) */
1444 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1447 struct qemu_alarm_win32
*data
= t
->priv
;
1450 memset(&tc
, 0, sizeof(tc
));
1451 timeGetDevCaps(&tc
, sizeof(tc
));
1453 if (data
->period
< tc
.wPeriodMin
)
1454 data
->period
= tc
.wPeriodMin
;
1456 timeBeginPeriod(data
->period
);
1458 flags
= TIME_CALLBACK_FUNCTION
;
1459 if (alarm_has_dynticks(t
))
1460 flags
|= TIME_ONESHOT
;
1462 flags
|= TIME_PERIODIC
;
1464 data
->timerId
= timeSetEvent(1, // interval (ms)
1465 data
->period
, // resolution
1466 host_alarm_handler
, // function
1467 (DWORD
)t
, // parameter
1470 if (!data
->timerId
) {
1471 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1473 timeEndPeriod(data
->period
);
1480 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1482 struct qemu_alarm_win32
*data
= t
->priv
;
1484 timeKillEvent(data
->timerId
);
1485 timeEndPeriod(data
->period
);
1488 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1490 struct qemu_alarm_win32
*data
= t
->priv
;
1492 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1493 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1494 !active_timers
[QEMU_CLOCK_HOST
])
1497 timeKillEvent(data
->timerId
);
1499 data
->timerId
= timeSetEvent(1,
1503 TIME_ONESHOT
| TIME_PERIODIC
);
1505 if (!data
->timerId
) {
1506 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1509 timeEndPeriod(data
->period
);
1516 static int init_timer_alarm(void)
1518 struct qemu_alarm_timer
*t
= NULL
;
1521 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1522 t
= &alarm_timers
[i
];
1542 static void quit_timers(void)
1544 alarm_timer
->stop(alarm_timer
);
1548 /***********************************************************/
1549 /* host time/date access */
1550 void qemu_get_timedate(struct tm
*tm
, int offset
)
1557 if (rtc_date_offset
== -1) {
1561 ret
= localtime(&ti
);
1563 ti
-= rtc_date_offset
;
1567 memcpy(tm
, ret
, sizeof(struct tm
));
1570 int qemu_timedate_diff(struct tm
*tm
)
1574 if (rtc_date_offset
== -1)
1576 seconds
= mktimegm(tm
);
1578 seconds
= mktime(tm
);
1580 seconds
= mktimegm(tm
) + rtc_date_offset
;
1582 return seconds
- time(NULL
);
1585 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1587 time_t rtc_start_date
;
1590 if (!strcmp(startdate
, "now") && legacy
) {
1591 rtc_date_offset
= -1;
1593 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1601 } else if (sscanf(startdate
, "%d-%d-%d",
1604 &tm
.tm_mday
) == 3) {
1613 rtc_start_date
= mktimegm(&tm
);
1614 if (rtc_start_date
== -1) {
1616 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1617 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1620 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1624 static void configure_rtc(QemuOpts
*opts
)
1628 value
= qemu_opt_get(opts
, "base");
1630 if (!strcmp(value
, "utc")) {
1632 } else if (!strcmp(value
, "localtime")) {
1635 configure_rtc_date_offset(value
, 0);
1638 value
= qemu_opt_get(opts
, "clock");
1640 if (!strcmp(value
, "host")) {
1641 rtc_clock
= host_clock
;
1642 } else if (!strcmp(value
, "vm")) {
1643 rtc_clock
= vm_clock
;
1645 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1649 #ifdef CONFIG_TARGET_I386
1650 value
= qemu_opt_get(opts
, "driftfix");
1652 if (!strcmp(buf
, "slew")) {
1654 } else if (!strcmp(buf
, "none")) {
1657 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1665 static void socket_cleanup(void)
1670 static int socket_init(void)
1675 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1677 err
= WSAGetLastError();
1678 fprintf(stderr
, "WSAStartup: %d\n", err
);
1681 atexit(socket_cleanup
);
1686 /***********************************************************/
1687 /* Bluetooth support */
1690 static struct HCIInfo
*hci_table
[MAX_NICS
];
1692 static struct bt_vlan_s
{
1693 struct bt_scatternet_s net
;
1695 struct bt_vlan_s
*next
;
1698 /* find or alloc a new bluetooth "VLAN" */
1699 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1701 struct bt_vlan_s
**pvlan
, *vlan
;
1702 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1706 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1708 pvlan
= &first_bt_vlan
;
1709 while (*pvlan
!= NULL
)
1710 pvlan
= &(*pvlan
)->next
;
1715 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1719 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1724 static struct HCIInfo null_hci
= {
1725 .cmd_send
= null_hci_send
,
1726 .sco_send
= null_hci_send
,
1727 .acl_send
= null_hci_send
,
1728 .bdaddr_set
= null_hci_addr_set
,
1731 struct HCIInfo
*qemu_next_hci(void)
1733 if (cur_hci
== nb_hcis
)
1736 return hci_table
[cur_hci
++];
1739 static struct HCIInfo
*hci_init(const char *str
)
1742 struct bt_scatternet_s
*vlan
= 0;
1744 if (!strcmp(str
, "null"))
1747 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1749 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1750 else if (!strncmp(str
, "hci", 3)) {
1753 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1754 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1759 vlan
= qemu_find_bt_vlan(0);
1761 return bt_new_hci(vlan
);
1764 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1769 static int bt_hci_parse(const char *str
)
1771 struct HCIInfo
*hci
;
1774 if (nb_hcis
>= MAX_NICS
) {
1775 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1779 hci
= hci_init(str
);
1788 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1789 hci
->bdaddr_set(hci
, bdaddr
.b
);
1791 hci_table
[nb_hcis
++] = hci
;
1796 static void bt_vhci_add(int vlan_id
)
1798 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1801 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1802 "an empty scatternet %i\n", vlan_id
);
1804 bt_vhci_init(bt_new_hci(vlan
));
1807 static struct bt_device_s
*bt_device_add(const char *opt
)
1809 struct bt_scatternet_s
*vlan
;
1811 char *endp
= strstr(opt
, ",vlan=");
1812 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1815 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1818 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1820 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1825 vlan
= qemu_find_bt_vlan(vlan_id
);
1828 fprintf(stderr
, "qemu: warning: adding a slave device to "
1829 "an empty scatternet %i\n", vlan_id
);
1831 if (!strcmp(devname
, "keyboard"))
1832 return bt_keyboard_init(vlan
);
1834 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1838 static int bt_parse(const char *opt
)
1840 const char *endp
, *p
;
1843 if (strstart(opt
, "hci", &endp
)) {
1844 if (!*endp
|| *endp
== ',') {
1846 if (!strstart(endp
, ",vlan=", 0))
1849 return bt_hci_parse(opt
);
1851 } else if (strstart(opt
, "vhci", &endp
)) {
1852 if (!*endp
|| *endp
== ',') {
1854 if (strstart(endp
, ",vlan=", &p
)) {
1855 vlan
= strtol(p
, (char **) &endp
, 0);
1857 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1861 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1870 } else if (strstart(opt
, "device:", &endp
))
1871 return !bt_device_add(endp
);
1873 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1877 /***********************************************************/
1878 /* QEMU Block devices */
1880 #define HD_ALIAS "index=%d,media=disk"
1881 #define CDROM_ALIAS "index=2,media=cdrom"
1882 #define FD_ALIAS "index=%d,if=floppy"
1883 #define PFLASH_ALIAS "if=pflash"
1884 #define MTD_ALIAS "if=mtd"
1885 #define SD_ALIAS "index=0,if=sd"
1887 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1894 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1897 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1899 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1900 __FUNCTION__
, optstr
);
1904 qemu_opt_set(opts
, "file", file
);
1908 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1912 /* seek interface, bus and unit */
1914 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1915 if (dinfo
->type
== type
&&
1916 dinfo
->bus
== bus
&&
1917 dinfo
->unit
== unit
)
1924 DriveInfo
*drive_get_by_id(const char *id
)
1928 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1929 if (strcmp(id
, dinfo
->id
))
1936 int drive_get_max_bus(BlockInterfaceType type
)
1942 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1943 if(dinfo
->type
== type
&&
1944 dinfo
->bus
> max_bus
)
1945 max_bus
= dinfo
->bus
;
1950 const char *drive_get_serial(BlockDriverState
*bdrv
)
1954 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1955 if (dinfo
->bdrv
== bdrv
)
1956 return dinfo
->serial
;
1962 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1966 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1967 if (dinfo
->bdrv
== bdrv
)
1968 return dinfo
->onerror
;
1971 return BLOCK_ERR_STOP_ENOSPC
;
1974 static void bdrv_format_print(void *opaque
, const char *name
)
1976 fprintf(stderr
, " %s", name
);
1979 void drive_uninit(DriveInfo
*dinfo
)
1981 qemu_opts_del(dinfo
->opts
);
1982 bdrv_delete(dinfo
->bdrv
);
1983 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1987 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1991 const char *file
= NULL
;
1994 const char *mediastr
= "";
1995 BlockInterfaceType type
;
1996 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1997 int bus_id
, unit_id
;
1998 int cyls
, heads
, secs
, translation
;
1999 BlockDriver
*drv
= NULL
;
2000 QEMUMachine
*machine
= opaque
;
2006 int bdrv_flags
, onerror
;
2007 const char *devaddr
;
2013 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2016 if (machine
&& machine
->use_scsi
) {
2018 max_devs
= MAX_SCSI_DEVS
;
2019 pstrcpy(devname
, sizeof(devname
), "scsi");
2022 max_devs
= MAX_IDE_DEVS
;
2023 pstrcpy(devname
, sizeof(devname
), "ide");
2027 /* extract parameters */
2028 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2029 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2030 index
= qemu_opt_get_number(opts
, "index", -1);
2032 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2033 heads
= qemu_opt_get_number(opts
, "heads", 0);
2034 secs
= qemu_opt_get_number(opts
, "secs", 0);
2036 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2037 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2039 file
= qemu_opt_get(opts
, "file");
2040 serial
= qemu_opt_get(opts
, "serial");
2042 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2043 pstrcpy(devname
, sizeof(devname
), buf
);
2044 if (!strcmp(buf
, "ide")) {
2046 max_devs
= MAX_IDE_DEVS
;
2047 } else if (!strcmp(buf
, "scsi")) {
2049 max_devs
= MAX_SCSI_DEVS
;
2050 } else if (!strcmp(buf
, "floppy")) {
2053 } else if (!strcmp(buf
, "pflash")) {
2056 } else if (!strcmp(buf
, "mtd")) {
2059 } else if (!strcmp(buf
, "sd")) {
2062 } else if (!strcmp(buf
, "virtio")) {
2065 } else if (!strcmp(buf
, "xen")) {
2068 } else if (!strcmp(buf
, "none")) {
2072 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2077 if (cyls
|| heads
|| secs
) {
2078 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2079 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2082 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2083 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2086 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2087 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2092 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2095 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2099 if (!strcmp(buf
, "none"))
2100 translation
= BIOS_ATA_TRANSLATION_NONE
;
2101 else if (!strcmp(buf
, "lba"))
2102 translation
= BIOS_ATA_TRANSLATION_LBA
;
2103 else if (!strcmp(buf
, "auto"))
2104 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2106 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2111 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2112 if (!strcmp(buf
, "disk")) {
2114 } else if (!strcmp(buf
, "cdrom")) {
2115 if (cyls
|| secs
|| heads
) {
2117 "qemu: '%s' invalid physical CHS format\n", buf
);
2120 media
= MEDIA_CDROM
;
2122 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2127 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2128 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2130 else if (!strcmp(buf
, "writethrough"))
2132 else if (!strcmp(buf
, "writeback"))
2135 fprintf(stderr
, "qemu: invalid cache option\n");
2140 #ifdef CONFIG_LINUX_AIO
2141 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2142 if (!strcmp(buf
, "threads"))
2144 else if (!strcmp(buf
, "native"))
2147 fprintf(stderr
, "qemu: invalid aio option\n");
2153 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2154 if (strcmp(buf
, "?") == 0) {
2155 fprintf(stderr
, "qemu: Supported formats:");
2156 bdrv_iterate_format(bdrv_format_print
, NULL
);
2157 fprintf(stderr
, "\n");
2160 drv
= bdrv_find_whitelisted_format(buf
);
2162 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2167 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2168 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2169 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2170 fprintf(stderr
, "werror is no supported by this format\n");
2173 if (!strcmp(buf
, "ignore"))
2174 onerror
= BLOCK_ERR_IGNORE
;
2175 else if (!strcmp(buf
, "enospc"))
2176 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2177 else if (!strcmp(buf
, "stop"))
2178 onerror
= BLOCK_ERR_STOP_ANY
;
2179 else if (!strcmp(buf
, "report"))
2180 onerror
= BLOCK_ERR_REPORT
;
2182 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2187 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2188 if (type
!= IF_VIRTIO
) {
2189 fprintf(stderr
, "addr is not supported\n");
2194 /* compute bus and unit according index */
2197 if (bus_id
!= 0 || unit_id
!= -1) {
2199 "qemu: index cannot be used with bus and unit\n");
2207 unit_id
= index
% max_devs
;
2208 bus_id
= index
/ max_devs
;
2212 /* if user doesn't specify a unit_id,
2213 * try to find the first free
2216 if (unit_id
== -1) {
2218 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2220 if (max_devs
&& unit_id
>= max_devs
) {
2221 unit_id
-= max_devs
;
2229 if (max_devs
&& unit_id
>= max_devs
) {
2230 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2231 unit_id
, max_devs
- 1);
2236 * ignore multiple definitions
2239 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2246 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2247 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2248 dinfo
->id
= qemu_strdup(buf
);
2250 /* no id supplied -> create one */
2251 dinfo
->id
= qemu_mallocz(32);
2252 if (type
== IF_IDE
|| type
== IF_SCSI
)
2253 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2255 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2256 devname
, bus_id
, mediastr
, unit_id
);
2258 snprintf(dinfo
->id
, 32, "%s%s%i",
2259 devname
, mediastr
, unit_id
);
2261 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2262 dinfo
->devaddr
= devaddr
;
2264 dinfo
->bus
= bus_id
;
2265 dinfo
->unit
= unit_id
;
2266 dinfo
->onerror
= onerror
;
2269 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2270 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2280 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2281 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2285 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2290 /* FIXME: This isn't really a floppy, but it's a reasonable
2293 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2299 /* add virtio block device */
2300 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2301 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2302 qemu_opt_set(opts
, "drive", dinfo
->id
);
2304 qemu_opt_set(opts
, "addr", devaddr
);
2315 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2316 cache
= 2; /* always use write-back with snapshot */
2318 if (cache
== 0) /* no caching */
2319 bdrv_flags
|= BDRV_O_NOCACHE
;
2320 else if (cache
== 2) /* write-back */
2321 bdrv_flags
|= BDRV_O_CACHE_WB
;
2324 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2326 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2330 if (type
== IF_IDE
) {
2331 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2334 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2337 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2338 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2339 file
, strerror(errno
));
2343 if (bdrv_key_required(dinfo
->bdrv
))
2349 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2351 QEMUMachine
*machine
= opaque
;
2352 int fatal_error
= 0;
2354 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2361 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2363 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2364 qemu_opt_set(opts
, "snapshot", "on");
2369 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2371 boot_set_handler
= func
;
2372 boot_set_opaque
= opaque
;
2375 int qemu_boot_set(const char *boot_devices
)
2377 if (!boot_set_handler
) {
2380 return boot_set_handler(boot_set_opaque
, boot_devices
);
2383 static int parse_bootdevices(char *devices
)
2385 /* We just do some generic consistency checks */
2389 for (p
= devices
; *p
!= '\0'; p
++) {
2390 /* Allowed boot devices are:
2391 * a-b: floppy disk drives
2392 * c-f: IDE disk drives
2393 * g-m: machine implementation dependant drives
2394 * n-p: network devices
2395 * It's up to each machine implementation to check if the given boot
2396 * devices match the actual hardware implementation and firmware
2399 if (*p
< 'a' || *p
> 'p') {
2400 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2403 if (bitmap
& (1 << (*p
- 'a'))) {
2404 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2407 bitmap
|= 1 << (*p
- 'a');
2412 static void restore_boot_devices(void *opaque
)
2414 char *standard_boot_devices
= opaque
;
2416 qemu_boot_set(standard_boot_devices
);
2418 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2419 qemu_free(standard_boot_devices
);
2422 static void numa_add(const char *optarg
)
2426 unsigned long long value
, endvalue
;
2429 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2430 if (!strcmp(option
, "node")) {
2431 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2432 nodenr
= nb_numa_nodes
;
2434 nodenr
= strtoull(option
, NULL
, 10);
2437 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2438 node_mem
[nodenr
] = 0;
2440 value
= strtoull(option
, &endptr
, 0);
2442 case 0: case 'M': case 'm':
2449 node_mem
[nodenr
] = value
;
2451 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2452 node_cpumask
[nodenr
] = 0;
2454 value
= strtoull(option
, &endptr
, 10);
2457 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2459 if (*endptr
== '-') {
2460 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2461 if (endvalue
>= 63) {
2464 "only 63 CPUs in NUMA mode supported.\n");
2466 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2471 node_cpumask
[nodenr
] = value
;
2478 static void smp_parse(const char *optarg
)
2480 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2484 smp
= strtoul(optarg
, &endptr
, 10);
2485 if (endptr
!= optarg
) {
2486 if (*endptr
== ',') {
2490 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2491 sockets
= strtoull(option
, NULL
, 10);
2492 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2493 cores
= strtoull(option
, NULL
, 10);
2494 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2495 threads
= strtoull(option
, NULL
, 10);
2496 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2497 max_cpus
= strtoull(option
, NULL
, 10);
2499 /* compute missing values, prefer sockets over cores over threads */
2500 if (smp
== 0 || sockets
== 0) {
2501 sockets
= sockets
> 0 ? sockets
: 1;
2502 cores
= cores
> 0 ? cores
: 1;
2503 threads
= threads
> 0 ? threads
: 1;
2505 smp
= cores
* threads
* sockets
;
2507 sockets
= smp
/ (cores
* threads
);
2511 threads
= threads
> 0 ? threads
: 1;
2512 cores
= smp
/ (sockets
* threads
);
2515 sockets
= smp
/ (cores
* threads
);
2517 threads
= smp
/ (cores
* sockets
);
2522 smp_cores
= cores
> 0 ? cores
: 1;
2523 smp_threads
= threads
> 0 ? threads
: 1;
2525 max_cpus
= smp_cpus
;
2528 /***********************************************************/
2531 static int usb_device_add(const char *devname
, int is_hotplug
)
2534 USBDevice
*dev
= NULL
;
2539 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2540 dev
= usbdevice_create(devname
);
2544 /* the other ones */
2545 if (strstart(devname
, "host:", &p
)) {
2546 dev
= usb_host_device_open(p
);
2547 } else if (strstart(devname
, "net:", &p
)) {
2551 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2556 qemu_opt_set(opts
, "type", "nic");
2557 qemu_opt_set(opts
, "model", "usb");
2559 idx
= net_client_init(NULL
, opts
, 0);
2564 dev
= usb_net_init(&nd_table
[idx
]);
2565 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2566 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2567 bt_new_hci(qemu_find_bt_vlan(0)));
2578 static int usb_device_del(const char *devname
)
2583 if (strstart(devname
, "host:", &p
))
2584 return usb_host_device_close(p
);
2589 p
= strchr(devname
, '.');
2592 bus_num
= strtoul(devname
, NULL
, 0);
2593 addr
= strtoul(p
+ 1, NULL
, 0);
2595 return usb_device_delete_addr(bus_num
, addr
);
2598 static int usb_parse(const char *cmdline
)
2600 return usb_device_add(cmdline
, 0);
2603 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2605 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2608 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2610 usb_device_del(qdict_get_str(qdict
, "devname"));
2613 /***********************************************************/
2614 /* PCMCIA/Cardbus */
2616 static struct pcmcia_socket_entry_s
{
2617 PCMCIASocket
*socket
;
2618 struct pcmcia_socket_entry_s
*next
;
2619 } *pcmcia_sockets
= 0;
2621 void pcmcia_socket_register(PCMCIASocket
*socket
)
2623 struct pcmcia_socket_entry_s
*entry
;
2625 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2626 entry
->socket
= socket
;
2627 entry
->next
= pcmcia_sockets
;
2628 pcmcia_sockets
= entry
;
2631 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2633 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2635 ptr
= &pcmcia_sockets
;
2636 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2637 if (entry
->socket
== socket
) {
2643 void pcmcia_info(Monitor
*mon
)
2645 struct pcmcia_socket_entry_s
*iter
;
2647 if (!pcmcia_sockets
)
2648 monitor_printf(mon
, "No PCMCIA sockets\n");
2650 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2651 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2652 iter
->socket
->attached
? iter
->socket
->card_string
:
2656 /***********************************************************/
2657 /* register display */
2659 struct DisplayAllocator default_allocator
= {
2660 defaultallocator_create_displaysurface
,
2661 defaultallocator_resize_displaysurface
,
2662 defaultallocator_free_displaysurface
2665 void register_displaystate(DisplayState
*ds
)
2675 DisplayState
*get_displaystate(void)
2677 return display_state
;
2680 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2682 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2683 return ds
->allocator
;
2688 static void dumb_display_init(void)
2690 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2691 ds
->allocator
= &default_allocator
;
2692 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2693 register_displaystate(ds
);
2696 /***********************************************************/
2699 typedef struct IOHandlerRecord
{
2701 IOCanRWHandler
*fd_read_poll
;
2703 IOHandler
*fd_write
;
2706 /* temporary data */
2708 struct IOHandlerRecord
*next
;
2711 static IOHandlerRecord
*first_io_handler
;
2713 /* XXX: fd_read_poll should be suppressed, but an API change is
2714 necessary in the character devices to suppress fd_can_read(). */
2715 int qemu_set_fd_handler2(int fd
,
2716 IOCanRWHandler
*fd_read_poll
,
2718 IOHandler
*fd_write
,
2721 IOHandlerRecord
**pioh
, *ioh
;
2723 if (!fd_read
&& !fd_write
) {
2724 pioh
= &first_io_handler
;
2729 if (ioh
->fd
== fd
) {
2736 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2740 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2741 ioh
->next
= first_io_handler
;
2742 first_io_handler
= ioh
;
2745 ioh
->fd_read_poll
= fd_read_poll
;
2746 ioh
->fd_read
= fd_read
;
2747 ioh
->fd_write
= fd_write
;
2748 ioh
->opaque
= opaque
;
2754 int qemu_set_fd_handler(int fd
,
2756 IOHandler
*fd_write
,
2759 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2763 /***********************************************************/
2764 /* Polling handling */
2766 typedef struct PollingEntry
{
2769 struct PollingEntry
*next
;
2772 static PollingEntry
*first_polling_entry
;
2774 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2776 PollingEntry
**ppe
, *pe
;
2777 pe
= qemu_mallocz(sizeof(PollingEntry
));
2779 pe
->opaque
= opaque
;
2780 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2785 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2787 PollingEntry
**ppe
, *pe
;
2788 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2790 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2798 /***********************************************************/
2799 /* Wait objects support */
2800 typedef struct WaitObjects
{
2802 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2803 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2804 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2807 static WaitObjects wait_objects
= {0};
2809 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2811 WaitObjects
*w
= &wait_objects
;
2813 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2815 w
->events
[w
->num
] = handle
;
2816 w
->func
[w
->num
] = func
;
2817 w
->opaque
[w
->num
] = opaque
;
2822 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2825 WaitObjects
*w
= &wait_objects
;
2828 for (i
= 0; i
< w
->num
; i
++) {
2829 if (w
->events
[i
] == handle
)
2832 w
->events
[i
] = w
->events
[i
+ 1];
2833 w
->func
[i
] = w
->func
[i
+ 1];
2834 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2842 /***********************************************************/
2843 /* ram save/restore */
2845 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2846 #define RAM_SAVE_FLAG_COMPRESS 0x02
2847 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2848 #define RAM_SAVE_FLAG_PAGE 0x08
2849 #define RAM_SAVE_FLAG_EOS 0x10
2851 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2853 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2854 uint32_t *array
= (uint32_t *)page
;
2857 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2858 if (array
[i
] != val
)
2865 static int ram_save_block(QEMUFile
*f
)
2867 static ram_addr_t current_addr
= 0;
2868 ram_addr_t saved_addr
= current_addr
;
2869 ram_addr_t addr
= 0;
2872 while (addr
< last_ram_offset
) {
2873 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2876 cpu_physical_memory_reset_dirty(current_addr
,
2877 current_addr
+ TARGET_PAGE_SIZE
,
2878 MIGRATION_DIRTY_FLAG
);
2880 p
= qemu_get_ram_ptr(current_addr
);
2882 if (is_dup_page(p
, *p
)) {
2883 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2884 qemu_put_byte(f
, *p
);
2886 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2887 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2893 addr
+= TARGET_PAGE_SIZE
;
2894 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2900 static uint64_t bytes_transferred
;
2902 static ram_addr_t
ram_save_remaining(void)
2905 ram_addr_t count
= 0;
2907 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2908 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2915 uint64_t ram_bytes_remaining(void)
2917 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2920 uint64_t ram_bytes_transferred(void)
2922 return bytes_transferred
;
2925 uint64_t ram_bytes_total(void)
2927 return last_ram_offset
;
2930 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2933 uint64_t bytes_transferred_last
;
2935 uint64_t expected_time
= 0;
2938 cpu_physical_memory_set_dirty_tracking(0);
2942 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2943 qemu_file_set_error(f
);
2948 bytes_transferred
= 0;
2950 /* Make sure all dirty bits are set */
2951 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2952 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2953 cpu_physical_memory_set_dirty(addr
);
2956 /* Enable dirty memory tracking */
2957 cpu_physical_memory_set_dirty_tracking(1);
2959 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2962 bytes_transferred_last
= bytes_transferred
;
2963 bwidth
= get_clock();
2965 while (!qemu_file_rate_limit(f
)) {
2968 ret
= ram_save_block(f
);
2969 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2970 if (ret
== 0) /* no more blocks */
2974 bwidth
= get_clock() - bwidth
;
2975 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2977 /* if we haven't transferred anything this round, force expected_time to a
2978 * a very high value, but without crashing */
2982 /* try transferring iterative blocks of memory */
2984 /* flush all remaining blocks regardless of rate limiting */
2985 while (ram_save_block(f
) != 0) {
2986 bytes_transferred
+= TARGET_PAGE_SIZE
;
2988 cpu_physical_memory_set_dirty_tracking(0);
2991 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2993 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2995 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2998 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3003 if (version_id
!= 3)
3007 addr
= qemu_get_be64(f
);
3009 flags
= addr
& ~TARGET_PAGE_MASK
;
3010 addr
&= TARGET_PAGE_MASK
;
3012 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3013 if (addr
!= last_ram_offset
)
3017 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3018 uint8_t ch
= qemu_get_byte(f
);
3019 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3022 (!kvm_enabled() || kvm_has_sync_mmu())) {
3023 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3026 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
3027 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3029 if (qemu_file_has_error(f
)) {
3032 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3037 void qemu_service_io(void)
3039 qemu_notify_event();
3042 /***********************************************************/
3043 /* machine registration */
3045 static QEMUMachine
*first_machine
= NULL
;
3046 QEMUMachine
*current_machine
= NULL
;
3048 int qemu_register_machine(QEMUMachine
*m
)
3051 pm
= &first_machine
;
3059 static QEMUMachine
*find_machine(const char *name
)
3063 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3064 if (!strcmp(m
->name
, name
))
3066 if (m
->alias
&& !strcmp(m
->alias
, name
))
3072 static QEMUMachine
*find_default_machine(void)
3076 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3077 if (m
->is_default
) {
3084 /***********************************************************/
3085 /* main execution loop */
3087 static void gui_update(void *opaque
)
3089 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3090 DisplayState
*ds
= opaque
;
3091 DisplayChangeListener
*dcl
= ds
->listeners
;
3095 while (dcl
!= NULL
) {
3096 if (dcl
->gui_timer_interval
&&
3097 dcl
->gui_timer_interval
< interval
)
3098 interval
= dcl
->gui_timer_interval
;
3101 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3104 static void nographic_update(void *opaque
)
3106 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3108 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3111 struct vm_change_state_entry
{
3112 VMChangeStateHandler
*cb
;
3114 QLIST_ENTRY (vm_change_state_entry
) entries
;
3117 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3119 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3122 VMChangeStateEntry
*e
;
3124 e
= qemu_mallocz(sizeof (*e
));
3128 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3132 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3134 QLIST_REMOVE (e
, entries
);
3138 static void vm_state_notify(int running
, int reason
)
3140 VMChangeStateEntry
*e
;
3142 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3143 e
->cb(e
->opaque
, running
, reason
);
3147 static void resume_all_vcpus(void);
3148 static void pause_all_vcpus(void);
3155 vm_state_notify(1, 0);
3156 qemu_rearm_alarm_timer(alarm_timer
);
3161 /* reset/shutdown handler */
3163 typedef struct QEMUResetEntry
{
3164 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3165 QEMUResetHandler
*func
;
3169 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3170 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3171 static int reset_requested
;
3172 static int shutdown_requested
;
3173 static int powerdown_requested
;
3174 static int debug_requested
;
3175 static int vmstop_requested
;
3177 int qemu_shutdown_requested(void)
3179 int r
= shutdown_requested
;
3180 shutdown_requested
= 0;
3184 int qemu_reset_requested(void)
3186 int r
= reset_requested
;
3187 reset_requested
= 0;
3191 int qemu_powerdown_requested(void)
3193 int r
= powerdown_requested
;
3194 powerdown_requested
= 0;
3198 static int qemu_debug_requested(void)
3200 int r
= debug_requested
;
3201 debug_requested
= 0;
3205 static int qemu_vmstop_requested(void)
3207 int r
= vmstop_requested
;
3208 vmstop_requested
= 0;
3212 static void do_vm_stop(int reason
)
3215 cpu_disable_ticks();
3218 vm_state_notify(0, reason
);
3222 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3224 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3227 re
->opaque
= opaque
;
3228 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3231 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3235 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3236 if (re
->func
== func
&& re
->opaque
== opaque
) {
3237 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3244 void qemu_system_reset(void)
3246 QEMUResetEntry
*re
, *nre
;
3248 /* reset all devices */
3249 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3250 re
->func(re
->opaque
);
3254 void qemu_system_reset_request(void)
3257 shutdown_requested
= 1;
3259 reset_requested
= 1;
3261 qemu_notify_event();
3264 void qemu_system_shutdown_request(void)
3266 shutdown_requested
= 1;
3267 qemu_notify_event();
3270 void qemu_system_powerdown_request(void)
3272 powerdown_requested
= 1;
3273 qemu_notify_event();
3276 #ifdef CONFIG_IOTHREAD
3277 static void qemu_system_vmstop_request(int reason
)
3279 vmstop_requested
= reason
;
3280 qemu_notify_event();
3285 static int io_thread_fd
= -1;
3287 static void qemu_event_increment(void)
3289 static const char byte
= 0;
3291 if (io_thread_fd
== -1)
3294 write(io_thread_fd
, &byte
, sizeof(byte
));
3297 static void qemu_event_read(void *opaque
)
3299 int fd
= (unsigned long)opaque
;
3302 /* Drain the notify pipe */
3305 len
= read(fd
, buffer
, sizeof(buffer
));
3306 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3309 static int qemu_event_init(void)
3318 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3322 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3326 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3327 (void *)(unsigned long)fds
[0]);
3329 io_thread_fd
= fds
[1];
3338 HANDLE qemu_event_handle
;
3340 static void dummy_event_handler(void *opaque
)
3344 static int qemu_event_init(void)
3346 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3347 if (!qemu_event_handle
) {
3348 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3351 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3355 static void qemu_event_increment(void)
3357 if (!SetEvent(qemu_event_handle
)) {
3358 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3365 static int cpu_can_run(CPUState
*env
)
3374 #ifndef CONFIG_IOTHREAD
3375 static int qemu_init_main_loop(void)
3377 return qemu_event_init();
3380 void qemu_init_vcpu(void *_env
)
3382 CPUState
*env
= _env
;
3386 env
->nr_cores
= smp_cores
;
3387 env
->nr_threads
= smp_threads
;
3391 int qemu_cpu_self(void *env
)
3396 static void resume_all_vcpus(void)
3400 static void pause_all_vcpus(void)
3404 void qemu_cpu_kick(void *env
)
3409 void qemu_notify_event(void)
3411 CPUState
*env
= cpu_single_env
;
3418 void qemu_mutex_lock_iothread(void) {}
3419 void qemu_mutex_unlock_iothread(void) {}
3421 void vm_stop(int reason
)
3426 #else /* CONFIG_IOTHREAD */
3428 #include "qemu-thread.h"
3430 QemuMutex qemu_global_mutex
;
3431 static QemuMutex qemu_fair_mutex
;
3433 static QemuThread io_thread
;
3435 static QemuThread
*tcg_cpu_thread
;
3436 static QemuCond
*tcg_halt_cond
;
3438 static int qemu_system_ready
;
3440 static QemuCond qemu_cpu_cond
;
3442 static QemuCond qemu_system_cond
;
3443 static QemuCond qemu_pause_cond
;
3445 static void block_io_signals(void);
3446 static void unblock_io_signals(void);
3447 static int tcg_has_work(void);
3449 static int qemu_init_main_loop(void)
3453 ret
= qemu_event_init();
3457 qemu_cond_init(&qemu_pause_cond
);
3458 qemu_mutex_init(&qemu_fair_mutex
);
3459 qemu_mutex_init(&qemu_global_mutex
);
3460 qemu_mutex_lock(&qemu_global_mutex
);
3462 unblock_io_signals();
3463 qemu_thread_self(&io_thread
);
3468 static void qemu_wait_io_event(CPUState
*env
)
3470 while (!tcg_has_work())
3471 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3473 qemu_mutex_unlock(&qemu_global_mutex
);
3476 * Users of qemu_global_mutex can be starved, having no chance
3477 * to acquire it since this path will get to it first.
3478 * So use another lock to provide fairness.
3480 qemu_mutex_lock(&qemu_fair_mutex
);
3481 qemu_mutex_unlock(&qemu_fair_mutex
);
3483 qemu_mutex_lock(&qemu_global_mutex
);
3487 qemu_cond_signal(&qemu_pause_cond
);
3491 static int qemu_cpu_exec(CPUState
*env
);
3493 static void *kvm_cpu_thread_fn(void *arg
)
3495 CPUState
*env
= arg
;
3498 qemu_thread_self(env
->thread
);
3502 /* signal CPU creation */
3503 qemu_mutex_lock(&qemu_global_mutex
);
3505 qemu_cond_signal(&qemu_cpu_cond
);
3507 /* and wait for machine initialization */
3508 while (!qemu_system_ready
)
3509 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3512 if (cpu_can_run(env
))
3514 qemu_wait_io_event(env
);
3520 static void tcg_cpu_exec(void);
3522 static void *tcg_cpu_thread_fn(void *arg
)
3524 CPUState
*env
= arg
;
3527 qemu_thread_self(env
->thread
);
3529 /* signal CPU creation */
3530 qemu_mutex_lock(&qemu_global_mutex
);
3531 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3533 qemu_cond_signal(&qemu_cpu_cond
);
3535 /* and wait for machine initialization */
3536 while (!qemu_system_ready
)
3537 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3541 qemu_wait_io_event(cur_cpu
);
3547 void qemu_cpu_kick(void *_env
)
3549 CPUState
*env
= _env
;
3550 qemu_cond_broadcast(env
->halt_cond
);
3552 qemu_thread_signal(env
->thread
, SIGUSR1
);
3555 int qemu_cpu_self(void *_env
)
3557 CPUState
*env
= _env
;
3560 qemu_thread_self(&this);
3562 return qemu_thread_equal(&this, env
->thread
);
3565 static void cpu_signal(int sig
)
3568 cpu_exit(cpu_single_env
);
3571 static void block_io_signals(void)
3574 struct sigaction sigact
;
3577 sigaddset(&set
, SIGUSR2
);
3578 sigaddset(&set
, SIGIO
);
3579 sigaddset(&set
, SIGALRM
);
3580 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3583 sigaddset(&set
, SIGUSR1
);
3584 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3586 memset(&sigact
, 0, sizeof(sigact
));
3587 sigact
.sa_handler
= cpu_signal
;
3588 sigaction(SIGUSR1
, &sigact
, NULL
);
3591 static void unblock_io_signals(void)
3596 sigaddset(&set
, SIGUSR2
);
3597 sigaddset(&set
, SIGIO
);
3598 sigaddset(&set
, SIGALRM
);
3599 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3602 sigaddset(&set
, SIGUSR1
);
3603 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3606 static void qemu_signal_lock(unsigned int msecs
)
3608 qemu_mutex_lock(&qemu_fair_mutex
);
3610 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3611 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3612 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3615 qemu_mutex_unlock(&qemu_fair_mutex
);
3618 void qemu_mutex_lock_iothread(void)
3620 if (kvm_enabled()) {
3621 qemu_mutex_lock(&qemu_fair_mutex
);
3622 qemu_mutex_lock(&qemu_global_mutex
);
3623 qemu_mutex_unlock(&qemu_fair_mutex
);
3625 qemu_signal_lock(100);
3628 void qemu_mutex_unlock_iothread(void)
3630 qemu_mutex_unlock(&qemu_global_mutex
);
3633 static int all_vcpus_paused(void)
3635 CPUState
*penv
= first_cpu
;
3640 penv
= (CPUState
*)penv
->next_cpu
;
3646 static void pause_all_vcpus(void)
3648 CPUState
*penv
= first_cpu
;
3652 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3653 qemu_cpu_kick(penv
);
3654 penv
= (CPUState
*)penv
->next_cpu
;
3657 while (!all_vcpus_paused()) {
3658 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3661 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3662 penv
= (CPUState
*)penv
->next_cpu
;
3667 static void resume_all_vcpus(void)
3669 CPUState
*penv
= first_cpu
;
3674 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3675 qemu_cpu_kick(penv
);
3676 penv
= (CPUState
*)penv
->next_cpu
;
3680 static void tcg_init_vcpu(void *_env
)
3682 CPUState
*env
= _env
;
3683 /* share a single thread for all cpus with TCG */
3684 if (!tcg_cpu_thread
) {
3685 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3686 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3687 qemu_cond_init(env
->halt_cond
);
3688 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3689 while (env
->created
== 0)
3690 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3691 tcg_cpu_thread
= env
->thread
;
3692 tcg_halt_cond
= env
->halt_cond
;
3694 env
->thread
= tcg_cpu_thread
;
3695 env
->halt_cond
= tcg_halt_cond
;
3699 static void kvm_start_vcpu(CPUState
*env
)
3701 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3702 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3703 qemu_cond_init(env
->halt_cond
);
3704 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3705 while (env
->created
== 0)
3706 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3709 void qemu_init_vcpu(void *_env
)
3711 CPUState
*env
= _env
;
3714 kvm_start_vcpu(env
);
3717 env
->nr_cores
= smp_cores
;
3718 env
->nr_threads
= smp_threads
;
3721 void qemu_notify_event(void)
3723 qemu_event_increment();
3726 void vm_stop(int reason
)
3729 qemu_thread_self(&me
);
3731 if (!qemu_thread_equal(&me
, &io_thread
)) {
3732 qemu_system_vmstop_request(reason
);
3734 * FIXME: should not return to device code in case
3735 * vm_stop() has been requested.
3737 if (cpu_single_env
) {
3738 cpu_exit(cpu_single_env
);
3739 cpu_single_env
->stop
= 1;
3750 static void host_main_loop_wait(int *timeout
)
3756 /* XXX: need to suppress polling by better using win32 events */
3758 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3759 ret
|= pe
->func(pe
->opaque
);
3763 WaitObjects
*w
= &wait_objects
;
3765 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3766 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3767 if (w
->func
[ret
- WAIT_OBJECT_0
])
3768 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3770 /* Check for additional signaled events */
3771 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3773 /* Check if event is signaled */
3774 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3775 if(ret2
== WAIT_OBJECT_0
) {
3777 w
->func
[i
](w
->opaque
[i
]);
3778 } else if (ret2
== WAIT_TIMEOUT
) {
3780 err
= GetLastError();
3781 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3784 } else if (ret
== WAIT_TIMEOUT
) {
3786 err
= GetLastError();
3787 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3794 static void host_main_loop_wait(int *timeout
)
3799 void main_loop_wait(int timeout
)
3801 IOHandlerRecord
*ioh
;
3802 fd_set rfds
, wfds
, xfds
;
3806 qemu_bh_update_timeout(&timeout
);
3808 host_main_loop_wait(&timeout
);
3810 /* poll any events */
3811 /* XXX: separate device handlers from system ones */
3816 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3820 (!ioh
->fd_read_poll
||
3821 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3822 FD_SET(ioh
->fd
, &rfds
);
3826 if (ioh
->fd_write
) {
3827 FD_SET(ioh
->fd
, &wfds
);
3833 tv
.tv_sec
= timeout
/ 1000;
3834 tv
.tv_usec
= (timeout
% 1000) * 1000;
3836 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3838 qemu_mutex_unlock_iothread();
3839 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3840 qemu_mutex_lock_iothread();
3842 IOHandlerRecord
**pioh
;
3844 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3845 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3846 ioh
->fd_read(ioh
->opaque
);
3848 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3849 ioh
->fd_write(ioh
->opaque
);
3853 /* remove deleted IO handlers */
3854 pioh
= &first_io_handler
;
3865 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3867 /* rearm timer, if not periodic */
3868 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3869 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3870 qemu_rearm_alarm_timer(alarm_timer
);
3873 /* vm time timers */
3875 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3876 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3877 qemu_get_clock(vm_clock
));
3880 /* real time timers */
3881 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3882 qemu_get_clock(rt_clock
));
3884 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3885 qemu_get_clock(host_clock
));
3887 /* Check bottom-halves last in case any of the earlier events triggered
3893 static int qemu_cpu_exec(CPUState
*env
)
3896 #ifdef CONFIG_PROFILER
3900 #ifdef CONFIG_PROFILER
3901 ti
= profile_getclock();
3906 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3907 env
->icount_decr
.u16
.low
= 0;
3908 env
->icount_extra
= 0;
3909 count
= qemu_next_deadline();
3910 count
= (count
+ (1 << icount_time_shift
) - 1)
3911 >> icount_time_shift
;
3912 qemu_icount
+= count
;
3913 decr
= (count
> 0xffff) ? 0xffff : count
;
3915 env
->icount_decr
.u16
.low
= decr
;
3916 env
->icount_extra
= count
;
3918 ret
= cpu_exec(env
);
3919 #ifdef CONFIG_PROFILER
3920 qemu_time
+= profile_getclock() - ti
;
3923 /* Fold pending instructions back into the
3924 instruction counter, and clear the interrupt flag. */
3925 qemu_icount
-= (env
->icount_decr
.u16
.low
3926 + env
->icount_extra
);
3927 env
->icount_decr
.u32
= 0;
3928 env
->icount_extra
= 0;
3933 static void tcg_cpu_exec(void)
3937 if (next_cpu
== NULL
)
3938 next_cpu
= first_cpu
;
3939 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3940 CPUState
*env
= cur_cpu
= next_cpu
;
3944 if (timer_alarm_pending
) {
3945 timer_alarm_pending
= 0;
3948 if (cpu_can_run(env
))
3949 ret
= qemu_cpu_exec(env
);
3950 if (ret
== EXCP_DEBUG
) {
3951 gdb_set_stop_cpu(env
);
3952 debug_requested
= 1;
3958 static int cpu_has_work(CPUState
*env
)
3966 if (qemu_cpu_has_work(env
))
3971 static int tcg_has_work(void)
3975 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3976 if (cpu_has_work(env
))
3981 static int qemu_calculate_timeout(void)
3983 #ifndef CONFIG_IOTHREAD
3988 else if (tcg_has_work())
3990 else if (!use_icount
)
3993 /* XXX: use timeout computed from timers */
3996 /* Advance virtual time to the next event. */
3997 if (use_icount
== 1) {
3998 /* When not using an adaptive execution frequency
3999 we tend to get badly out of sync with real time,
4000 so just delay for a reasonable amount of time. */
4003 delta
= cpu_get_icount() - cpu_get_clock();
4006 /* If virtual time is ahead of real time then just
4008 timeout
= (delta
/ 1000000) + 1;
4010 /* Wait for either IO to occur or the next
4012 add
= qemu_next_deadline();
4013 /* We advance the timer before checking for IO.
4014 Limit the amount we advance so that early IO
4015 activity won't get the guest too far ahead. */
4019 add
= (add
+ (1 << icount_time_shift
) - 1)
4020 >> icount_time_shift
;
4022 timeout
= delta
/ 1000000;
4029 #else /* CONFIG_IOTHREAD */
4034 static int vm_can_run(void)
4036 if (powerdown_requested
)
4038 if (reset_requested
)
4040 if (shutdown_requested
)
4042 if (debug_requested
)
4047 qemu_irq qemu_system_powerdown
;
4049 static void main_loop(void)
4053 #ifdef CONFIG_IOTHREAD
4054 qemu_system_ready
= 1;
4055 qemu_cond_broadcast(&qemu_system_cond
);
4060 #ifdef CONFIG_PROFILER
4063 #ifndef CONFIG_IOTHREAD
4066 #ifdef CONFIG_PROFILER
4067 ti
= profile_getclock();
4069 main_loop_wait(qemu_calculate_timeout());
4070 #ifdef CONFIG_PROFILER
4071 dev_time
+= profile_getclock() - ti
;
4073 } while (vm_can_run());
4075 if (qemu_debug_requested()) {
4076 monitor_protocol_event(EVENT_DEBUG
, NULL
);
4077 vm_stop(EXCP_DEBUG
);
4079 if (qemu_shutdown_requested()) {
4080 monitor_protocol_event(EVENT_SHUTDOWN
, NULL
);
4087 if (qemu_reset_requested()) {
4088 monitor_protocol_event(EVENT_RESET
, NULL
);
4090 qemu_system_reset();
4093 if (qemu_powerdown_requested()) {
4094 monitor_protocol_event(EVENT_POWERDOWN
, NULL
);
4095 qemu_irq_raise(qemu_system_powerdown
);
4097 if ((r
= qemu_vmstop_requested())) {
4098 monitor_protocol_event(EVENT_STOP
, NULL
);
4105 static void version(void)
4107 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4110 static void help(int exitcode
)
4113 printf("usage: %s [options] [disk_image]\n"
4115 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4117 #define DEF(option, opt_arg, opt_enum, opt_help) \
4119 #define DEFHEADING(text) stringify(text) "\n"
4120 #include "qemu-options.h"
4125 "During emulation, the following keys are useful:\n"
4126 "ctrl-alt-f toggle full screen\n"
4127 "ctrl-alt-n switch to virtual console 'n'\n"
4128 "ctrl-alt toggle mouse and keyboard grab\n"
4130 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4135 DEFAULT_NETWORK_SCRIPT
,
4136 DEFAULT_NETWORK_DOWN_SCRIPT
,
4138 DEFAULT_GDBSTUB_PORT
,
4143 #define HAS_ARG 0x0001
4146 #define DEF(option, opt_arg, opt_enum, opt_help) \
4148 #define DEFHEADING(text)
4149 #include "qemu-options.h"
4155 typedef struct QEMUOption
{
4161 static const QEMUOption qemu_options
[] = {
4162 { "h", 0, QEMU_OPTION_h
},
4163 #define DEF(option, opt_arg, opt_enum, opt_help) \
4164 { option, opt_arg, opt_enum },
4165 #define DEFHEADING(text)
4166 #include "qemu-options.h"
4174 struct soundhw soundhw
[] = {
4175 #ifdef HAS_AUDIO_CHOICE
4176 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4182 { .init_isa
= pcspk_audio_init
}
4189 "Creative Sound Blaster 16",
4192 { .init_isa
= SB16_init
}
4196 #ifdef CONFIG_CS4231A
4202 { .init_isa
= cs4231a_init
}
4210 "Yamaha YMF262 (OPL3)",
4212 "Yamaha YM3812 (OPL2)",
4216 { .init_isa
= Adlib_init
}
4223 "Gravis Ultrasound GF1",
4226 { .init_isa
= GUS_init
}
4233 "Intel 82801AA AC97 Audio",
4236 { .init_pci
= ac97_init
}
4240 #ifdef CONFIG_ES1370
4243 "ENSONIQ AudioPCI ES1370",
4246 { .init_pci
= es1370_init
}
4250 #endif /* HAS_AUDIO_CHOICE */
4252 { NULL
, NULL
, 0, 0, { NULL
} }
4255 static void select_soundhw (const char *optarg
)
4259 if (*optarg
== '?') {
4262 printf ("Valid sound card names (comma separated):\n");
4263 for (c
= soundhw
; c
->name
; ++c
) {
4264 printf ("%-11s %s\n", c
->name
, c
->descr
);
4266 printf ("\n-soundhw all will enable all of the above\n");
4267 exit (*optarg
!= '?');
4275 if (!strcmp (optarg
, "all")) {
4276 for (c
= soundhw
; c
->name
; ++c
) {
4284 e
= strchr (p
, ',');
4285 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4287 for (c
= soundhw
; c
->name
; ++c
) {
4288 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4297 "Unknown sound card name (too big to show)\n");
4300 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4305 p
+= l
+ (e
!= NULL
);
4309 goto show_valid_cards
;
4314 static void select_vgahw (const char *p
)
4318 vga_interface_type
= VGA_NONE
;
4319 if (strstart(p
, "std", &opts
)) {
4320 vga_interface_type
= VGA_STD
;
4321 } else if (strstart(p
, "cirrus", &opts
)) {
4322 vga_interface_type
= VGA_CIRRUS
;
4323 } else if (strstart(p
, "vmware", &opts
)) {
4324 vga_interface_type
= VGA_VMWARE
;
4325 } else if (strstart(p
, "xenfb", &opts
)) {
4326 vga_interface_type
= VGA_XENFB
;
4327 } else if (!strstart(p
, "none", &opts
)) {
4329 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4333 const char *nextopt
;
4335 if (strstart(opts
, ",retrace=", &nextopt
)) {
4337 if (strstart(opts
, "dumb", &nextopt
))
4338 vga_retrace_method
= VGA_RETRACE_DUMB
;
4339 else if (strstart(opts
, "precise", &nextopt
))
4340 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4341 else goto invalid_vga
;
4342 } else goto invalid_vga
;
4348 static int balloon_parse(const char *arg
)
4352 if (strcmp(arg
, "none") == 0) {
4356 if (!strncmp(arg
, "virtio", 6)) {
4357 if (arg
[6] == ',') {
4358 /* have params -> parse them */
4359 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4363 /* create empty opts */
4364 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4366 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4375 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4377 exit(STATUS_CONTROL_C_EXIT
);
4382 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4386 if(strlen(str
) != 36)
4389 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4390 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4391 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4397 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4405 static void termsig_handler(int signal
)
4407 qemu_system_shutdown_request();
4410 static void sigchld_handler(int signal
)
4412 waitpid(-1, NULL
, WNOHANG
);
4415 static void sighandler_setup(void)
4417 struct sigaction act
;
4419 memset(&act
, 0, sizeof(act
));
4420 act
.sa_handler
= termsig_handler
;
4421 sigaction(SIGINT
, &act
, NULL
);
4422 sigaction(SIGHUP
, &act
, NULL
);
4423 sigaction(SIGTERM
, &act
, NULL
);
4425 act
.sa_handler
= sigchld_handler
;
4426 act
.sa_flags
= SA_NOCLDSTOP
;
4427 sigaction(SIGCHLD
, &act
, NULL
);
4433 /* Look for support files in the same directory as the executable. */
4434 static char *find_datadir(const char *argv0
)
4440 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4447 while (p
!= buf
&& *p
!= '\\')
4450 if (access(buf
, R_OK
) == 0) {
4451 return qemu_strdup(buf
);
4457 /* Find a likely location for support files using the location of the binary.
4458 For installed binaries this will be "$bindir/../share/qemu". When
4459 running from the build tree this will be "$bindir/../pc-bios". */
4460 #define SHARE_SUFFIX "/share/qemu"
4461 #define BUILD_SUFFIX "/pc-bios"
4462 static char *find_datadir(const char *argv0
)
4470 #if defined(__linux__)
4473 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4479 #elif defined(__FreeBSD__)
4482 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4489 /* If we don't have any way of figuring out the actual executable
4490 location then try argv[0]. */
4492 p
= realpath(argv0
, buf
);
4500 max_len
= strlen(dir
) +
4501 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4502 res
= qemu_mallocz(max_len
);
4503 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4504 if (access(res
, R_OK
)) {
4505 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4506 if (access(res
, R_OK
)) {
4518 char *qemu_find_file(int type
, const char *name
)
4524 /* If name contains path separators then try it as a straight path. */
4525 if ((strchr(name
, '/') || strchr(name
, '\\'))
4526 && access(name
, R_OK
) == 0) {
4527 return qemu_strdup(name
);
4530 case QEMU_FILE_TYPE_BIOS
:
4533 case QEMU_FILE_TYPE_KEYMAP
:
4534 subdir
= "keymaps/";
4539 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4540 buf
= qemu_mallocz(len
);
4541 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4542 if (access(buf
, R_OK
)) {
4549 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4553 dev
= qdev_device_add(opts
);
4559 struct device_config
{
4561 DEV_USB
, /* -usbdevice */
4564 const char *cmdline
;
4565 QTAILQ_ENTRY(device_config
) next
;
4567 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4569 static void add_device_config(int type
, const char *cmdline
)
4571 struct device_config
*conf
;
4573 conf
= qemu_mallocz(sizeof(*conf
));
4575 conf
->cmdline
= cmdline
;
4576 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4579 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4581 struct device_config
*conf
;
4584 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4585 if (conf
->type
!= type
)
4587 rc
= func(conf
->cmdline
);
4594 int main(int argc
, char **argv
, char **envp
)
4596 const char *gdbstub_dev
= NULL
;
4597 uint32_t boot_devices_bitmap
= 0;
4599 int snapshot
, linux_boot
, net_boot
;
4600 const char *initrd_filename
;
4601 const char *kernel_filename
, *kernel_cmdline
;
4602 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4604 DisplayChangeListener
*dcl
;
4605 int cyls
, heads
, secs
, translation
;
4606 QemuOpts
*hda_opts
= NULL
, *opts
;
4608 const char *r
, *optarg
;
4609 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4610 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4611 int monitor_flags
[MAX_MONITOR_DEVICES
];
4612 int monitor_device_index
;
4613 const char *serial_devices
[MAX_SERIAL_PORTS
];
4614 int serial_device_index
;
4615 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4616 int parallel_device_index
;
4617 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4618 int virtio_console_index
;
4619 const char *loadvm
= NULL
;
4620 QEMUMachine
*machine
;
4621 const char *cpu_model
;
4626 const char *pid_file
= NULL
;
4627 const char *incoming
= NULL
;
4630 struct passwd
*pwd
= NULL
;
4631 const char *chroot_dir
= NULL
;
4632 const char *run_as
= NULL
;
4635 int show_vnc_port
= 0;
4639 qemu_errors_to_file(stderr
);
4640 qemu_cache_utils_init(envp
);
4642 QLIST_INIT (&vm_change_state_head
);
4645 struct sigaction act
;
4646 sigfillset(&act
.sa_mask
);
4648 act
.sa_handler
= SIG_IGN
;
4649 sigaction(SIGPIPE
, &act
, NULL
);
4652 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4653 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4654 QEMU to run on a single CPU */
4659 h
= GetCurrentProcess();
4660 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4661 for(i
= 0; i
< 32; i
++) {
4662 if (mask
& (1 << i
))
4667 SetProcessAffinityMask(h
, mask
);
4673 module_call_init(MODULE_INIT_MACHINE
);
4674 machine
= find_default_machine();
4676 initrd_filename
= NULL
;
4679 kernel_filename
= NULL
;
4680 kernel_cmdline
= "";
4681 cyls
= heads
= secs
= 0;
4682 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4684 serial_devices
[0] = "vc:80Cx24C";
4685 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4686 serial_devices
[i
] = NULL
;
4687 serial_device_index
= 0;
4689 parallel_devices
[0] = "vc:80Cx24C";
4690 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4691 parallel_devices
[i
] = NULL
;
4692 parallel_device_index
= 0;
4694 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4695 virtio_consoles
[i
] = NULL
;
4696 virtio_console_index
= 0;
4698 monitor_devices
[0] = "vc:80Cx24C";
4699 monitor_flags
[0] = MONITOR_IS_DEFAULT
| MONITOR_USE_READLINE
;
4700 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4701 monitor_devices
[i
] = NULL
;
4702 monitor_flags
[i
] = MONITOR_USE_READLINE
;
4704 monitor_device_index
= 0;
4706 for (i
= 0; i
< MAX_NODES
; i
++) {
4708 node_cpumask
[i
] = 0;
4723 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4725 const QEMUOption
*popt
;
4728 /* Treat --foo the same as -foo. */
4731 popt
= qemu_options
;
4734 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4738 if (!strcmp(popt
->name
, r
+ 1))
4742 if (popt
->flags
& HAS_ARG
) {
4743 if (optind
>= argc
) {
4744 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4748 optarg
= argv
[optind
++];
4753 switch(popt
->index
) {
4755 machine
= find_machine(optarg
);
4758 printf("Supported machines are:\n");
4759 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4761 printf("%-10s %s (alias of %s)\n",
4762 m
->alias
, m
->desc
, m
->name
);
4763 printf("%-10s %s%s\n",
4765 m
->is_default
? " (default)" : "");
4767 exit(*optarg
!= '?');
4770 case QEMU_OPTION_cpu
:
4771 /* hw initialization will check this */
4772 if (*optarg
== '?') {
4773 /* XXX: implement xxx_cpu_list for targets that still miss it */
4774 #if defined(cpu_list)
4775 cpu_list(stdout
, &fprintf
);
4782 case QEMU_OPTION_initrd
:
4783 initrd_filename
= optarg
;
4785 case QEMU_OPTION_hda
:
4787 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4789 hda_opts
= drive_add(optarg
, HD_ALIAS
4790 ",cyls=%d,heads=%d,secs=%d%s",
4791 0, cyls
, heads
, secs
,
4792 translation
== BIOS_ATA_TRANSLATION_LBA
?
4794 translation
== BIOS_ATA_TRANSLATION_NONE
?
4795 ",trans=none" : "");
4797 case QEMU_OPTION_hdb
:
4798 case QEMU_OPTION_hdc
:
4799 case QEMU_OPTION_hdd
:
4800 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4802 case QEMU_OPTION_drive
:
4803 drive_add(NULL
, "%s", optarg
);
4805 case QEMU_OPTION_set
:
4806 if (qemu_set_option(optarg
) != 0)
4809 case QEMU_OPTION_mtdblock
:
4810 drive_add(optarg
, MTD_ALIAS
);
4812 case QEMU_OPTION_sd
:
4813 drive_add(optarg
, SD_ALIAS
);
4815 case QEMU_OPTION_pflash
:
4816 drive_add(optarg
, PFLASH_ALIAS
);
4818 case QEMU_OPTION_snapshot
:
4821 case QEMU_OPTION_hdachs
:
4825 cyls
= strtol(p
, (char **)&p
, 0);
4826 if (cyls
< 1 || cyls
> 16383)
4831 heads
= strtol(p
, (char **)&p
, 0);
4832 if (heads
< 1 || heads
> 16)
4837 secs
= strtol(p
, (char **)&p
, 0);
4838 if (secs
< 1 || secs
> 63)
4842 if (!strcmp(p
, "none"))
4843 translation
= BIOS_ATA_TRANSLATION_NONE
;
4844 else if (!strcmp(p
, "lba"))
4845 translation
= BIOS_ATA_TRANSLATION_LBA
;
4846 else if (!strcmp(p
, "auto"))
4847 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4850 } else if (*p
!= '\0') {
4852 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4855 if (hda_opts
!= NULL
) {
4857 snprintf(num
, sizeof(num
), "%d", cyls
);
4858 qemu_opt_set(hda_opts
, "cyls", num
);
4859 snprintf(num
, sizeof(num
), "%d", heads
);
4860 qemu_opt_set(hda_opts
, "heads", num
);
4861 snprintf(num
, sizeof(num
), "%d", secs
);
4862 qemu_opt_set(hda_opts
, "secs", num
);
4863 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4864 qemu_opt_set(hda_opts
, "trans", "lba");
4865 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4866 qemu_opt_set(hda_opts
, "trans", "none");
4870 case QEMU_OPTION_numa
:
4871 if (nb_numa_nodes
>= MAX_NODES
) {
4872 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4877 case QEMU_OPTION_nographic
:
4878 display_type
= DT_NOGRAPHIC
;
4880 #ifdef CONFIG_CURSES
4881 case QEMU_OPTION_curses
:
4882 display_type
= DT_CURSES
;
4885 case QEMU_OPTION_portrait
:
4888 case QEMU_OPTION_kernel
:
4889 kernel_filename
= optarg
;
4891 case QEMU_OPTION_append
:
4892 kernel_cmdline
= optarg
;
4894 case QEMU_OPTION_cdrom
:
4895 drive_add(optarg
, CDROM_ALIAS
);
4897 case QEMU_OPTION_boot
:
4899 static const char * const params
[] = {
4900 "order", "once", "menu", NULL
4902 char buf
[sizeof(boot_devices
)];
4903 char *standard_boot_devices
;
4906 if (!strchr(optarg
, '=')) {
4908 pstrcpy(buf
, sizeof(buf
), optarg
);
4909 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4911 "qemu: unknown boot parameter '%s' in '%s'\n",
4917 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4918 boot_devices_bitmap
= parse_bootdevices(buf
);
4919 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4922 if (get_param_value(buf
, sizeof(buf
),
4924 boot_devices_bitmap
|= parse_bootdevices(buf
);
4925 standard_boot_devices
= qemu_strdup(boot_devices
);
4926 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4927 qemu_register_reset(restore_boot_devices
,
4928 standard_boot_devices
);
4930 if (get_param_value(buf
, sizeof(buf
),
4932 if (!strcmp(buf
, "on")) {
4934 } else if (!strcmp(buf
, "off")) {
4938 "qemu: invalid option value '%s'\n",
4946 case QEMU_OPTION_fda
:
4947 case QEMU_OPTION_fdb
:
4948 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4951 case QEMU_OPTION_no_fd_bootchk
:
4955 case QEMU_OPTION_netdev
:
4956 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4960 case QEMU_OPTION_net
:
4961 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4966 case QEMU_OPTION_tftp
:
4967 legacy_tftp_prefix
= optarg
;
4969 case QEMU_OPTION_bootp
:
4970 legacy_bootp_filename
= optarg
;
4973 case QEMU_OPTION_smb
:
4974 if (net_slirp_smb(optarg
) < 0)
4978 case QEMU_OPTION_redir
:
4979 if (net_slirp_redir(optarg
) < 0)
4983 case QEMU_OPTION_bt
:
4984 add_device_config(DEV_BT
, optarg
);
4987 case QEMU_OPTION_audio_help
:
4991 case QEMU_OPTION_soundhw
:
4992 select_soundhw (optarg
);
4998 case QEMU_OPTION_version
:
5002 case QEMU_OPTION_m
: {
5006 value
= strtoul(optarg
, &ptr
, 10);
5008 case 0: case 'M': case 'm':
5015 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5019 /* On 32-bit hosts, QEMU is limited by virtual address space */
5020 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5021 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5024 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5025 fprintf(stderr
, "qemu: ram size too large\n");
5034 const CPULogItem
*item
;
5036 mask
= cpu_str_to_log_mask(optarg
);
5038 printf("Log items (comma separated):\n");
5039 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5040 printf("%-10s %s\n", item
->name
, item
->help
);
5048 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5050 case QEMU_OPTION_gdb
:
5051 gdbstub_dev
= optarg
;
5056 case QEMU_OPTION_bios
:
5059 case QEMU_OPTION_singlestep
:
5066 keyboard_layout
= optarg
;
5068 case QEMU_OPTION_localtime
:
5071 case QEMU_OPTION_vga
:
5072 select_vgahw (optarg
);
5074 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5080 w
= strtol(p
, (char **)&p
, 10);
5083 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5089 h
= strtol(p
, (char **)&p
, 10);
5094 depth
= strtol(p
, (char **)&p
, 10);
5095 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5096 depth
!= 24 && depth
!= 32)
5098 } else if (*p
== '\0') {
5099 depth
= graphic_depth
;
5106 graphic_depth
= depth
;
5110 case QEMU_OPTION_echr
:
5113 term_escape_char
= strtol(optarg
, &r
, 0);
5115 printf("Bad argument to echr\n");
5118 case QEMU_OPTION_monitor
:
5119 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5120 fprintf(stderr
, "qemu: too many monitor devices\n");
5123 monitor_devices
[monitor_device_index
] =
5124 monitor_cmdline_parse(optarg
,
5125 &monitor_flags
[monitor_device_index
]);
5126 monitor_device_index
++;
5128 case QEMU_OPTION_chardev
:
5129 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5131 fprintf(stderr
, "parse error: %s\n", optarg
);
5134 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5138 case QEMU_OPTION_serial
:
5139 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5140 fprintf(stderr
, "qemu: too many serial ports\n");
5143 serial_devices
[serial_device_index
] = optarg
;
5144 serial_device_index
++;
5146 case QEMU_OPTION_watchdog
:
5149 "qemu: only one watchdog option may be given\n");
5154 case QEMU_OPTION_watchdog_action
:
5155 if (select_watchdog_action(optarg
) == -1) {
5156 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5160 case QEMU_OPTION_virtiocon
:
5161 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5162 fprintf(stderr
, "qemu: too many virtio consoles\n");
5165 virtio_consoles
[virtio_console_index
] = optarg
;
5166 virtio_console_index
++;
5168 case QEMU_OPTION_parallel
:
5169 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5170 fprintf(stderr
, "qemu: too many parallel ports\n");
5173 parallel_devices
[parallel_device_index
] = optarg
;
5174 parallel_device_index
++;
5176 case QEMU_OPTION_loadvm
:
5179 case QEMU_OPTION_full_screen
:
5183 case QEMU_OPTION_no_frame
:
5186 case QEMU_OPTION_alt_grab
:
5189 case QEMU_OPTION_ctrl_grab
:
5192 case QEMU_OPTION_no_quit
:
5195 case QEMU_OPTION_sdl
:
5196 display_type
= DT_SDL
;
5199 case QEMU_OPTION_pidfile
:
5203 case QEMU_OPTION_win2k_hack
:
5204 win2k_install_hack
= 1;
5206 case QEMU_OPTION_rtc_td_hack
:
5209 case QEMU_OPTION_acpitable
:
5210 if(acpi_table_add(optarg
) < 0) {
5211 fprintf(stderr
, "Wrong acpi table provided\n");
5215 case QEMU_OPTION_smbios
:
5216 if(smbios_entry_add(optarg
) < 0) {
5217 fprintf(stderr
, "Wrong smbios provided\n");
5223 case QEMU_OPTION_enable_kvm
:
5227 case QEMU_OPTION_usb
:
5230 case QEMU_OPTION_usbdevice
:
5232 add_device_config(DEV_USB
, optarg
);
5234 case QEMU_OPTION_device
:
5235 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5239 case QEMU_OPTION_smp
:
5242 fprintf(stderr
, "Invalid number of CPUs\n");
5245 if (max_cpus
< smp_cpus
) {
5246 fprintf(stderr
, "maxcpus must be equal to or greater than "
5250 if (max_cpus
> 255) {
5251 fprintf(stderr
, "Unsupported number of maxcpus\n");
5255 case QEMU_OPTION_vnc
:
5256 display_type
= DT_VNC
;
5257 vnc_display
= optarg
;
5260 case QEMU_OPTION_no_acpi
:
5263 case QEMU_OPTION_no_hpet
:
5266 case QEMU_OPTION_balloon
:
5267 if (balloon_parse(optarg
) < 0) {
5268 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5273 case QEMU_OPTION_no_reboot
:
5276 case QEMU_OPTION_no_shutdown
:
5279 case QEMU_OPTION_show_cursor
:
5282 case QEMU_OPTION_uuid
:
5283 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5284 fprintf(stderr
, "Fail to parse UUID string."
5285 " Wrong format.\n");
5290 case QEMU_OPTION_daemonize
:
5294 case QEMU_OPTION_option_rom
:
5295 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5296 fprintf(stderr
, "Too many option ROMs\n");
5299 option_rom
[nb_option_roms
] = optarg
;
5302 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5303 case QEMU_OPTION_semihosting
:
5304 semihosting_enabled
= 1;
5307 case QEMU_OPTION_name
:
5308 qemu_name
= qemu_strdup(optarg
);
5310 char *p
= strchr(qemu_name
, ',');
5313 if (strncmp(p
, "process=", 8)) {
5314 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5322 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5323 case QEMU_OPTION_prom_env
:
5324 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5325 fprintf(stderr
, "Too many prom variables\n");
5328 prom_envs
[nb_prom_envs
] = optarg
;
5333 case QEMU_OPTION_old_param
:
5337 case QEMU_OPTION_clock
:
5338 configure_alarms(optarg
);
5340 case QEMU_OPTION_startdate
:
5341 configure_rtc_date_offset(optarg
, 1);
5343 case QEMU_OPTION_rtc
:
5344 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5346 fprintf(stderr
, "parse error: %s\n", optarg
);
5349 configure_rtc(opts
);
5351 case QEMU_OPTION_tb_size
:
5352 tb_size
= strtol(optarg
, NULL
, 0);
5356 case QEMU_OPTION_icount
:
5358 if (strcmp(optarg
, "auto") == 0) {
5359 icount_time_shift
= -1;
5361 icount_time_shift
= strtol(optarg
, NULL
, 0);
5364 case QEMU_OPTION_incoming
:
5368 case QEMU_OPTION_chroot
:
5369 chroot_dir
= optarg
;
5371 case QEMU_OPTION_runas
:
5376 case QEMU_OPTION_xen_domid
:
5377 xen_domid
= atoi(optarg
);
5379 case QEMU_OPTION_xen_create
:
5380 xen_mode
= XEN_CREATE
;
5382 case QEMU_OPTION_xen_attach
:
5383 xen_mode
= XEN_ATTACH
;
5386 case QEMU_OPTION_readconfig
:
5389 fp
= fopen(optarg
, "r");
5391 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5394 if (qemu_config_parse(fp
) != 0) {
5400 case QEMU_OPTION_writeconfig
:
5403 if (strcmp(optarg
, "-") == 0) {
5406 fp
= fopen(optarg
, "w");
5408 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5412 qemu_config_write(fp
);
5420 /* If no data_dir is specified then try to find it relative to the
5423 data_dir
= find_datadir(argv
[0]);
5425 /* If all else fails use the install patch specified when building. */
5427 data_dir
= CONFIG_QEMU_SHAREDIR
;
5431 * Default to max_cpus = smp_cpus, in case the user doesn't
5432 * specify a max_cpus value.
5435 max_cpus
= smp_cpus
;
5437 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5438 if (smp_cpus
> machine
->max_cpus
) {
5439 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5440 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5445 if (display_type
== DT_NOGRAPHIC
) {
5446 if (serial_device_index
== 0)
5447 serial_devices
[0] = "stdio";
5448 if (parallel_device_index
== 0)
5449 parallel_devices
[0] = "null";
5450 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5451 monitor_devices
[0] = "stdio";
5459 if (pipe(fds
) == -1)
5470 len
= read(fds
[0], &status
, 1);
5471 if (len
== -1 && (errno
== EINTR
))
5476 else if (status
== 1) {
5477 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5494 signal(SIGTSTP
, SIG_IGN
);
5495 signal(SIGTTOU
, SIG_IGN
);
5496 signal(SIGTTIN
, SIG_IGN
);
5499 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5502 write(fds
[1], &status
, 1);
5504 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5509 if (kvm_enabled()) {
5512 ret
= kvm_init(smp_cpus
);
5514 fprintf(stderr
, "failed to initialize KVM\n");
5519 if (qemu_init_main_loop()) {
5520 fprintf(stderr
, "qemu_init_main_loop failed\n");
5523 linux_boot
= (kernel_filename
!= NULL
);
5525 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5526 fprintf(stderr
, "-append only allowed with -kernel option\n");
5530 if (!linux_boot
&& initrd_filename
!= NULL
) {
5531 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5536 /* Win32 doesn't support line-buffering and requires size >= 2 */
5537 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5540 if (init_timer_alarm() < 0) {
5541 fprintf(stderr
, "could not initialize alarm timer\n");
5544 if (use_icount
&& icount_time_shift
< 0) {
5546 /* 125MIPS seems a reasonable initial guess at the guest speed.
5547 It will be corrected fairly quickly anyway. */
5548 icount_time_shift
= 3;
5549 init_icount_adjust();
5556 if (net_init_clients() < 0) {
5560 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5561 net_set_boot_mask(net_boot
);
5563 /* init the bluetooth world */
5564 if (foreach_device_config(DEV_BT
, bt_parse
))
5567 /* init the memory */
5569 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5571 /* init the dynamic translator */
5572 cpu_exec_init_all(tb_size
* 1024 * 1024);
5574 bdrv_init_with_whitelist();
5578 /* we always create the cdrom drive, even if no disk is there */
5579 drive_add(NULL
, CDROM_ALIAS
);
5581 /* we always create at least one floppy */
5582 drive_add(NULL
, FD_ALIAS
, 0);
5584 /* we always create one sd slot, even if no card is in it */
5585 drive_add(NULL
, SD_ALIAS
);
5587 /* open the virtual block devices */
5589 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5590 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5593 vmstate_register(0, &vmstate_timers
,&timers_state
);
5594 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5597 /* Maintain compatibility with multiple stdio monitors */
5598 if (!strcmp(monitor_devices
[0],"stdio")) {
5599 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5600 const char *devname
= serial_devices
[i
];
5601 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5602 monitor_devices
[0] = NULL
;
5604 } else if (devname
&& !strcmp(devname
,"stdio")) {
5605 monitor_devices
[0] = NULL
;
5606 serial_devices
[i
] = "mon:stdio";
5612 if (nb_numa_nodes
> 0) {
5615 if (nb_numa_nodes
> smp_cpus
) {
5616 nb_numa_nodes
= smp_cpus
;
5619 /* If no memory size if given for any node, assume the default case
5620 * and distribute the available memory equally across all nodes
5622 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5623 if (node_mem
[i
] != 0)
5626 if (i
== nb_numa_nodes
) {
5627 uint64_t usedmem
= 0;
5629 /* On Linux, the each node's border has to be 8MB aligned,
5630 * the final node gets the rest.
5632 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5633 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5634 usedmem
+= node_mem
[i
];
5636 node_mem
[i
] = ram_size
- usedmem
;
5639 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5640 if (node_cpumask
[i
] != 0)
5643 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5644 * must cope with this anyway, because there are BIOSes out there in
5645 * real machines which also use this scheme.
5647 if (i
== nb_numa_nodes
) {
5648 for (i
= 0; i
< smp_cpus
; i
++) {
5649 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5654 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5655 const char *devname
= monitor_devices
[i
];
5656 if (devname
&& strcmp(devname
, "none")) {
5659 snprintf(label
, sizeof(label
), "monitor");
5661 snprintf(label
, sizeof(label
), "monitor%d", i
);
5663 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5664 if (!monitor_hds
[i
]) {
5665 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5672 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5673 const char *devname
= serial_devices
[i
];
5674 if (devname
&& strcmp(devname
, "none")) {
5676 snprintf(label
, sizeof(label
), "serial%d", i
);
5677 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5678 if (!serial_hds
[i
]) {
5679 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
5680 devname
, strerror(errno
));
5686 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5687 const char *devname
= parallel_devices
[i
];
5688 if (devname
&& strcmp(devname
, "none")) {
5690 snprintf(label
, sizeof(label
), "parallel%d", i
);
5691 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5692 if (!parallel_hds
[i
]) {
5693 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5694 devname
, strerror(errno
));
5700 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5701 const char *devname
= virtio_consoles
[i
];
5702 if (devname
&& strcmp(devname
, "none")) {
5704 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5705 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5706 if (!virtcon_hds
[i
]) {
5707 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5708 devname
, strerror(errno
));
5714 module_call_init(MODULE_INIT_DEVICE
);
5717 i
= select_watchdog(watchdog
);
5719 exit (i
== 1 ? 1 : 0);
5722 if (machine
->compat_props
) {
5723 qdev_prop_register_compat(machine
->compat_props
);
5725 machine
->init(ram_size
, boot_devices
,
5726 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5730 /* must be after terminal init, SDL library changes signal handlers */
5734 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5735 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5736 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5742 current_machine
= machine
;
5744 /* init USB devices */
5746 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5750 /* init generic devices */
5751 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5755 dumb_display_init();
5756 /* just use the first displaystate for the moment */
5759 if (display_type
== DT_DEFAULT
) {
5760 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5761 display_type
= DT_SDL
;
5763 display_type
= DT_VNC
;
5764 vnc_display
= "localhost:0,to=99";
5770 switch (display_type
) {
5773 #if defined(CONFIG_CURSES)
5775 curses_display_init(ds
, full_screen
);
5778 #if defined(CONFIG_SDL)
5780 sdl_display_init(ds
, full_screen
, no_frame
);
5782 #elif defined(CONFIG_COCOA)
5784 cocoa_display_init(ds
, full_screen
);
5788 vnc_display_init(ds
);
5789 if (vnc_display_open(ds
, vnc_display
) < 0)
5792 if (show_vnc_port
) {
5793 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5801 dcl
= ds
->listeners
;
5802 while (dcl
!= NULL
) {
5803 if (dcl
->dpy_refresh
!= NULL
) {
5804 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5805 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5810 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5811 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5812 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5815 text_consoles_set_display(display_state
);
5817 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5818 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5819 monitor_init(monitor_hds
[i
], monitor_flags
[i
]);
5823 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5824 const char *devname
= serial_devices
[i
];
5825 if (devname
&& strcmp(devname
, "none")) {
5826 if (strstart(devname
, "vc", 0))
5827 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5831 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5832 const char *devname
= parallel_devices
[i
];
5833 if (devname
&& strcmp(devname
, "none")) {
5834 if (strstart(devname
, "vc", 0))
5835 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5839 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5840 const char *devname
= virtio_consoles
[i
];
5841 if (virtcon_hds
[i
] && devname
) {
5842 if (strstart(devname
, "vc", 0))
5843 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5847 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5848 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5853 qdev_machine_creation_done();
5857 qemu_system_reset();
5859 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5865 qemu_start_incoming_migration(incoming
);
5866 } else if (autostart
) {
5876 len
= write(fds
[1], &status
, 1);
5877 if (len
== -1 && (errno
== EINTR
))
5884 TFR(fd
= open("/dev/null", O_RDWR
));
5890 pwd
= getpwnam(run_as
);
5892 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5898 if (chroot(chroot_dir
) < 0) {
5899 fprintf(stderr
, "chroot failed\n");
5906 if (setgid(pwd
->pw_gid
) < 0) {
5907 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5910 if (setuid(pwd
->pw_uid
) < 0) {
5911 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5914 if (setuid(0) != -1) {
5915 fprintf(stderr
, "Dropping privileges failed\n");