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"
145 #include "qemu-timer.h"
146 #include "qemu-char.h"
147 #include "cache-utils.h"
149 #include "block_int.h"
150 #include "block-migration.h"
152 #include "audio/audio.h"
153 #include "migration.h"
156 #include "qemu-option.h"
157 #include "qemu-config.h"
161 #include "exec-all.h"
163 #include "qemu_socket.h"
165 #include "slirp/libslirp.h"
167 #include "qemu-queue.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 /* Maximum number of monitor devices */
175 #define MAX_MONITOR_DEVICES 10
177 static const char *data_dir
;
178 const char *bios_name
= NULL
;
179 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
180 to store the VM snapshots */
181 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
182 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
183 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
184 static DisplayState
*display_state
;
185 DisplayType display_type
= DT_DEFAULT
;
186 const char* keyboard_layout
= NULL
;
189 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 QEMUClock
*rtc_clock
;
195 int vga_interface_type
= VGA_CIRRUS
;
197 int graphic_width
= 1024;
198 int graphic_height
= 768;
199 int graphic_depth
= 8;
201 int graphic_width
= 800;
202 int graphic_height
= 600;
203 int graphic_depth
= 15;
205 static int full_screen
= 0;
207 static int no_frame
= 0;
210 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
211 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
212 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
214 int win2k_install_hack
= 0;
223 const char *vnc_display
;
224 int acpi_enabled
= 1;
230 int graphic_rotate
= 0;
231 uint8_t irq0override
= 1;
235 const char *watchdog
;
236 const char *option_rom
[MAX_OPTION_ROMS
];
238 int semihosting_enabled
= 0;
242 const char *qemu_name
;
245 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
246 unsigned int nb_prom_envs
= 0;
247 const char *prom_envs
[MAX_PROM_ENVS
];
252 uint64_t node_mem
[MAX_NODES
];
253 uint64_t node_cpumask
[MAX_NODES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int timer_alarm_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
266 static QEMUTimer
*nographic_timer
;
268 uint8_t qemu_uuid
[16];
270 static QEMUBootSetHandler
*boot_set_handler
;
271 static void *boot_set_opaque
;
273 /***********************************************************/
274 /* x86 ISA bus support */
276 target_phys_addr_t isa_mem_base
= 0;
279 /***********************************************************/
280 void hw_error(const char *fmt
, ...)
286 fprintf(stderr
, "qemu: hardware error: ");
287 vfprintf(stderr
, fmt
, ap
);
288 fprintf(stderr
, "\n");
289 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
290 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
292 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
294 cpu_dump_state(env
, stderr
, fprintf
, 0);
301 static void set_proc_name(const char *s
)
303 #if defined(__linux__) && defined(PR_SET_NAME)
307 name
[sizeof(name
) - 1] = 0;
308 strncpy(name
, s
, sizeof(name
));
309 /* Could rewrite argv[0] too, but that's a bit more complicated.
310 This simple way is enough for `top'. */
311 prctl(PR_SET_NAME
, name
);
318 static QEMUBalloonEvent
*qemu_balloon_event
;
319 void *qemu_balloon_event_opaque
;
321 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
323 qemu_balloon_event
= func
;
324 qemu_balloon_event_opaque
= opaque
;
327 void qemu_balloon(ram_addr_t target
)
329 if (qemu_balloon_event
)
330 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
333 ram_addr_t
qemu_balloon_status(void)
335 if (qemu_balloon_event
)
336 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
340 /***********************************************************/
343 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
344 static void *qemu_put_kbd_event_opaque
;
345 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
348 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
350 qemu_put_kbd_event_opaque
= opaque
;
351 qemu_put_kbd_event
= func
;
354 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
355 void *opaque
, int absolute
,
358 QEMUPutMouseEntry
*s
, *cursor
;
360 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
362 s
->qemu_put_mouse_event
= func
;
363 s
->qemu_put_mouse_event_opaque
= opaque
;
364 s
->qemu_put_mouse_event_absolute
= absolute
;
365 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
368 if (!qemu_put_mouse_event_head
) {
369 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
373 cursor
= qemu_put_mouse_event_head
;
374 while (cursor
->next
!= NULL
)
375 cursor
= cursor
->next
;
378 qemu_put_mouse_event_current
= s
;
383 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
385 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
387 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
390 cursor
= qemu_put_mouse_event_head
;
391 while (cursor
!= NULL
&& cursor
!= entry
) {
393 cursor
= cursor
->next
;
396 if (cursor
== NULL
) // does not exist or list empty
398 else if (prev
== NULL
) { // entry is head
399 qemu_put_mouse_event_head
= cursor
->next
;
400 if (qemu_put_mouse_event_current
== entry
)
401 qemu_put_mouse_event_current
= cursor
->next
;
402 qemu_free(entry
->qemu_put_mouse_event_name
);
407 prev
->next
= entry
->next
;
409 if (qemu_put_mouse_event_current
== entry
)
410 qemu_put_mouse_event_current
= prev
;
412 qemu_free(entry
->qemu_put_mouse_event_name
);
416 void kbd_put_keycode(int keycode
)
418 if (qemu_put_kbd_event
) {
419 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
423 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
425 QEMUPutMouseEvent
*mouse_event
;
426 void *mouse_event_opaque
;
429 if (!qemu_put_mouse_event_current
) {
434 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
436 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
439 if (graphic_rotate
) {
440 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
443 width
= graphic_width
- 1;
444 mouse_event(mouse_event_opaque
,
445 width
- dy
, dx
, dz
, buttons_state
);
447 mouse_event(mouse_event_opaque
,
448 dx
, dy
, dz
, buttons_state
);
452 int kbd_mouse_is_absolute(void)
454 if (!qemu_put_mouse_event_current
)
457 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
460 void do_info_mice(Monitor
*mon
)
462 QEMUPutMouseEntry
*cursor
;
465 if (!qemu_put_mouse_event_head
) {
466 monitor_printf(mon
, "No mouse devices connected\n");
470 monitor_printf(mon
, "Mouse devices available:\n");
471 cursor
= qemu_put_mouse_event_head
;
472 while (cursor
!= NULL
) {
473 monitor_printf(mon
, "%c Mouse #%d: %s\n",
474 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
475 index
, cursor
->qemu_put_mouse_event_name
);
477 cursor
= cursor
->next
;
481 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
483 QEMUPutMouseEntry
*cursor
;
485 int index
= qdict_get_int(qdict
, "index");
487 if (!qemu_put_mouse_event_head
) {
488 monitor_printf(mon
, "No mouse devices connected\n");
492 cursor
= qemu_put_mouse_event_head
;
493 while (cursor
!= NULL
&& index
!= i
) {
495 cursor
= cursor
->next
;
499 qemu_put_mouse_event_current
= cursor
;
501 monitor_printf(mon
, "Mouse at given index not found\n");
504 /* compute with 96 bit intermediate result: (a*b)/c */
505 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
510 #ifdef HOST_WORDS_BIGENDIAN
520 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
521 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
524 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
528 /***********************************************************/
529 /* real time host monotonic timer */
531 static int64_t get_clock_realtime(void)
535 gettimeofday(&tv
, NULL
);
536 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
541 static int64_t clock_freq
;
543 static void init_get_clock(void)
547 ret
= QueryPerformanceFrequency(&freq
);
549 fprintf(stderr
, "Could not calibrate ticks\n");
552 clock_freq
= freq
.QuadPart
;
555 static int64_t get_clock(void)
558 QueryPerformanceCounter(&ti
);
559 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
564 static int use_rt_clock
;
566 static void init_get_clock(void)
569 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
570 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
573 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
580 static int64_t get_clock(void)
582 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
583 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
586 clock_gettime(CLOCK_MONOTONIC
, &ts
);
587 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
591 /* XXX: using gettimeofday leads to problems if the date
592 changes, so it should be avoided. */
593 return get_clock_realtime();
598 /* Return the virtual CPU time, based on the instruction counter. */
599 static int64_t cpu_get_icount(void)
602 CPUState
*env
= cpu_single_env
;;
603 icount
= qemu_icount
;
606 fprintf(stderr
, "Bad clock read\n");
607 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
609 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
612 /***********************************************************/
613 /* guest cycle counter */
615 typedef struct TimersState
{
616 int64_t cpu_ticks_prev
;
617 int64_t cpu_ticks_offset
;
618 int64_t cpu_clock_offset
;
619 int32_t cpu_ticks_enabled
;
623 TimersState timers_state
;
625 /* return the host CPU cycle counter and handle stop/restart */
626 int64_t cpu_get_ticks(void)
629 return cpu_get_icount();
631 if (!timers_state
.cpu_ticks_enabled
) {
632 return timers_state
.cpu_ticks_offset
;
635 ticks
= cpu_get_real_ticks();
636 if (timers_state
.cpu_ticks_prev
> ticks
) {
637 /* Note: non increasing ticks may happen if the host uses
639 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
641 timers_state
.cpu_ticks_prev
= ticks
;
642 return ticks
+ timers_state
.cpu_ticks_offset
;
646 /* return the host CPU monotonic timer and handle stop/restart */
647 static int64_t cpu_get_clock(void)
650 if (!timers_state
.cpu_ticks_enabled
) {
651 return timers_state
.cpu_clock_offset
;
654 return ti
+ timers_state
.cpu_clock_offset
;
658 /* enable cpu_get_ticks() */
659 void cpu_enable_ticks(void)
661 if (!timers_state
.cpu_ticks_enabled
) {
662 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
663 timers_state
.cpu_clock_offset
-= get_clock();
664 timers_state
.cpu_ticks_enabled
= 1;
668 /* disable cpu_get_ticks() : the clock is stopped. You must not call
669 cpu_get_ticks() after that. */
670 void cpu_disable_ticks(void)
672 if (timers_state
.cpu_ticks_enabled
) {
673 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
674 timers_state
.cpu_clock_offset
= cpu_get_clock();
675 timers_state
.cpu_ticks_enabled
= 0;
679 /***********************************************************/
682 #define QEMU_CLOCK_REALTIME 0
683 #define QEMU_CLOCK_VIRTUAL 1
684 #define QEMU_CLOCK_HOST 2
688 /* XXX: add frequency */
696 struct QEMUTimer
*next
;
699 struct qemu_alarm_timer
{
703 int (*start
)(struct qemu_alarm_timer
*t
);
704 void (*stop
)(struct qemu_alarm_timer
*t
);
705 void (*rearm
)(struct qemu_alarm_timer
*t
);
709 #define ALARM_FLAG_DYNTICKS 0x1
710 #define ALARM_FLAG_EXPIRED 0x2
712 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
714 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
717 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
719 if (!alarm_has_dynticks(t
))
725 /* TODO: MIN_TIMER_REARM_US should be optimized */
726 #define MIN_TIMER_REARM_US 250
728 static struct qemu_alarm_timer
*alarm_timer
;
732 struct qemu_alarm_win32
{
735 } alarm_win32_data
= {0, -1};
737 static int win32_start_timer(struct qemu_alarm_timer
*t
);
738 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
739 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
743 static int unix_start_timer(struct qemu_alarm_timer
*t
);
744 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
748 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
749 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
750 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
752 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
753 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
755 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
756 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
758 #endif /* __linux__ */
762 /* Correlation between real and virtual time is always going to be
763 fairly approximate, so ignore small variation.
764 When the guest is idle real and virtual time will be aligned in
766 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
768 static void icount_adjust(void)
773 static int64_t last_delta
;
774 /* If the VM is not running, then do nothing. */
778 cur_time
= cpu_get_clock();
779 cur_icount
= qemu_get_clock(vm_clock
);
780 delta
= cur_icount
- cur_time
;
781 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
783 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
784 && icount_time_shift
> 0) {
785 /* The guest is getting too far ahead. Slow time down. */
789 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
790 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
791 /* The guest is getting too far behind. Speed time up. */
795 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
798 static void icount_adjust_rt(void * opaque
)
800 qemu_mod_timer(icount_rt_timer
,
801 qemu_get_clock(rt_clock
) + 1000);
805 static void icount_adjust_vm(void * opaque
)
807 qemu_mod_timer(icount_vm_timer
,
808 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
812 static void init_icount_adjust(void)
814 /* Have both realtime and virtual time triggers for speed adjustment.
815 The realtime trigger catches emulated time passing too slowly,
816 the virtual time trigger catches emulated time passing too fast.
817 Realtime triggers occur even when idle, so use them less frequently
819 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
820 qemu_mod_timer(icount_rt_timer
,
821 qemu_get_clock(rt_clock
) + 1000);
822 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
823 qemu_mod_timer(icount_vm_timer
,
824 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
827 static struct qemu_alarm_timer alarm_timers
[] = {
830 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
831 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
832 /* HPET - if available - is preferred */
833 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
834 /* ...otherwise try RTC */
835 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
837 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
839 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
840 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
841 {"win32", 0, win32_start_timer
,
842 win32_stop_timer
, NULL
, &alarm_win32_data
},
847 static void show_available_alarms(void)
851 printf("Available alarm timers, in order of precedence:\n");
852 for (i
= 0; alarm_timers
[i
].name
; i
++)
853 printf("%s\n", alarm_timers
[i
].name
);
856 static void configure_alarms(char const *opt
)
860 int count
= ARRAY_SIZE(alarm_timers
) - 1;
863 struct qemu_alarm_timer tmp
;
865 if (!strcmp(opt
, "?")) {
866 show_available_alarms();
870 arg
= qemu_strdup(opt
);
872 /* Reorder the array */
873 name
= strtok(arg
, ",");
875 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
876 if (!strcmp(alarm_timers
[i
].name
, name
))
881 fprintf(stderr
, "Unknown clock %s\n", name
);
890 tmp
= alarm_timers
[i
];
891 alarm_timers
[i
] = alarm_timers
[cur
];
892 alarm_timers
[cur
] = tmp
;
896 name
= strtok(NULL
, ",");
902 /* Disable remaining timers */
903 for (i
= cur
; i
< count
; i
++)
904 alarm_timers
[i
].name
= NULL
;
906 show_available_alarms();
911 #define QEMU_NUM_CLOCKS 3
915 QEMUClock
*host_clock
;
917 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
919 static QEMUClock
*qemu_new_clock(int type
)
922 clock
= qemu_mallocz(sizeof(QEMUClock
));
927 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
931 ts
= qemu_mallocz(sizeof(QEMUTimer
));
938 void qemu_free_timer(QEMUTimer
*ts
)
943 /* stop a timer, but do not dealloc it */
944 void qemu_del_timer(QEMUTimer
*ts
)
948 /* NOTE: this code must be signal safe because
949 qemu_timer_expired() can be called from a signal. */
950 pt
= &active_timers
[ts
->clock
->type
];
963 /* modify the current timer so that it will be fired when current_time
964 >= expire_time. The corresponding callback will be called. */
965 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
971 /* add the timer in the sorted list */
972 /* NOTE: this code must be signal safe because
973 qemu_timer_expired() can be called from a signal. */
974 pt
= &active_timers
[ts
->clock
->type
];
979 if (t
->expire_time
> expire_time
)
983 ts
->expire_time
= expire_time
;
987 /* Rearm if necessary */
988 if (pt
== &active_timers
[ts
->clock
->type
]) {
989 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
990 qemu_rearm_alarm_timer(alarm_timer
);
992 /* Interrupt execution to force deadline recalculation. */
998 int qemu_timer_pending(QEMUTimer
*ts
)
1001 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1008 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1012 return (timer_head
->expire_time
<= current_time
);
1015 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1021 if (!ts
|| ts
->expire_time
> current_time
)
1023 /* remove timer from the list before calling the callback */
1024 *ptimer_head
= ts
->next
;
1027 /* run the callback (the timer list can be modified) */
1032 int64_t qemu_get_clock(QEMUClock
*clock
)
1034 switch(clock
->type
) {
1035 case QEMU_CLOCK_REALTIME
:
1036 return get_clock() / 1000000;
1038 case QEMU_CLOCK_VIRTUAL
:
1040 return cpu_get_icount();
1042 return cpu_get_clock();
1044 case QEMU_CLOCK_HOST
:
1045 return get_clock_realtime();
1049 static void init_clocks(void)
1052 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1053 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1054 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1056 rtc_clock
= host_clock
;
1060 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1062 uint64_t expire_time
;
1064 if (qemu_timer_pending(ts
)) {
1065 expire_time
= ts
->expire_time
;
1069 qemu_put_be64(f
, expire_time
);
1072 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1074 uint64_t expire_time
;
1076 expire_time
= qemu_get_be64(f
);
1077 if (expire_time
!= -1) {
1078 qemu_mod_timer(ts
, expire_time
);
1084 static const VMStateDescription vmstate_timers
= {
1087 .minimum_version_id
= 1,
1088 .minimum_version_id_old
= 1,
1089 .fields
= (VMStateField
[]) {
1090 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1091 VMSTATE_INT64(dummy
, TimersState
),
1092 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1093 VMSTATE_END_OF_LIST()
1097 static void qemu_event_increment(void);
1100 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1101 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1104 static void host_alarm_handler(int host_signum
)
1108 #define DISP_FREQ 1000
1110 static int64_t delta_min
= INT64_MAX
;
1111 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1113 ti
= qemu_get_clock(vm_clock
);
1114 if (last_clock
!= 0) {
1115 delta
= ti
- last_clock
;
1116 if (delta
< delta_min
)
1118 if (delta
> delta_max
)
1121 if (++count
== DISP_FREQ
) {
1122 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1123 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1124 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1125 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1126 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1128 delta_min
= INT64_MAX
;
1136 if (alarm_has_dynticks(alarm_timer
) ||
1138 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1139 qemu_get_clock(vm_clock
))) ||
1140 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1141 qemu_get_clock(rt_clock
)) ||
1142 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1143 qemu_get_clock(host_clock
))) {
1144 qemu_event_increment();
1145 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1147 #ifndef CONFIG_IOTHREAD
1149 /* stop the currently executing cpu because a timer occured */
1153 timer_alarm_pending
= 1;
1154 qemu_notify_event();
1158 static int64_t qemu_next_deadline(void)
1160 /* To avoid problems with overflow limit this to 2^32. */
1161 int64_t delta
= INT32_MAX
;
1163 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1164 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1165 qemu_get_clock(vm_clock
);
1167 if (active_timers
[QEMU_CLOCK_HOST
]) {
1168 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1169 qemu_get_clock(host_clock
);
1180 #if defined(__linux__)
1181 static uint64_t qemu_next_deadline_dyntick(void)
1189 delta
= (qemu_next_deadline() + 999) / 1000;
1191 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1192 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1193 qemu_get_clock(rt_clock
))*1000;
1194 if (rtdelta
< delta
)
1198 if (delta
< MIN_TIMER_REARM_US
)
1199 delta
= MIN_TIMER_REARM_US
;
1207 /* Sets a specific flag */
1208 static int fcntl_setfl(int fd
, int flag
)
1212 flags
= fcntl(fd
, F_GETFL
);
1216 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1222 #if defined(__linux__)
1224 #define RTC_FREQ 1024
1226 static void enable_sigio_timer(int fd
)
1228 struct sigaction act
;
1231 sigfillset(&act
.sa_mask
);
1233 act
.sa_handler
= host_alarm_handler
;
1235 sigaction(SIGIO
, &act
, NULL
);
1236 fcntl_setfl(fd
, O_ASYNC
);
1237 fcntl(fd
, F_SETOWN
, getpid());
1240 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1242 struct hpet_info info
;
1245 fd
= open("/dev/hpet", O_RDONLY
);
1250 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1252 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1253 "error, but for better emulation accuracy type:\n"
1254 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1258 /* Check capabilities */
1259 r
= ioctl(fd
, HPET_INFO
, &info
);
1263 /* Enable periodic mode */
1264 r
= ioctl(fd
, HPET_EPI
, 0);
1265 if (info
.hi_flags
&& (r
< 0))
1268 /* Enable interrupt */
1269 r
= ioctl(fd
, HPET_IE_ON
, 0);
1273 enable_sigio_timer(fd
);
1274 t
->priv
= (void *)(long)fd
;
1282 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1284 int fd
= (long)t
->priv
;
1289 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1292 unsigned long current_rtc_freq
= 0;
1294 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1297 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1298 if (current_rtc_freq
!= RTC_FREQ
&&
1299 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1300 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1301 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1302 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1305 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1311 enable_sigio_timer(rtc_fd
);
1313 t
->priv
= (void *)(long)rtc_fd
;
1318 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1320 int rtc_fd
= (long)t
->priv
;
1325 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1329 struct sigaction act
;
1331 sigfillset(&act
.sa_mask
);
1333 act
.sa_handler
= host_alarm_handler
;
1335 sigaction(SIGALRM
, &act
, NULL
);
1338 * Initialize ev struct to 0 to avoid valgrind complaining
1339 * about uninitialized data in timer_create call
1341 memset(&ev
, 0, sizeof(ev
));
1342 ev
.sigev_value
.sival_int
= 0;
1343 ev
.sigev_notify
= SIGEV_SIGNAL
;
1344 ev
.sigev_signo
= SIGALRM
;
1346 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1347 perror("timer_create");
1349 /* disable dynticks */
1350 fprintf(stderr
, "Dynamic Ticks disabled\n");
1355 t
->priv
= (void *)(long)host_timer
;
1360 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1362 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1364 timer_delete(host_timer
);
1367 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1369 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1370 struct itimerspec timeout
;
1371 int64_t nearest_delta_us
= INT64_MAX
;
1374 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1375 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1376 !active_timers
[QEMU_CLOCK_HOST
])
1379 nearest_delta_us
= qemu_next_deadline_dyntick();
1381 /* check whether a timer is already running */
1382 if (timer_gettime(host_timer
, &timeout
)) {
1384 fprintf(stderr
, "Internal timer error: aborting\n");
1387 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1388 if (current_us
&& current_us
<= nearest_delta_us
)
1391 timeout
.it_interval
.tv_sec
= 0;
1392 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1393 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1394 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1395 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1397 fprintf(stderr
, "Internal timer error: aborting\n");
1402 #endif /* defined(__linux__) */
1404 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1406 struct sigaction act
;
1407 struct itimerval itv
;
1411 sigfillset(&act
.sa_mask
);
1413 act
.sa_handler
= host_alarm_handler
;
1415 sigaction(SIGALRM
, &act
, NULL
);
1417 itv
.it_interval
.tv_sec
= 0;
1418 /* for i386 kernel 2.6 to get 1 ms */
1419 itv
.it_interval
.tv_usec
= 999;
1420 itv
.it_value
.tv_sec
= 0;
1421 itv
.it_value
.tv_usec
= 10 * 1000;
1423 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1430 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1432 struct itimerval itv
;
1434 memset(&itv
, 0, sizeof(itv
));
1435 setitimer(ITIMER_REAL
, &itv
, NULL
);
1438 #endif /* !defined(_WIN32) */
1443 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1446 struct qemu_alarm_win32
*data
= t
->priv
;
1449 memset(&tc
, 0, sizeof(tc
));
1450 timeGetDevCaps(&tc
, sizeof(tc
));
1452 if (data
->period
< tc
.wPeriodMin
)
1453 data
->period
= tc
.wPeriodMin
;
1455 timeBeginPeriod(data
->period
);
1457 flags
= TIME_CALLBACK_FUNCTION
;
1458 if (alarm_has_dynticks(t
))
1459 flags
|= TIME_ONESHOT
;
1461 flags
|= TIME_PERIODIC
;
1463 data
->timerId
= timeSetEvent(1, // interval (ms)
1464 data
->period
, // resolution
1465 host_alarm_handler
, // function
1466 (DWORD
)t
, // parameter
1469 if (!data
->timerId
) {
1470 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1472 timeEndPeriod(data
->period
);
1479 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1481 struct qemu_alarm_win32
*data
= t
->priv
;
1483 timeKillEvent(data
->timerId
);
1484 timeEndPeriod(data
->period
);
1487 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1489 struct qemu_alarm_win32
*data
= t
->priv
;
1491 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1492 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1493 !active_timers
[QEMU_CLOCK_HOST
])
1496 timeKillEvent(data
->timerId
);
1498 data
->timerId
= timeSetEvent(1,
1502 TIME_ONESHOT
| TIME_PERIODIC
);
1504 if (!data
->timerId
) {
1505 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1508 timeEndPeriod(data
->period
);
1515 static int init_timer_alarm(void)
1517 struct qemu_alarm_timer
*t
= NULL
;
1520 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1521 t
= &alarm_timers
[i
];
1541 static void quit_timers(void)
1543 alarm_timer
->stop(alarm_timer
);
1547 /***********************************************************/
1548 /* host time/date access */
1549 void qemu_get_timedate(struct tm
*tm
, int offset
)
1556 if (rtc_date_offset
== -1) {
1560 ret
= localtime(&ti
);
1562 ti
-= rtc_date_offset
;
1566 memcpy(tm
, ret
, sizeof(struct tm
));
1569 int qemu_timedate_diff(struct tm
*tm
)
1573 if (rtc_date_offset
== -1)
1575 seconds
= mktimegm(tm
);
1577 seconds
= mktime(tm
);
1579 seconds
= mktimegm(tm
) + rtc_date_offset
;
1581 return seconds
- time(NULL
);
1584 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1586 time_t rtc_start_date
;
1589 if (!strcmp(startdate
, "now") && legacy
) {
1590 rtc_date_offset
= -1;
1592 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1600 } else if (sscanf(startdate
, "%d-%d-%d",
1603 &tm
.tm_mday
) == 3) {
1612 rtc_start_date
= mktimegm(&tm
);
1613 if (rtc_start_date
== -1) {
1615 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1616 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1619 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1623 static void configure_rtc(QemuOpts
*opts
)
1627 value
= qemu_opt_get(opts
, "base");
1629 if (!strcmp(value
, "utc")) {
1631 } else if (!strcmp(value
, "localtime")) {
1634 configure_rtc_date_offset(value
, 0);
1637 value
= qemu_opt_get(opts
, "clock");
1639 if (!strcmp(value
, "host")) {
1640 rtc_clock
= host_clock
;
1641 } else if (!strcmp(value
, "vm")) {
1642 rtc_clock
= vm_clock
;
1644 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1648 #ifdef CONFIG_TARGET_I386
1649 value
= qemu_opt_get(opts
, "driftfix");
1651 if (!strcmp(buf
, "slew")) {
1653 } else if (!strcmp(buf
, "none")) {
1656 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1664 static void socket_cleanup(void)
1669 static int socket_init(void)
1674 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1676 err
= WSAGetLastError();
1677 fprintf(stderr
, "WSAStartup: %d\n", err
);
1680 atexit(socket_cleanup
);
1685 /***********************************************************/
1686 /* Bluetooth support */
1689 static struct HCIInfo
*hci_table
[MAX_NICS
];
1691 static struct bt_vlan_s
{
1692 struct bt_scatternet_s net
;
1694 struct bt_vlan_s
*next
;
1697 /* find or alloc a new bluetooth "VLAN" */
1698 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1700 struct bt_vlan_s
**pvlan
, *vlan
;
1701 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1705 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1707 pvlan
= &first_bt_vlan
;
1708 while (*pvlan
!= NULL
)
1709 pvlan
= &(*pvlan
)->next
;
1714 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1718 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1723 static struct HCIInfo null_hci
= {
1724 .cmd_send
= null_hci_send
,
1725 .sco_send
= null_hci_send
,
1726 .acl_send
= null_hci_send
,
1727 .bdaddr_set
= null_hci_addr_set
,
1730 struct HCIInfo
*qemu_next_hci(void)
1732 if (cur_hci
== nb_hcis
)
1735 return hci_table
[cur_hci
++];
1738 static struct HCIInfo
*hci_init(const char *str
)
1741 struct bt_scatternet_s
*vlan
= 0;
1743 if (!strcmp(str
, "null"))
1746 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1748 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1749 else if (!strncmp(str
, "hci", 3)) {
1752 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1753 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1758 vlan
= qemu_find_bt_vlan(0);
1760 return bt_new_hci(vlan
);
1763 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1768 static int bt_hci_parse(const char *str
)
1770 struct HCIInfo
*hci
;
1773 if (nb_hcis
>= MAX_NICS
) {
1774 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1778 hci
= hci_init(str
);
1787 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1788 hci
->bdaddr_set(hci
, bdaddr
.b
);
1790 hci_table
[nb_hcis
++] = hci
;
1795 static void bt_vhci_add(int vlan_id
)
1797 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1800 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1801 "an empty scatternet %i\n", vlan_id
);
1803 bt_vhci_init(bt_new_hci(vlan
));
1806 static struct bt_device_s
*bt_device_add(const char *opt
)
1808 struct bt_scatternet_s
*vlan
;
1810 char *endp
= strstr(opt
, ",vlan=");
1811 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1814 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1817 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1819 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1824 vlan
= qemu_find_bt_vlan(vlan_id
);
1827 fprintf(stderr
, "qemu: warning: adding a slave device to "
1828 "an empty scatternet %i\n", vlan_id
);
1830 if (!strcmp(devname
, "keyboard"))
1831 return bt_keyboard_init(vlan
);
1833 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1837 static int bt_parse(const char *opt
)
1839 const char *endp
, *p
;
1842 if (strstart(opt
, "hci", &endp
)) {
1843 if (!*endp
|| *endp
== ',') {
1845 if (!strstart(endp
, ",vlan=", 0))
1848 return bt_hci_parse(opt
);
1850 } else if (strstart(opt
, "vhci", &endp
)) {
1851 if (!*endp
|| *endp
== ',') {
1853 if (strstart(endp
, ",vlan=", &p
)) {
1854 vlan
= strtol(p
, (char **) &endp
, 0);
1856 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1860 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1869 } else if (strstart(opt
, "device:", &endp
))
1870 return !bt_device_add(endp
);
1872 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1876 /***********************************************************/
1877 /* QEMU Block devices */
1879 #define HD_ALIAS "index=%d,media=disk"
1880 #define CDROM_ALIAS "index=2,media=cdrom"
1881 #define FD_ALIAS "index=%d,if=floppy"
1882 #define PFLASH_ALIAS "if=pflash"
1883 #define MTD_ALIAS "if=mtd"
1884 #define SD_ALIAS "index=0,if=sd"
1886 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1893 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1896 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1898 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1899 __FUNCTION__
, optstr
);
1903 qemu_opt_set(opts
, "file", file
);
1907 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1911 /* seek interface, bus and unit */
1913 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1914 if (dinfo
->type
== type
&&
1915 dinfo
->bus
== bus
&&
1916 dinfo
->unit
== unit
)
1923 DriveInfo
*drive_get_by_id(const char *id
)
1927 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1928 if (strcmp(id
, dinfo
->id
))
1935 int drive_get_max_bus(BlockInterfaceType type
)
1941 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1942 if(dinfo
->type
== type
&&
1943 dinfo
->bus
> max_bus
)
1944 max_bus
= dinfo
->bus
;
1949 const char *drive_get_serial(BlockDriverState
*bdrv
)
1953 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1954 if (dinfo
->bdrv
== bdrv
)
1955 return dinfo
->serial
;
1961 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1965 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1966 if (dinfo
->bdrv
== bdrv
)
1967 return dinfo
->onerror
;
1970 return BLOCK_ERR_STOP_ENOSPC
;
1973 static void bdrv_format_print(void *opaque
, const char *name
)
1975 fprintf(stderr
, " %s", name
);
1978 void drive_uninit(DriveInfo
*dinfo
)
1980 qemu_opts_del(dinfo
->opts
);
1981 bdrv_delete(dinfo
->bdrv
);
1982 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1986 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1990 const char *file
= NULL
;
1993 const char *mediastr
= "";
1994 BlockInterfaceType type
;
1995 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1996 int bus_id
, unit_id
;
1997 int cyls
, heads
, secs
, translation
;
1998 BlockDriver
*drv
= NULL
;
1999 QEMUMachine
*machine
= opaque
;
2005 int bdrv_flags
, onerror
;
2006 const char *devaddr
;
2012 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2015 if (machine
&& machine
->use_scsi
) {
2017 max_devs
= MAX_SCSI_DEVS
;
2018 pstrcpy(devname
, sizeof(devname
), "scsi");
2021 max_devs
= MAX_IDE_DEVS
;
2022 pstrcpy(devname
, sizeof(devname
), "ide");
2026 /* extract parameters */
2027 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2028 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2029 index
= qemu_opt_get_number(opts
, "index", -1);
2031 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2032 heads
= qemu_opt_get_number(opts
, "heads", 0);
2033 secs
= qemu_opt_get_number(opts
, "secs", 0);
2035 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2036 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2038 file
= qemu_opt_get(opts
, "file");
2039 serial
= qemu_opt_get(opts
, "serial");
2041 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2042 pstrcpy(devname
, sizeof(devname
), buf
);
2043 if (!strcmp(buf
, "ide")) {
2045 max_devs
= MAX_IDE_DEVS
;
2046 } else if (!strcmp(buf
, "scsi")) {
2048 max_devs
= MAX_SCSI_DEVS
;
2049 } else if (!strcmp(buf
, "floppy")) {
2052 } else if (!strcmp(buf
, "pflash")) {
2055 } else if (!strcmp(buf
, "mtd")) {
2058 } else if (!strcmp(buf
, "sd")) {
2061 } else if (!strcmp(buf
, "virtio")) {
2064 } else if (!strcmp(buf
, "xen")) {
2067 } else if (!strcmp(buf
, "none")) {
2071 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2076 if (cyls
|| heads
|| secs
) {
2077 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2078 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2081 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2082 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2085 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2086 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2091 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2094 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2098 if (!strcmp(buf
, "none"))
2099 translation
= BIOS_ATA_TRANSLATION_NONE
;
2100 else if (!strcmp(buf
, "lba"))
2101 translation
= BIOS_ATA_TRANSLATION_LBA
;
2102 else if (!strcmp(buf
, "auto"))
2103 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2105 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2110 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2111 if (!strcmp(buf
, "disk")) {
2113 } else if (!strcmp(buf
, "cdrom")) {
2114 if (cyls
|| secs
|| heads
) {
2116 "qemu: '%s' invalid physical CHS format\n", buf
);
2119 media
= MEDIA_CDROM
;
2121 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2126 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2127 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2129 else if (!strcmp(buf
, "writethrough"))
2131 else if (!strcmp(buf
, "writeback"))
2134 fprintf(stderr
, "qemu: invalid cache option\n");
2139 #ifdef CONFIG_LINUX_AIO
2140 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2141 if (!strcmp(buf
, "threads"))
2143 else if (!strcmp(buf
, "native"))
2146 fprintf(stderr
, "qemu: invalid aio option\n");
2152 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2153 if (strcmp(buf
, "?") == 0) {
2154 fprintf(stderr
, "qemu: Supported formats:");
2155 bdrv_iterate_format(bdrv_format_print
, NULL
);
2156 fprintf(stderr
, "\n");
2159 drv
= bdrv_find_whitelisted_format(buf
);
2161 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2166 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2167 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2168 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2169 fprintf(stderr
, "werror is no supported by this format\n");
2172 if (!strcmp(buf
, "ignore"))
2173 onerror
= BLOCK_ERR_IGNORE
;
2174 else if (!strcmp(buf
, "enospc"))
2175 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2176 else if (!strcmp(buf
, "stop"))
2177 onerror
= BLOCK_ERR_STOP_ANY
;
2178 else if (!strcmp(buf
, "report"))
2179 onerror
= BLOCK_ERR_REPORT
;
2181 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2186 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2187 if (type
!= IF_VIRTIO
) {
2188 fprintf(stderr
, "addr is not supported\n");
2193 /* compute bus and unit according index */
2196 if (bus_id
!= 0 || unit_id
!= -1) {
2198 "qemu: index cannot be used with bus and unit\n");
2206 unit_id
= index
% max_devs
;
2207 bus_id
= index
/ max_devs
;
2211 /* if user doesn't specify a unit_id,
2212 * try to find the first free
2215 if (unit_id
== -1) {
2217 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2219 if (max_devs
&& unit_id
>= max_devs
) {
2220 unit_id
-= max_devs
;
2228 if (max_devs
&& unit_id
>= max_devs
) {
2229 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2230 unit_id
, max_devs
- 1);
2235 * ignore multiple definitions
2238 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2245 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2246 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2247 dinfo
->id
= qemu_strdup(buf
);
2249 /* no id supplied -> create one */
2250 dinfo
->id
= qemu_mallocz(32);
2251 if (type
== IF_IDE
|| type
== IF_SCSI
)
2252 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2254 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2255 devname
, bus_id
, mediastr
, unit_id
);
2257 snprintf(dinfo
->id
, 32, "%s%s%i",
2258 devname
, mediastr
, unit_id
);
2260 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2261 dinfo
->devaddr
= devaddr
;
2263 dinfo
->bus
= bus_id
;
2264 dinfo
->unit
= unit_id
;
2265 dinfo
->onerror
= onerror
;
2268 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2269 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2279 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2280 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2284 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2289 /* FIXME: This isn't really a floppy, but it's a reasonable
2292 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2298 /* add virtio block device */
2299 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2300 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2301 qemu_opt_set(opts
, "drive", dinfo
->id
);
2303 qemu_opt_set(opts
, "addr", devaddr
);
2314 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2315 cache
= 2; /* always use write-back with snapshot */
2317 if (cache
== 0) /* no caching */
2318 bdrv_flags
|= BDRV_O_NOCACHE
;
2319 else if (cache
== 2) /* write-back */
2320 bdrv_flags
|= BDRV_O_CACHE_WB
;
2323 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2325 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2329 if (type
== IF_IDE
) {
2330 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2333 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2336 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2337 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2338 file
, strerror(errno
));
2342 if (bdrv_key_required(dinfo
->bdrv
))
2348 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2350 QEMUMachine
*machine
= opaque
;
2351 int fatal_error
= 0;
2353 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2360 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2362 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2363 qemu_opt_set(opts
, "snapshot", "on");
2368 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2370 boot_set_handler
= func
;
2371 boot_set_opaque
= opaque
;
2374 int qemu_boot_set(const char *boot_devices
)
2376 if (!boot_set_handler
) {
2379 return boot_set_handler(boot_set_opaque
, boot_devices
);
2382 static int parse_bootdevices(char *devices
)
2384 /* We just do some generic consistency checks */
2388 for (p
= devices
; *p
!= '\0'; p
++) {
2389 /* Allowed boot devices are:
2390 * a-b: floppy disk drives
2391 * c-f: IDE disk drives
2392 * g-m: machine implementation dependant drives
2393 * n-p: network devices
2394 * It's up to each machine implementation to check if the given boot
2395 * devices match the actual hardware implementation and firmware
2398 if (*p
< 'a' || *p
> 'p') {
2399 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2402 if (bitmap
& (1 << (*p
- 'a'))) {
2403 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2406 bitmap
|= 1 << (*p
- 'a');
2411 static void restore_boot_devices(void *opaque
)
2413 char *standard_boot_devices
= opaque
;
2415 qemu_boot_set(standard_boot_devices
);
2417 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2418 qemu_free(standard_boot_devices
);
2421 static void numa_add(const char *optarg
)
2425 unsigned long long value
, endvalue
;
2428 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2429 if (!strcmp(option
, "node")) {
2430 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2431 nodenr
= nb_numa_nodes
;
2433 nodenr
= strtoull(option
, NULL
, 10);
2436 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2437 node_mem
[nodenr
] = 0;
2439 value
= strtoull(option
, &endptr
, 0);
2441 case 0: case 'M': case 'm':
2448 node_mem
[nodenr
] = value
;
2450 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2451 node_cpumask
[nodenr
] = 0;
2453 value
= strtoull(option
, &endptr
, 10);
2456 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2458 if (*endptr
== '-') {
2459 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2460 if (endvalue
>= 63) {
2463 "only 63 CPUs in NUMA mode supported.\n");
2465 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2470 node_cpumask
[nodenr
] = value
;
2477 static void smp_parse(const char *optarg
)
2479 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2483 smp
= strtoul(optarg
, &endptr
, 10);
2484 if (endptr
!= optarg
) {
2485 if (*endptr
== ',') {
2489 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2490 sockets
= strtoull(option
, NULL
, 10);
2491 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2492 cores
= strtoull(option
, NULL
, 10);
2493 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2494 threads
= strtoull(option
, NULL
, 10);
2495 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2496 max_cpus
= strtoull(option
, NULL
, 10);
2498 /* compute missing values, prefer sockets over cores over threads */
2499 if (smp
== 0 || sockets
== 0) {
2500 sockets
= sockets
> 0 ? sockets
: 1;
2501 cores
= cores
> 0 ? cores
: 1;
2502 threads
= threads
> 0 ? threads
: 1;
2504 smp
= cores
* threads
* sockets
;
2506 sockets
= smp
/ (cores
* threads
);
2510 threads
= threads
> 0 ? threads
: 1;
2511 cores
= smp
/ (sockets
* threads
);
2514 sockets
= smp
/ (cores
* threads
);
2516 threads
= smp
/ (cores
* sockets
);
2521 smp_cores
= cores
> 0 ? cores
: 1;
2522 smp_threads
= threads
> 0 ? threads
: 1;
2524 max_cpus
= smp_cpus
;
2527 /***********************************************************/
2530 static int usb_device_add(const char *devname
, int is_hotplug
)
2533 USBDevice
*dev
= NULL
;
2538 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2539 dev
= usbdevice_create(devname
);
2543 /* the other ones */
2544 if (strstart(devname
, "host:", &p
)) {
2545 dev
= usb_host_device_open(p
);
2546 } else if (strstart(devname
, "net:", &p
)) {
2550 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2555 qemu_opt_set(opts
, "type", "nic");
2556 qemu_opt_set(opts
, "model", "usb");
2558 idx
= net_client_init(NULL
, opts
, 0);
2563 dev
= usb_net_init(&nd_table
[idx
]);
2564 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2565 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2566 bt_new_hci(qemu_find_bt_vlan(0)));
2577 static int usb_device_del(const char *devname
)
2582 if (strstart(devname
, "host:", &p
))
2583 return usb_host_device_close(p
);
2588 p
= strchr(devname
, '.');
2591 bus_num
= strtoul(devname
, NULL
, 0);
2592 addr
= strtoul(p
+ 1, NULL
, 0);
2594 return usb_device_delete_addr(bus_num
, addr
);
2597 static int usb_parse(const char *cmdline
)
2599 return usb_device_add(cmdline
, 0);
2602 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2604 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2607 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2609 usb_device_del(qdict_get_str(qdict
, "devname"));
2612 /***********************************************************/
2613 /* PCMCIA/Cardbus */
2615 static struct pcmcia_socket_entry_s
{
2616 PCMCIASocket
*socket
;
2617 struct pcmcia_socket_entry_s
*next
;
2618 } *pcmcia_sockets
= 0;
2620 void pcmcia_socket_register(PCMCIASocket
*socket
)
2622 struct pcmcia_socket_entry_s
*entry
;
2624 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2625 entry
->socket
= socket
;
2626 entry
->next
= pcmcia_sockets
;
2627 pcmcia_sockets
= entry
;
2630 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2632 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2634 ptr
= &pcmcia_sockets
;
2635 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2636 if (entry
->socket
== socket
) {
2642 void pcmcia_info(Monitor
*mon
)
2644 struct pcmcia_socket_entry_s
*iter
;
2646 if (!pcmcia_sockets
)
2647 monitor_printf(mon
, "No PCMCIA sockets\n");
2649 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2650 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2651 iter
->socket
->attached
? iter
->socket
->card_string
:
2655 /***********************************************************/
2656 /* register display */
2658 struct DisplayAllocator default_allocator
= {
2659 defaultallocator_create_displaysurface
,
2660 defaultallocator_resize_displaysurface
,
2661 defaultallocator_free_displaysurface
2664 void register_displaystate(DisplayState
*ds
)
2674 DisplayState
*get_displaystate(void)
2676 return display_state
;
2679 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2681 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2682 return ds
->allocator
;
2687 static void dumb_display_init(void)
2689 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2690 ds
->allocator
= &default_allocator
;
2691 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2692 register_displaystate(ds
);
2695 /***********************************************************/
2698 typedef struct IOHandlerRecord
{
2700 IOCanRWHandler
*fd_read_poll
;
2702 IOHandler
*fd_write
;
2705 /* temporary data */
2707 struct IOHandlerRecord
*next
;
2710 static IOHandlerRecord
*first_io_handler
;
2712 /* XXX: fd_read_poll should be suppressed, but an API change is
2713 necessary in the character devices to suppress fd_can_read(). */
2714 int qemu_set_fd_handler2(int fd
,
2715 IOCanRWHandler
*fd_read_poll
,
2717 IOHandler
*fd_write
,
2720 IOHandlerRecord
**pioh
, *ioh
;
2722 if (!fd_read
&& !fd_write
) {
2723 pioh
= &first_io_handler
;
2728 if (ioh
->fd
== fd
) {
2735 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2739 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2740 ioh
->next
= first_io_handler
;
2741 first_io_handler
= ioh
;
2744 ioh
->fd_read_poll
= fd_read_poll
;
2745 ioh
->fd_read
= fd_read
;
2746 ioh
->fd_write
= fd_write
;
2747 ioh
->opaque
= opaque
;
2753 int qemu_set_fd_handler(int fd
,
2755 IOHandler
*fd_write
,
2758 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2762 /***********************************************************/
2763 /* Polling handling */
2765 typedef struct PollingEntry
{
2768 struct PollingEntry
*next
;
2771 static PollingEntry
*first_polling_entry
;
2773 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2775 PollingEntry
**ppe
, *pe
;
2776 pe
= qemu_mallocz(sizeof(PollingEntry
));
2778 pe
->opaque
= opaque
;
2779 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2784 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2786 PollingEntry
**ppe
, *pe
;
2787 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2789 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2797 /***********************************************************/
2798 /* Wait objects support */
2799 typedef struct WaitObjects
{
2801 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2802 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2803 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2806 static WaitObjects wait_objects
= {0};
2808 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2810 WaitObjects
*w
= &wait_objects
;
2812 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2814 w
->events
[w
->num
] = handle
;
2815 w
->func
[w
->num
] = func
;
2816 w
->opaque
[w
->num
] = opaque
;
2821 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2824 WaitObjects
*w
= &wait_objects
;
2827 for (i
= 0; i
< w
->num
; i
++) {
2828 if (w
->events
[i
] == handle
)
2831 w
->events
[i
] = w
->events
[i
+ 1];
2832 w
->func
[i
] = w
->func
[i
+ 1];
2833 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2841 /***********************************************************/
2842 /* ram save/restore */
2844 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2845 #define RAM_SAVE_FLAG_COMPRESS 0x02
2846 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2847 #define RAM_SAVE_FLAG_PAGE 0x08
2848 #define RAM_SAVE_FLAG_EOS 0x10
2850 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2852 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2853 uint32_t *array
= (uint32_t *)page
;
2856 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2857 if (array
[i
] != val
)
2864 static int ram_save_block(QEMUFile
*f
)
2866 static ram_addr_t current_addr
= 0;
2867 ram_addr_t saved_addr
= current_addr
;
2868 ram_addr_t addr
= 0;
2871 while (addr
< last_ram_offset
) {
2872 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2875 cpu_physical_memory_reset_dirty(current_addr
,
2876 current_addr
+ TARGET_PAGE_SIZE
,
2877 MIGRATION_DIRTY_FLAG
);
2879 p
= qemu_get_ram_ptr(current_addr
);
2881 if (is_dup_page(p
, *p
)) {
2882 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2883 qemu_put_byte(f
, *p
);
2885 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2886 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2892 addr
+= TARGET_PAGE_SIZE
;
2893 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2899 static uint64_t bytes_transferred
= 0;
2901 static ram_addr_t
ram_save_remaining(void)
2904 ram_addr_t count
= 0;
2906 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2907 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2914 uint64_t ram_bytes_remaining(void)
2916 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2919 uint64_t ram_bytes_transferred(void)
2921 return bytes_transferred
;
2924 uint64_t ram_bytes_total(void)
2926 return last_ram_offset
;
2929 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2932 uint64_t bytes_transferred_last
;
2934 uint64_t expected_time
= 0;
2936 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2937 qemu_file_set_error(f
);
2942 /* Make sure all dirty bits are set */
2943 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2944 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2945 cpu_physical_memory_set_dirty(addr
);
2948 /* Enable dirty memory tracking */
2949 cpu_physical_memory_set_dirty_tracking(1);
2951 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2954 bytes_transferred_last
= bytes_transferred
;
2955 bwidth
= get_clock();
2957 while (!qemu_file_rate_limit(f
)) {
2960 ret
= ram_save_block(f
);
2961 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2962 if (ret
== 0) /* no more blocks */
2966 bwidth
= get_clock() - bwidth
;
2967 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2969 /* if we haven't transferred anything this round, force expected_time to a
2970 * a very high value, but without crashing */
2974 /* try transferring iterative blocks of memory */
2976 /* flush all remaining blocks regardless of rate limiting */
2977 while (ram_save_block(f
) != 0) {
2978 bytes_transferred
+= TARGET_PAGE_SIZE
;
2980 cpu_physical_memory_set_dirty_tracking(0);
2983 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2985 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2987 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2990 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2995 if (version_id
!= 3)
2999 addr
= qemu_get_be64(f
);
3001 flags
= addr
& ~TARGET_PAGE_MASK
;
3002 addr
&= TARGET_PAGE_MASK
;
3004 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3005 if (addr
!= last_ram_offset
)
3009 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3010 uint8_t ch
= qemu_get_byte(f
);
3011 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3014 (!kvm_enabled() || kvm_has_sync_mmu())) {
3015 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3018 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3019 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3020 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3025 void qemu_service_io(void)
3027 qemu_notify_event();
3030 /***********************************************************/
3031 /* machine registration */
3033 static QEMUMachine
*first_machine
= NULL
;
3034 QEMUMachine
*current_machine
= NULL
;
3036 int qemu_register_machine(QEMUMachine
*m
)
3039 pm
= &first_machine
;
3047 static QEMUMachine
*find_machine(const char *name
)
3051 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3052 if (!strcmp(m
->name
, name
))
3054 if (m
->alias
&& !strcmp(m
->alias
, name
))
3060 static QEMUMachine
*find_default_machine(void)
3064 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3065 if (m
->is_default
) {
3072 /***********************************************************/
3073 /* main execution loop */
3075 static void gui_update(void *opaque
)
3077 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3078 DisplayState
*ds
= opaque
;
3079 DisplayChangeListener
*dcl
= ds
->listeners
;
3083 while (dcl
!= NULL
) {
3084 if (dcl
->gui_timer_interval
&&
3085 dcl
->gui_timer_interval
< interval
)
3086 interval
= dcl
->gui_timer_interval
;
3089 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3092 static void nographic_update(void *opaque
)
3094 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3096 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3099 struct vm_change_state_entry
{
3100 VMChangeStateHandler
*cb
;
3102 QLIST_ENTRY (vm_change_state_entry
) entries
;
3105 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3107 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3110 VMChangeStateEntry
*e
;
3112 e
= qemu_mallocz(sizeof (*e
));
3116 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3120 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3122 QLIST_REMOVE (e
, entries
);
3126 static void vm_state_notify(int running
, int reason
)
3128 VMChangeStateEntry
*e
;
3130 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3131 e
->cb(e
->opaque
, running
, reason
);
3135 static void resume_all_vcpus(void);
3136 static void pause_all_vcpus(void);
3143 vm_state_notify(1, 0);
3144 qemu_rearm_alarm_timer(alarm_timer
);
3149 /* reset/shutdown handler */
3151 typedef struct QEMUResetEntry
{
3152 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3153 QEMUResetHandler
*func
;
3157 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3158 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3159 static int reset_requested
;
3160 static int shutdown_requested
;
3161 static int powerdown_requested
;
3162 static int debug_requested
;
3163 static int vmstop_requested
;
3165 int qemu_shutdown_requested(void)
3167 int r
= shutdown_requested
;
3168 shutdown_requested
= 0;
3172 int qemu_reset_requested(void)
3174 int r
= reset_requested
;
3175 reset_requested
= 0;
3179 int qemu_powerdown_requested(void)
3181 int r
= powerdown_requested
;
3182 powerdown_requested
= 0;
3186 static int qemu_debug_requested(void)
3188 int r
= debug_requested
;
3189 debug_requested
= 0;
3193 static int qemu_vmstop_requested(void)
3195 int r
= vmstop_requested
;
3196 vmstop_requested
= 0;
3200 static void do_vm_stop(int reason
)
3203 cpu_disable_ticks();
3206 vm_state_notify(0, reason
);
3210 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3212 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3215 re
->opaque
= opaque
;
3216 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3219 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3223 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3224 if (re
->func
== func
&& re
->opaque
== opaque
) {
3225 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3232 void qemu_system_reset(void)
3234 QEMUResetEntry
*re
, *nre
;
3236 /* reset all devices */
3237 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3238 re
->func(re
->opaque
);
3242 void qemu_system_reset_request(void)
3245 shutdown_requested
= 1;
3247 reset_requested
= 1;
3249 qemu_notify_event();
3252 void qemu_system_shutdown_request(void)
3254 shutdown_requested
= 1;
3255 qemu_notify_event();
3258 void qemu_system_powerdown_request(void)
3260 powerdown_requested
= 1;
3261 qemu_notify_event();
3264 #ifdef CONFIG_IOTHREAD
3265 static void qemu_system_vmstop_request(int reason
)
3267 vmstop_requested
= reason
;
3268 qemu_notify_event();
3273 static int io_thread_fd
= -1;
3275 static void qemu_event_increment(void)
3277 static const char byte
= 0;
3279 if (io_thread_fd
== -1)
3282 write(io_thread_fd
, &byte
, sizeof(byte
));
3285 static void qemu_event_read(void *opaque
)
3287 int fd
= (unsigned long)opaque
;
3290 /* Drain the notify pipe */
3293 len
= read(fd
, buffer
, sizeof(buffer
));
3294 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3297 static int qemu_event_init(void)
3306 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3310 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3314 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3315 (void *)(unsigned long)fds
[0]);
3317 io_thread_fd
= fds
[1];
3326 HANDLE qemu_event_handle
;
3328 static void dummy_event_handler(void *opaque
)
3332 static int qemu_event_init(void)
3334 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3335 if (!qemu_event_handle
) {
3336 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3339 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3343 static void qemu_event_increment(void)
3345 if (!SetEvent(qemu_event_handle
)) {
3346 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3353 static int cpu_can_run(CPUState
*env
)
3362 #ifndef CONFIG_IOTHREAD
3363 static int qemu_init_main_loop(void)
3365 return qemu_event_init();
3368 void qemu_init_vcpu(void *_env
)
3370 CPUState
*env
= _env
;
3374 env
->nr_cores
= smp_cores
;
3375 env
->nr_threads
= smp_threads
;
3379 int qemu_cpu_self(void *env
)
3384 static void resume_all_vcpus(void)
3388 static void pause_all_vcpus(void)
3392 void qemu_cpu_kick(void *env
)
3397 void qemu_notify_event(void)
3399 CPUState
*env
= cpu_single_env
;
3406 void qemu_mutex_lock_iothread(void) {}
3407 void qemu_mutex_unlock_iothread(void) {}
3409 void vm_stop(int reason
)
3414 #else /* CONFIG_IOTHREAD */
3416 #include "qemu-thread.h"
3418 QemuMutex qemu_global_mutex
;
3419 static QemuMutex qemu_fair_mutex
;
3421 static QemuThread io_thread
;
3423 static QemuThread
*tcg_cpu_thread
;
3424 static QemuCond
*tcg_halt_cond
;
3426 static int qemu_system_ready
;
3428 static QemuCond qemu_cpu_cond
;
3430 static QemuCond qemu_system_cond
;
3431 static QemuCond qemu_pause_cond
;
3433 static void block_io_signals(void);
3434 static void unblock_io_signals(void);
3435 static int tcg_has_work(void);
3437 static int qemu_init_main_loop(void)
3441 ret
= qemu_event_init();
3445 qemu_cond_init(&qemu_pause_cond
);
3446 qemu_mutex_init(&qemu_fair_mutex
);
3447 qemu_mutex_init(&qemu_global_mutex
);
3448 qemu_mutex_lock(&qemu_global_mutex
);
3450 unblock_io_signals();
3451 qemu_thread_self(&io_thread
);
3456 static void qemu_wait_io_event(CPUState
*env
)
3458 while (!tcg_has_work())
3459 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3461 qemu_mutex_unlock(&qemu_global_mutex
);
3464 * Users of qemu_global_mutex can be starved, having no chance
3465 * to acquire it since this path will get to it first.
3466 * So use another lock to provide fairness.
3468 qemu_mutex_lock(&qemu_fair_mutex
);
3469 qemu_mutex_unlock(&qemu_fair_mutex
);
3471 qemu_mutex_lock(&qemu_global_mutex
);
3475 qemu_cond_signal(&qemu_pause_cond
);
3479 static int qemu_cpu_exec(CPUState
*env
);
3481 static void *kvm_cpu_thread_fn(void *arg
)
3483 CPUState
*env
= arg
;
3486 qemu_thread_self(env
->thread
);
3490 /* signal CPU creation */
3491 qemu_mutex_lock(&qemu_global_mutex
);
3493 qemu_cond_signal(&qemu_cpu_cond
);
3495 /* and wait for machine initialization */
3496 while (!qemu_system_ready
)
3497 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3500 if (cpu_can_run(env
))
3502 qemu_wait_io_event(env
);
3508 static void tcg_cpu_exec(void);
3510 static void *tcg_cpu_thread_fn(void *arg
)
3512 CPUState
*env
= arg
;
3515 qemu_thread_self(env
->thread
);
3517 /* signal CPU creation */
3518 qemu_mutex_lock(&qemu_global_mutex
);
3519 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3521 qemu_cond_signal(&qemu_cpu_cond
);
3523 /* and wait for machine initialization */
3524 while (!qemu_system_ready
)
3525 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3529 qemu_wait_io_event(cur_cpu
);
3535 void qemu_cpu_kick(void *_env
)
3537 CPUState
*env
= _env
;
3538 qemu_cond_broadcast(env
->halt_cond
);
3540 qemu_thread_signal(env
->thread
, SIGUSR1
);
3543 int qemu_cpu_self(void *_env
)
3545 CPUState
*env
= _env
;
3548 qemu_thread_self(&this);
3550 return qemu_thread_equal(&this, env
->thread
);
3553 static void cpu_signal(int sig
)
3556 cpu_exit(cpu_single_env
);
3559 static void block_io_signals(void)
3562 struct sigaction sigact
;
3565 sigaddset(&set
, SIGUSR2
);
3566 sigaddset(&set
, SIGIO
);
3567 sigaddset(&set
, SIGALRM
);
3568 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3571 sigaddset(&set
, SIGUSR1
);
3572 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3574 memset(&sigact
, 0, sizeof(sigact
));
3575 sigact
.sa_handler
= cpu_signal
;
3576 sigaction(SIGUSR1
, &sigact
, NULL
);
3579 static void unblock_io_signals(void)
3584 sigaddset(&set
, SIGUSR2
);
3585 sigaddset(&set
, SIGIO
);
3586 sigaddset(&set
, SIGALRM
);
3587 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3590 sigaddset(&set
, SIGUSR1
);
3591 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3594 static void qemu_signal_lock(unsigned int msecs
)
3596 qemu_mutex_lock(&qemu_fair_mutex
);
3598 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3599 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3600 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3603 qemu_mutex_unlock(&qemu_fair_mutex
);
3606 void qemu_mutex_lock_iothread(void)
3608 if (kvm_enabled()) {
3609 qemu_mutex_lock(&qemu_fair_mutex
);
3610 qemu_mutex_lock(&qemu_global_mutex
);
3611 qemu_mutex_unlock(&qemu_fair_mutex
);
3613 qemu_signal_lock(100);
3616 void qemu_mutex_unlock_iothread(void)
3618 qemu_mutex_unlock(&qemu_global_mutex
);
3621 static int all_vcpus_paused(void)
3623 CPUState
*penv
= first_cpu
;
3628 penv
= (CPUState
*)penv
->next_cpu
;
3634 static void pause_all_vcpus(void)
3636 CPUState
*penv
= first_cpu
;
3640 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3641 qemu_cpu_kick(penv
);
3642 penv
= (CPUState
*)penv
->next_cpu
;
3645 while (!all_vcpus_paused()) {
3646 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3649 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3650 penv
= (CPUState
*)penv
->next_cpu
;
3655 static void resume_all_vcpus(void)
3657 CPUState
*penv
= first_cpu
;
3662 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3663 qemu_cpu_kick(penv
);
3664 penv
= (CPUState
*)penv
->next_cpu
;
3668 static void tcg_init_vcpu(void *_env
)
3670 CPUState
*env
= _env
;
3671 /* share a single thread for all cpus with TCG */
3672 if (!tcg_cpu_thread
) {
3673 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3674 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3675 qemu_cond_init(env
->halt_cond
);
3676 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3677 while (env
->created
== 0)
3678 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3679 tcg_cpu_thread
= env
->thread
;
3680 tcg_halt_cond
= env
->halt_cond
;
3682 env
->thread
= tcg_cpu_thread
;
3683 env
->halt_cond
= tcg_halt_cond
;
3687 static void kvm_start_vcpu(CPUState
*env
)
3689 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3690 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3691 qemu_cond_init(env
->halt_cond
);
3692 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3693 while (env
->created
== 0)
3694 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3697 void qemu_init_vcpu(void *_env
)
3699 CPUState
*env
= _env
;
3702 kvm_start_vcpu(env
);
3705 env
->nr_cores
= smp_cores
;
3706 env
->nr_threads
= smp_threads
;
3709 void qemu_notify_event(void)
3711 qemu_event_increment();
3714 void vm_stop(int reason
)
3717 qemu_thread_self(&me
);
3719 if (!qemu_thread_equal(&me
, &io_thread
)) {
3720 qemu_system_vmstop_request(reason
);
3722 * FIXME: should not return to device code in case
3723 * vm_stop() has been requested.
3725 if (cpu_single_env
) {
3726 cpu_exit(cpu_single_env
);
3727 cpu_single_env
->stop
= 1;
3738 static void host_main_loop_wait(int *timeout
)
3744 /* XXX: need to suppress polling by better using win32 events */
3746 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3747 ret
|= pe
->func(pe
->opaque
);
3751 WaitObjects
*w
= &wait_objects
;
3753 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3754 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3755 if (w
->func
[ret
- WAIT_OBJECT_0
])
3756 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3758 /* Check for additional signaled events */
3759 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3761 /* Check if event is signaled */
3762 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3763 if(ret2
== WAIT_OBJECT_0
) {
3765 w
->func
[i
](w
->opaque
[i
]);
3766 } else if (ret2
== WAIT_TIMEOUT
) {
3768 err
= GetLastError();
3769 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3772 } else if (ret
== WAIT_TIMEOUT
) {
3774 err
= GetLastError();
3775 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3782 static void host_main_loop_wait(int *timeout
)
3787 void main_loop_wait(int timeout
)
3789 IOHandlerRecord
*ioh
;
3790 fd_set rfds
, wfds
, xfds
;
3794 qemu_bh_update_timeout(&timeout
);
3796 host_main_loop_wait(&timeout
);
3798 /* poll any events */
3799 /* XXX: separate device handlers from system ones */
3804 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3808 (!ioh
->fd_read_poll
||
3809 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3810 FD_SET(ioh
->fd
, &rfds
);
3814 if (ioh
->fd_write
) {
3815 FD_SET(ioh
->fd
, &wfds
);
3821 tv
.tv_sec
= timeout
/ 1000;
3822 tv
.tv_usec
= (timeout
% 1000) * 1000;
3824 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3826 qemu_mutex_unlock_iothread();
3827 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3828 qemu_mutex_lock_iothread();
3830 IOHandlerRecord
**pioh
;
3832 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3833 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3834 ioh
->fd_read(ioh
->opaque
);
3836 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3837 ioh
->fd_write(ioh
->opaque
);
3841 /* remove deleted IO handlers */
3842 pioh
= &first_io_handler
;
3853 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3855 /* rearm timer, if not periodic */
3856 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3857 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3858 qemu_rearm_alarm_timer(alarm_timer
);
3861 /* vm time timers */
3863 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3864 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3865 qemu_get_clock(vm_clock
));
3868 /* real time timers */
3869 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3870 qemu_get_clock(rt_clock
));
3872 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3873 qemu_get_clock(host_clock
));
3875 /* Check bottom-halves last in case any of the earlier events triggered
3881 static int qemu_cpu_exec(CPUState
*env
)
3884 #ifdef CONFIG_PROFILER
3888 #ifdef CONFIG_PROFILER
3889 ti
= profile_getclock();
3894 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3895 env
->icount_decr
.u16
.low
= 0;
3896 env
->icount_extra
= 0;
3897 count
= qemu_next_deadline();
3898 count
= (count
+ (1 << icount_time_shift
) - 1)
3899 >> icount_time_shift
;
3900 qemu_icount
+= count
;
3901 decr
= (count
> 0xffff) ? 0xffff : count
;
3903 env
->icount_decr
.u16
.low
= decr
;
3904 env
->icount_extra
= count
;
3906 ret
= cpu_exec(env
);
3907 #ifdef CONFIG_PROFILER
3908 qemu_time
+= profile_getclock() - ti
;
3911 /* Fold pending instructions back into the
3912 instruction counter, and clear the interrupt flag. */
3913 qemu_icount
-= (env
->icount_decr
.u16
.low
3914 + env
->icount_extra
);
3915 env
->icount_decr
.u32
= 0;
3916 env
->icount_extra
= 0;
3921 static void tcg_cpu_exec(void)
3925 if (next_cpu
== NULL
)
3926 next_cpu
= first_cpu
;
3927 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3928 CPUState
*env
= cur_cpu
= next_cpu
;
3932 if (timer_alarm_pending
) {
3933 timer_alarm_pending
= 0;
3936 if (cpu_can_run(env
))
3937 ret
= qemu_cpu_exec(env
);
3938 if (ret
== EXCP_DEBUG
) {
3939 gdb_set_stop_cpu(env
);
3940 debug_requested
= 1;
3946 static int cpu_has_work(CPUState
*env
)
3954 if (qemu_cpu_has_work(env
))
3959 static int tcg_has_work(void)
3963 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3964 if (cpu_has_work(env
))
3969 static int qemu_calculate_timeout(void)
3971 #ifndef CONFIG_IOTHREAD
3976 else if (tcg_has_work())
3978 else if (!use_icount
)
3981 /* XXX: use timeout computed from timers */
3984 /* Advance virtual time to the next event. */
3985 if (use_icount
== 1) {
3986 /* When not using an adaptive execution frequency
3987 we tend to get badly out of sync with real time,
3988 so just delay for a reasonable amount of time. */
3991 delta
= cpu_get_icount() - cpu_get_clock();
3994 /* If virtual time is ahead of real time then just
3996 timeout
= (delta
/ 1000000) + 1;
3998 /* Wait for either IO to occur or the next
4000 add
= qemu_next_deadline();
4001 /* We advance the timer before checking for IO.
4002 Limit the amount we advance so that early IO
4003 activity won't get the guest too far ahead. */
4007 add
= (add
+ (1 << icount_time_shift
) - 1)
4008 >> icount_time_shift
;
4010 timeout
= delta
/ 1000000;
4017 #else /* CONFIG_IOTHREAD */
4022 static int vm_can_run(void)
4024 if (powerdown_requested
)
4026 if (reset_requested
)
4028 if (shutdown_requested
)
4030 if (debug_requested
)
4035 qemu_irq qemu_system_powerdown
;
4037 static void main_loop(void)
4041 #ifdef CONFIG_IOTHREAD
4042 qemu_system_ready
= 1;
4043 qemu_cond_broadcast(&qemu_system_cond
);
4048 #ifdef CONFIG_PROFILER
4051 #ifndef CONFIG_IOTHREAD
4054 #ifdef CONFIG_PROFILER
4055 ti
= profile_getclock();
4057 main_loop_wait(qemu_calculate_timeout());
4058 #ifdef CONFIG_PROFILER
4059 dev_time
+= profile_getclock() - ti
;
4061 } while (vm_can_run());
4063 if (qemu_debug_requested()) {
4064 monitor_protocol_event(EVENT_DEBUG
, NULL
);
4065 vm_stop(EXCP_DEBUG
);
4067 if (qemu_shutdown_requested()) {
4068 monitor_protocol_event(EVENT_SHUTDOWN
, NULL
);
4075 if (qemu_reset_requested()) {
4076 monitor_protocol_event(EVENT_RESET
, NULL
);
4078 qemu_system_reset();
4081 if (qemu_powerdown_requested()) {
4082 monitor_protocol_event(EVENT_POWERDOWN
, NULL
);
4083 qemu_irq_raise(qemu_system_powerdown
);
4085 if ((r
= qemu_vmstop_requested())) {
4086 monitor_protocol_event(EVENT_STOP
, NULL
);
4093 static void version(void)
4095 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4098 static void help(int exitcode
)
4101 printf("usage: %s [options] [disk_image]\n"
4103 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4105 #define DEF(option, opt_arg, opt_enum, opt_help) \
4107 #define DEFHEADING(text) stringify(text) "\n"
4108 #include "qemu-options.h"
4113 "During emulation, the following keys are useful:\n"
4114 "ctrl-alt-f toggle full screen\n"
4115 "ctrl-alt-n switch to virtual console 'n'\n"
4116 "ctrl-alt toggle mouse and keyboard grab\n"
4118 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4123 DEFAULT_NETWORK_SCRIPT
,
4124 DEFAULT_NETWORK_DOWN_SCRIPT
,
4126 DEFAULT_GDBSTUB_PORT
,
4131 #define HAS_ARG 0x0001
4134 #define DEF(option, opt_arg, opt_enum, opt_help) \
4136 #define DEFHEADING(text)
4137 #include "qemu-options.h"
4143 typedef struct QEMUOption
{
4149 static const QEMUOption qemu_options
[] = {
4150 { "h", 0, QEMU_OPTION_h
},
4151 #define DEF(option, opt_arg, opt_enum, opt_help) \
4152 { option, opt_arg, opt_enum },
4153 #define DEFHEADING(text)
4154 #include "qemu-options.h"
4162 struct soundhw soundhw
[] = {
4163 #ifdef HAS_AUDIO_CHOICE
4164 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4170 { .init_isa
= pcspk_audio_init
}
4177 "Creative Sound Blaster 16",
4180 { .init_isa
= SB16_init
}
4184 #ifdef CONFIG_CS4231A
4190 { .init_isa
= cs4231a_init
}
4198 "Yamaha YMF262 (OPL3)",
4200 "Yamaha YM3812 (OPL2)",
4204 { .init_isa
= Adlib_init
}
4211 "Gravis Ultrasound GF1",
4214 { .init_isa
= GUS_init
}
4221 "Intel 82801AA AC97 Audio",
4224 { .init_pci
= ac97_init
}
4228 #ifdef CONFIG_ES1370
4231 "ENSONIQ AudioPCI ES1370",
4234 { .init_pci
= es1370_init
}
4238 #endif /* HAS_AUDIO_CHOICE */
4240 { NULL
, NULL
, 0, 0, { NULL
} }
4243 static void select_soundhw (const char *optarg
)
4247 if (*optarg
== '?') {
4250 printf ("Valid sound card names (comma separated):\n");
4251 for (c
= soundhw
; c
->name
; ++c
) {
4252 printf ("%-11s %s\n", c
->name
, c
->descr
);
4254 printf ("\n-soundhw all will enable all of the above\n");
4255 exit (*optarg
!= '?');
4263 if (!strcmp (optarg
, "all")) {
4264 for (c
= soundhw
; c
->name
; ++c
) {
4272 e
= strchr (p
, ',');
4273 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4275 for (c
= soundhw
; c
->name
; ++c
) {
4276 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4285 "Unknown sound card name (too big to show)\n");
4288 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4293 p
+= l
+ (e
!= NULL
);
4297 goto show_valid_cards
;
4302 static void select_vgahw (const char *p
)
4306 vga_interface_type
= VGA_NONE
;
4307 if (strstart(p
, "std", &opts
)) {
4308 vga_interface_type
= VGA_STD
;
4309 } else if (strstart(p
, "cirrus", &opts
)) {
4310 vga_interface_type
= VGA_CIRRUS
;
4311 } else if (strstart(p
, "vmware", &opts
)) {
4312 vga_interface_type
= VGA_VMWARE
;
4313 } else if (strstart(p
, "xenfb", &opts
)) {
4314 vga_interface_type
= VGA_XENFB
;
4315 } else if (!strstart(p
, "none", &opts
)) {
4317 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4321 const char *nextopt
;
4323 if (strstart(opts
, ",retrace=", &nextopt
)) {
4325 if (strstart(opts
, "dumb", &nextopt
))
4326 vga_retrace_method
= VGA_RETRACE_DUMB
;
4327 else if (strstart(opts
, "precise", &nextopt
))
4328 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4329 else goto invalid_vga
;
4330 } else goto invalid_vga
;
4336 static int balloon_parse(const char *arg
)
4340 if (strcmp(arg
, "none") == 0) {
4344 if (!strncmp(arg
, "virtio", 6)) {
4345 if (arg
[6] == ',') {
4346 /* have params -> parse them */
4347 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4351 /* create empty opts */
4352 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4354 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4363 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4365 exit(STATUS_CONTROL_C_EXIT
);
4370 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4374 if(strlen(str
) != 36)
4377 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4378 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4379 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4385 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4393 static void termsig_handler(int signal
)
4395 qemu_system_shutdown_request();
4398 static void sigchld_handler(int signal
)
4400 waitpid(-1, NULL
, WNOHANG
);
4403 static void sighandler_setup(void)
4405 struct sigaction act
;
4407 memset(&act
, 0, sizeof(act
));
4408 act
.sa_handler
= termsig_handler
;
4409 sigaction(SIGINT
, &act
, NULL
);
4410 sigaction(SIGHUP
, &act
, NULL
);
4411 sigaction(SIGTERM
, &act
, NULL
);
4413 act
.sa_handler
= sigchld_handler
;
4414 act
.sa_flags
= SA_NOCLDSTOP
;
4415 sigaction(SIGCHLD
, &act
, NULL
);
4421 /* Look for support files in the same directory as the executable. */
4422 static char *find_datadir(const char *argv0
)
4428 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4435 while (p
!= buf
&& *p
!= '\\')
4438 if (access(buf
, R_OK
) == 0) {
4439 return qemu_strdup(buf
);
4445 /* Find a likely location for support files using the location of the binary.
4446 For installed binaries this will be "$bindir/../share/qemu". When
4447 running from the build tree this will be "$bindir/../pc-bios". */
4448 #define SHARE_SUFFIX "/share/qemu"
4449 #define BUILD_SUFFIX "/pc-bios"
4450 static char *find_datadir(const char *argv0
)
4458 #if defined(__linux__)
4461 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4467 #elif defined(__FreeBSD__)
4470 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4477 /* If we don't have any way of figuring out the actual executable
4478 location then try argv[0]. */
4480 p
= realpath(argv0
, buf
);
4488 max_len
= strlen(dir
) +
4489 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4490 res
= qemu_mallocz(max_len
);
4491 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4492 if (access(res
, R_OK
)) {
4493 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4494 if (access(res
, R_OK
)) {
4506 char *qemu_find_file(int type
, const char *name
)
4512 /* If name contains path separators then try it as a straight path. */
4513 if ((strchr(name
, '/') || strchr(name
, '\\'))
4514 && access(name
, R_OK
) == 0) {
4515 return qemu_strdup(name
);
4518 case QEMU_FILE_TYPE_BIOS
:
4521 case QEMU_FILE_TYPE_KEYMAP
:
4522 subdir
= "keymaps/";
4527 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4528 buf
= qemu_mallocz(len
);
4529 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4530 if (access(buf
, R_OK
)) {
4537 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4541 dev
= qdev_device_add(opts
);
4547 struct device_config
{
4549 DEV_USB
, /* -usbdevice */
4552 const char *cmdline
;
4553 QTAILQ_ENTRY(device_config
) next
;
4555 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4557 static void add_device_config(int type
, const char *cmdline
)
4559 struct device_config
*conf
;
4561 conf
= qemu_mallocz(sizeof(*conf
));
4563 conf
->cmdline
= cmdline
;
4564 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4567 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4569 struct device_config
*conf
;
4572 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4573 if (conf
->type
!= type
)
4575 rc
= func(conf
->cmdline
);
4582 int main(int argc
, char **argv
, char **envp
)
4584 const char *gdbstub_dev
= NULL
;
4585 uint32_t boot_devices_bitmap
= 0;
4587 int snapshot
, linux_boot
, net_boot
;
4588 const char *initrd_filename
;
4589 const char *kernel_filename
, *kernel_cmdline
;
4590 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4592 DisplayChangeListener
*dcl
;
4593 int cyls
, heads
, secs
, translation
;
4594 QemuOpts
*hda_opts
= NULL
, *opts
;
4596 const char *r
, *optarg
;
4597 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4598 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4599 int monitor_flags
[MAX_MONITOR_DEVICES
];
4600 int monitor_device_index
;
4601 const char *serial_devices
[MAX_SERIAL_PORTS
];
4602 int serial_device_index
;
4603 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4604 int parallel_device_index
;
4605 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4606 int virtio_console_index
;
4607 const char *loadvm
= NULL
;
4608 QEMUMachine
*machine
;
4609 const char *cpu_model
;
4614 const char *pid_file
= NULL
;
4615 const char *incoming
= NULL
;
4618 struct passwd
*pwd
= NULL
;
4619 const char *chroot_dir
= NULL
;
4620 const char *run_as
= NULL
;
4623 int show_vnc_port
= 0;
4627 qemu_errors_to_file(stderr
);
4628 qemu_cache_utils_init(envp
);
4630 QLIST_INIT (&vm_change_state_head
);
4633 struct sigaction act
;
4634 sigfillset(&act
.sa_mask
);
4636 act
.sa_handler
= SIG_IGN
;
4637 sigaction(SIGPIPE
, &act
, NULL
);
4640 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4641 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4642 QEMU to run on a single CPU */
4647 h
= GetCurrentProcess();
4648 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4649 for(i
= 0; i
< 32; i
++) {
4650 if (mask
& (1 << i
))
4655 SetProcessAffinityMask(h
, mask
);
4661 module_call_init(MODULE_INIT_MACHINE
);
4662 machine
= find_default_machine();
4664 initrd_filename
= NULL
;
4667 kernel_filename
= NULL
;
4668 kernel_cmdline
= "";
4669 cyls
= heads
= secs
= 0;
4670 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4672 serial_devices
[0] = "vc:80Cx24C";
4673 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4674 serial_devices
[i
] = NULL
;
4675 serial_device_index
= 0;
4677 parallel_devices
[0] = "vc:80Cx24C";
4678 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4679 parallel_devices
[i
] = NULL
;
4680 parallel_device_index
= 0;
4682 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4683 virtio_consoles
[i
] = NULL
;
4684 virtio_console_index
= 0;
4686 monitor_devices
[0] = "vc:80Cx24C";
4687 monitor_flags
[0] = MONITOR_IS_DEFAULT
| MONITOR_USE_READLINE
;
4688 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4689 monitor_devices
[i
] = NULL
;
4690 monitor_flags
[i
] = MONITOR_USE_READLINE
;
4692 monitor_device_index
= 0;
4694 for (i
= 0; i
< MAX_NODES
; i
++) {
4696 node_cpumask
[i
] = 0;
4711 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4713 const QEMUOption
*popt
;
4716 /* Treat --foo the same as -foo. */
4719 popt
= qemu_options
;
4722 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4726 if (!strcmp(popt
->name
, r
+ 1))
4730 if (popt
->flags
& HAS_ARG
) {
4731 if (optind
>= argc
) {
4732 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4736 optarg
= argv
[optind
++];
4741 switch(popt
->index
) {
4743 machine
= find_machine(optarg
);
4746 printf("Supported machines are:\n");
4747 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4749 printf("%-10s %s (alias of %s)\n",
4750 m
->alias
, m
->desc
, m
->name
);
4751 printf("%-10s %s%s\n",
4753 m
->is_default
? " (default)" : "");
4755 exit(*optarg
!= '?');
4758 case QEMU_OPTION_cpu
:
4759 /* hw initialization will check this */
4760 if (*optarg
== '?') {
4761 /* XXX: implement xxx_cpu_list for targets that still miss it */
4762 #if defined(cpu_list)
4763 cpu_list(stdout
, &fprintf
);
4770 case QEMU_OPTION_initrd
:
4771 initrd_filename
= optarg
;
4773 case QEMU_OPTION_hda
:
4775 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4777 hda_opts
= drive_add(optarg
, HD_ALIAS
4778 ",cyls=%d,heads=%d,secs=%d%s",
4779 0, cyls
, heads
, secs
,
4780 translation
== BIOS_ATA_TRANSLATION_LBA
?
4782 translation
== BIOS_ATA_TRANSLATION_NONE
?
4783 ",trans=none" : "");
4785 case QEMU_OPTION_hdb
:
4786 case QEMU_OPTION_hdc
:
4787 case QEMU_OPTION_hdd
:
4788 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4790 case QEMU_OPTION_drive
:
4791 drive_add(NULL
, "%s", optarg
);
4793 case QEMU_OPTION_set
:
4794 if (qemu_set_option(optarg
) != 0)
4797 case QEMU_OPTION_mtdblock
:
4798 drive_add(optarg
, MTD_ALIAS
);
4800 case QEMU_OPTION_sd
:
4801 drive_add(optarg
, SD_ALIAS
);
4803 case QEMU_OPTION_pflash
:
4804 drive_add(optarg
, PFLASH_ALIAS
);
4806 case QEMU_OPTION_snapshot
:
4809 case QEMU_OPTION_hdachs
:
4813 cyls
= strtol(p
, (char **)&p
, 0);
4814 if (cyls
< 1 || cyls
> 16383)
4819 heads
= strtol(p
, (char **)&p
, 0);
4820 if (heads
< 1 || heads
> 16)
4825 secs
= strtol(p
, (char **)&p
, 0);
4826 if (secs
< 1 || secs
> 63)
4830 if (!strcmp(p
, "none"))
4831 translation
= BIOS_ATA_TRANSLATION_NONE
;
4832 else if (!strcmp(p
, "lba"))
4833 translation
= BIOS_ATA_TRANSLATION_LBA
;
4834 else if (!strcmp(p
, "auto"))
4835 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4838 } else if (*p
!= '\0') {
4840 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4843 if (hda_opts
!= NULL
) {
4845 snprintf(num
, sizeof(num
), "%d", cyls
);
4846 qemu_opt_set(hda_opts
, "cyls", num
);
4847 snprintf(num
, sizeof(num
), "%d", heads
);
4848 qemu_opt_set(hda_opts
, "heads", num
);
4849 snprintf(num
, sizeof(num
), "%d", secs
);
4850 qemu_opt_set(hda_opts
, "secs", num
);
4851 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4852 qemu_opt_set(hda_opts
, "trans", "lba");
4853 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4854 qemu_opt_set(hda_opts
, "trans", "none");
4858 case QEMU_OPTION_numa
:
4859 if (nb_numa_nodes
>= MAX_NODES
) {
4860 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4865 case QEMU_OPTION_nographic
:
4866 display_type
= DT_NOGRAPHIC
;
4868 #ifdef CONFIG_CURSES
4869 case QEMU_OPTION_curses
:
4870 display_type
= DT_CURSES
;
4873 case QEMU_OPTION_portrait
:
4876 case QEMU_OPTION_kernel
:
4877 kernel_filename
= optarg
;
4879 case QEMU_OPTION_append
:
4880 kernel_cmdline
= optarg
;
4882 case QEMU_OPTION_cdrom
:
4883 drive_add(optarg
, CDROM_ALIAS
);
4885 case QEMU_OPTION_boot
:
4887 static const char * const params
[] = {
4888 "order", "once", "menu", NULL
4890 char buf
[sizeof(boot_devices
)];
4891 char *standard_boot_devices
;
4894 if (!strchr(optarg
, '=')) {
4896 pstrcpy(buf
, sizeof(buf
), optarg
);
4897 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4899 "qemu: unknown boot parameter '%s' in '%s'\n",
4905 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4906 boot_devices_bitmap
= parse_bootdevices(buf
);
4907 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4910 if (get_param_value(buf
, sizeof(buf
),
4912 boot_devices_bitmap
|= parse_bootdevices(buf
);
4913 standard_boot_devices
= qemu_strdup(boot_devices
);
4914 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4915 qemu_register_reset(restore_boot_devices
,
4916 standard_boot_devices
);
4918 if (get_param_value(buf
, sizeof(buf
),
4920 if (!strcmp(buf
, "on")) {
4922 } else if (!strcmp(buf
, "off")) {
4926 "qemu: invalid option value '%s'\n",
4934 case QEMU_OPTION_fda
:
4935 case QEMU_OPTION_fdb
:
4936 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4939 case QEMU_OPTION_no_fd_bootchk
:
4943 case QEMU_OPTION_netdev
:
4944 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4948 case QEMU_OPTION_net
:
4949 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4954 case QEMU_OPTION_tftp
:
4955 legacy_tftp_prefix
= optarg
;
4957 case QEMU_OPTION_bootp
:
4958 legacy_bootp_filename
= optarg
;
4961 case QEMU_OPTION_smb
:
4962 if (net_slirp_smb(optarg
) < 0)
4966 case QEMU_OPTION_redir
:
4967 if (net_slirp_redir(optarg
) < 0)
4971 case QEMU_OPTION_bt
:
4972 add_device_config(DEV_BT
, optarg
);
4975 case QEMU_OPTION_audio_help
:
4979 case QEMU_OPTION_soundhw
:
4980 select_soundhw (optarg
);
4986 case QEMU_OPTION_version
:
4990 case QEMU_OPTION_m
: {
4994 value
= strtoul(optarg
, &ptr
, 10);
4996 case 0: case 'M': case 'm':
5003 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5007 /* On 32-bit hosts, QEMU is limited by virtual address space */
5008 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5009 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5012 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5013 fprintf(stderr
, "qemu: ram size too large\n");
5022 const CPULogItem
*item
;
5024 mask
= cpu_str_to_log_mask(optarg
);
5026 printf("Log items (comma separated):\n");
5027 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5028 printf("%-10s %s\n", item
->name
, item
->help
);
5036 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5038 case QEMU_OPTION_gdb
:
5039 gdbstub_dev
= optarg
;
5044 case QEMU_OPTION_bios
:
5047 case QEMU_OPTION_singlestep
:
5054 keyboard_layout
= optarg
;
5056 case QEMU_OPTION_localtime
:
5059 case QEMU_OPTION_vga
:
5060 select_vgahw (optarg
);
5062 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5068 w
= strtol(p
, (char **)&p
, 10);
5071 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5077 h
= strtol(p
, (char **)&p
, 10);
5082 depth
= strtol(p
, (char **)&p
, 10);
5083 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5084 depth
!= 24 && depth
!= 32)
5086 } else if (*p
== '\0') {
5087 depth
= graphic_depth
;
5094 graphic_depth
= depth
;
5098 case QEMU_OPTION_echr
:
5101 term_escape_char
= strtol(optarg
, &r
, 0);
5103 printf("Bad argument to echr\n");
5106 case QEMU_OPTION_monitor
:
5107 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5108 fprintf(stderr
, "qemu: too many monitor devices\n");
5111 monitor_devices
[monitor_device_index
] =
5112 monitor_cmdline_parse(optarg
,
5113 &monitor_flags
[monitor_device_index
]);
5114 monitor_device_index
++;
5116 case QEMU_OPTION_chardev
:
5117 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5119 fprintf(stderr
, "parse error: %s\n", optarg
);
5122 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5126 case QEMU_OPTION_serial
:
5127 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5128 fprintf(stderr
, "qemu: too many serial ports\n");
5131 serial_devices
[serial_device_index
] = optarg
;
5132 serial_device_index
++;
5134 case QEMU_OPTION_watchdog
:
5137 "qemu: only one watchdog option may be given\n");
5142 case QEMU_OPTION_watchdog_action
:
5143 if (select_watchdog_action(optarg
) == -1) {
5144 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5148 case QEMU_OPTION_virtiocon
:
5149 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5150 fprintf(stderr
, "qemu: too many virtio consoles\n");
5153 virtio_consoles
[virtio_console_index
] = optarg
;
5154 virtio_console_index
++;
5156 case QEMU_OPTION_parallel
:
5157 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5158 fprintf(stderr
, "qemu: too many parallel ports\n");
5161 parallel_devices
[parallel_device_index
] = optarg
;
5162 parallel_device_index
++;
5164 case QEMU_OPTION_loadvm
:
5167 case QEMU_OPTION_full_screen
:
5171 case QEMU_OPTION_no_frame
:
5174 case QEMU_OPTION_alt_grab
:
5177 case QEMU_OPTION_ctrl_grab
:
5180 case QEMU_OPTION_no_quit
:
5183 case QEMU_OPTION_sdl
:
5184 display_type
= DT_SDL
;
5187 case QEMU_OPTION_pidfile
:
5191 case QEMU_OPTION_win2k_hack
:
5192 win2k_install_hack
= 1;
5194 case QEMU_OPTION_rtc_td_hack
:
5197 case QEMU_OPTION_acpitable
:
5198 if(acpi_table_add(optarg
) < 0) {
5199 fprintf(stderr
, "Wrong acpi table provided\n");
5203 case QEMU_OPTION_smbios
:
5204 if(smbios_entry_add(optarg
) < 0) {
5205 fprintf(stderr
, "Wrong smbios provided\n");
5211 case QEMU_OPTION_enable_kvm
:
5215 case QEMU_OPTION_usb
:
5218 case QEMU_OPTION_usbdevice
:
5220 add_device_config(DEV_USB
, optarg
);
5222 case QEMU_OPTION_device
:
5223 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5227 case QEMU_OPTION_smp
:
5230 fprintf(stderr
, "Invalid number of CPUs\n");
5233 if (max_cpus
< smp_cpus
) {
5234 fprintf(stderr
, "maxcpus must be equal to or greater than "
5238 if (max_cpus
> 255) {
5239 fprintf(stderr
, "Unsupported number of maxcpus\n");
5243 case QEMU_OPTION_vnc
:
5244 display_type
= DT_VNC
;
5245 vnc_display
= optarg
;
5248 case QEMU_OPTION_no_acpi
:
5251 case QEMU_OPTION_no_hpet
:
5254 case QEMU_OPTION_balloon
:
5255 if (balloon_parse(optarg
) < 0) {
5256 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5261 case QEMU_OPTION_no_reboot
:
5264 case QEMU_OPTION_no_shutdown
:
5267 case QEMU_OPTION_show_cursor
:
5270 case QEMU_OPTION_uuid
:
5271 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5272 fprintf(stderr
, "Fail to parse UUID string."
5273 " Wrong format.\n");
5278 case QEMU_OPTION_daemonize
:
5282 case QEMU_OPTION_option_rom
:
5283 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5284 fprintf(stderr
, "Too many option ROMs\n");
5287 option_rom
[nb_option_roms
] = optarg
;
5290 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5291 case QEMU_OPTION_semihosting
:
5292 semihosting_enabled
= 1;
5295 case QEMU_OPTION_name
:
5296 qemu_name
= qemu_strdup(optarg
);
5298 char *p
= strchr(qemu_name
, ',');
5301 if (strncmp(p
, "process=", 8)) {
5302 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5310 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5311 case QEMU_OPTION_prom_env
:
5312 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5313 fprintf(stderr
, "Too many prom variables\n");
5316 prom_envs
[nb_prom_envs
] = optarg
;
5321 case QEMU_OPTION_old_param
:
5325 case QEMU_OPTION_clock
:
5326 configure_alarms(optarg
);
5328 case QEMU_OPTION_startdate
:
5329 configure_rtc_date_offset(optarg
, 1);
5331 case QEMU_OPTION_rtc
:
5332 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5334 fprintf(stderr
, "parse error: %s\n", optarg
);
5337 configure_rtc(opts
);
5339 case QEMU_OPTION_tb_size
:
5340 tb_size
= strtol(optarg
, NULL
, 0);
5344 case QEMU_OPTION_icount
:
5346 if (strcmp(optarg
, "auto") == 0) {
5347 icount_time_shift
= -1;
5349 icount_time_shift
= strtol(optarg
, NULL
, 0);
5352 case QEMU_OPTION_incoming
:
5356 case QEMU_OPTION_chroot
:
5357 chroot_dir
= optarg
;
5359 case QEMU_OPTION_runas
:
5364 case QEMU_OPTION_xen_domid
:
5365 xen_domid
= atoi(optarg
);
5367 case QEMU_OPTION_xen_create
:
5368 xen_mode
= XEN_CREATE
;
5370 case QEMU_OPTION_xen_attach
:
5371 xen_mode
= XEN_ATTACH
;
5374 case QEMU_OPTION_readconfig
:
5377 fp
= fopen(optarg
, "r");
5379 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5382 if (qemu_config_parse(fp
) != 0) {
5388 case QEMU_OPTION_writeconfig
:
5391 if (strcmp(optarg
, "-") == 0) {
5394 fp
= fopen(optarg
, "w");
5396 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5400 qemu_config_write(fp
);
5408 /* If no data_dir is specified then try to find it relative to the
5411 data_dir
= find_datadir(argv
[0]);
5413 /* If all else fails use the install patch specified when building. */
5415 data_dir
= CONFIG_QEMU_SHAREDIR
;
5419 * Default to max_cpus = smp_cpus, in case the user doesn't
5420 * specify a max_cpus value.
5423 max_cpus
= smp_cpus
;
5425 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5426 if (smp_cpus
> machine
->max_cpus
) {
5427 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5428 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5433 if (display_type
== DT_NOGRAPHIC
) {
5434 if (serial_device_index
== 0)
5435 serial_devices
[0] = "stdio";
5436 if (parallel_device_index
== 0)
5437 parallel_devices
[0] = "null";
5438 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5439 monitor_devices
[0] = "stdio";
5447 if (pipe(fds
) == -1)
5458 len
= read(fds
[0], &status
, 1);
5459 if (len
== -1 && (errno
== EINTR
))
5464 else if (status
== 1) {
5465 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5482 signal(SIGTSTP
, SIG_IGN
);
5483 signal(SIGTTOU
, SIG_IGN
);
5484 signal(SIGTTIN
, SIG_IGN
);
5487 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5490 write(fds
[1], &status
, 1);
5492 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5497 if (kvm_enabled()) {
5500 ret
= kvm_init(smp_cpus
);
5502 fprintf(stderr
, "failed to initialize KVM\n");
5507 if (qemu_init_main_loop()) {
5508 fprintf(stderr
, "qemu_init_main_loop failed\n");
5511 linux_boot
= (kernel_filename
!= NULL
);
5513 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5514 fprintf(stderr
, "-append only allowed with -kernel option\n");
5518 if (!linux_boot
&& initrd_filename
!= NULL
) {
5519 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5524 /* Win32 doesn't support line-buffering and requires size >= 2 */
5525 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5528 if (init_timer_alarm() < 0) {
5529 fprintf(stderr
, "could not initialize alarm timer\n");
5532 if (use_icount
&& icount_time_shift
< 0) {
5534 /* 125MIPS seems a reasonable initial guess at the guest speed.
5535 It will be corrected fairly quickly anyway. */
5536 icount_time_shift
= 3;
5537 init_icount_adjust();
5544 if (net_init_clients() < 0) {
5548 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5549 net_set_boot_mask(net_boot
);
5551 /* init the bluetooth world */
5552 if (foreach_device_config(DEV_BT
, bt_parse
))
5555 /* init the memory */
5557 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5559 /* init the dynamic translator */
5560 cpu_exec_init_all(tb_size
* 1024 * 1024);
5562 bdrv_init_with_whitelist();
5566 /* we always create the cdrom drive, even if no disk is there */
5567 drive_add(NULL
, CDROM_ALIAS
);
5569 /* we always create at least one floppy */
5570 drive_add(NULL
, FD_ALIAS
, 0);
5572 /* we always create one sd slot, even if no card is in it */
5573 drive_add(NULL
, SD_ALIAS
);
5575 /* open the virtual block devices */
5577 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5578 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5581 vmstate_register(0, &vmstate_timers
,&timers_state
);
5582 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5585 /* Maintain compatibility with multiple stdio monitors */
5586 if (!strcmp(monitor_devices
[0],"stdio")) {
5587 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5588 const char *devname
= serial_devices
[i
];
5589 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5590 monitor_devices
[0] = NULL
;
5592 } else if (devname
&& !strcmp(devname
,"stdio")) {
5593 monitor_devices
[0] = NULL
;
5594 serial_devices
[i
] = "mon:stdio";
5600 if (nb_numa_nodes
> 0) {
5603 if (nb_numa_nodes
> smp_cpus
) {
5604 nb_numa_nodes
= smp_cpus
;
5607 /* If no memory size if given for any node, assume the default case
5608 * and distribute the available memory equally across all nodes
5610 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5611 if (node_mem
[i
] != 0)
5614 if (i
== nb_numa_nodes
) {
5615 uint64_t usedmem
= 0;
5617 /* On Linux, the each node's border has to be 8MB aligned,
5618 * the final node gets the rest.
5620 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5621 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5622 usedmem
+= node_mem
[i
];
5624 node_mem
[i
] = ram_size
- usedmem
;
5627 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5628 if (node_cpumask
[i
] != 0)
5631 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5632 * must cope with this anyway, because there are BIOSes out there in
5633 * real machines which also use this scheme.
5635 if (i
== nb_numa_nodes
) {
5636 for (i
= 0; i
< smp_cpus
; i
++) {
5637 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5642 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5643 const char *devname
= monitor_devices
[i
];
5644 if (devname
&& strcmp(devname
, "none")) {
5647 snprintf(label
, sizeof(label
), "monitor");
5649 snprintf(label
, sizeof(label
), "monitor%d", i
);
5651 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5652 if (!monitor_hds
[i
]) {
5653 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5660 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5661 const char *devname
= serial_devices
[i
];
5662 if (devname
&& strcmp(devname
, "none")) {
5664 snprintf(label
, sizeof(label
), "serial%d", i
);
5665 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5666 if (!serial_hds
[i
]) {
5667 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
5668 devname
, strerror(errno
));
5674 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5675 const char *devname
= parallel_devices
[i
];
5676 if (devname
&& strcmp(devname
, "none")) {
5678 snprintf(label
, sizeof(label
), "parallel%d", i
);
5679 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5680 if (!parallel_hds
[i
]) {
5681 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5682 devname
, strerror(errno
));
5688 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5689 const char *devname
= virtio_consoles
[i
];
5690 if (devname
&& strcmp(devname
, "none")) {
5692 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5693 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5694 if (!virtcon_hds
[i
]) {
5695 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5696 devname
, strerror(errno
));
5702 module_call_init(MODULE_INIT_DEVICE
);
5705 i
= select_watchdog(watchdog
);
5707 exit (i
== 1 ? 1 : 0);
5710 if (machine
->compat_props
) {
5711 qdev_prop_register_compat(machine
->compat_props
);
5713 machine
->init(ram_size
, boot_devices
,
5714 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5718 /* must be after terminal init, SDL library changes signal handlers */
5722 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5723 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5724 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5730 current_machine
= machine
;
5732 /* init USB devices */
5734 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5738 /* init generic devices */
5739 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5743 dumb_display_init();
5744 /* just use the first displaystate for the moment */
5747 if (display_type
== DT_DEFAULT
) {
5748 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5749 display_type
= DT_SDL
;
5751 display_type
= DT_VNC
;
5752 vnc_display
= "localhost:0,to=99";
5758 switch (display_type
) {
5761 #if defined(CONFIG_CURSES)
5763 curses_display_init(ds
, full_screen
);
5766 #if defined(CONFIG_SDL)
5768 sdl_display_init(ds
, full_screen
, no_frame
);
5770 #elif defined(CONFIG_COCOA)
5772 cocoa_display_init(ds
, full_screen
);
5776 vnc_display_init(ds
);
5777 if (vnc_display_open(ds
, vnc_display
) < 0)
5780 if (show_vnc_port
) {
5781 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5789 dcl
= ds
->listeners
;
5790 while (dcl
!= NULL
) {
5791 if (dcl
->dpy_refresh
!= NULL
) {
5792 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5793 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5798 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5799 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5800 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5803 text_consoles_set_display(display_state
);
5805 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5806 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5807 monitor_init(monitor_hds
[i
], monitor_flags
[i
]);
5811 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5812 const char *devname
= serial_devices
[i
];
5813 if (devname
&& strcmp(devname
, "none")) {
5814 if (strstart(devname
, "vc", 0))
5815 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5819 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5820 const char *devname
= parallel_devices
[i
];
5821 if (devname
&& strcmp(devname
, "none")) {
5822 if (strstart(devname
, "vc", 0))
5823 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5827 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5828 const char *devname
= virtio_consoles
[i
];
5829 if (virtcon_hds
[i
] && devname
) {
5830 if (strstart(devname
, "vc", 0))
5831 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5835 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5836 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5841 qdev_machine_creation_done();
5845 qemu_system_reset();
5847 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5853 qemu_start_incoming_migration(incoming
);
5854 } else if (autostart
) {
5864 len
= write(fds
[1], &status
, 1);
5865 if (len
== -1 && (errno
== EINTR
))
5872 TFR(fd
= open("/dev/null", O_RDWR
));
5878 pwd
= getpwnam(run_as
);
5880 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5886 if (chroot(chroot_dir
) < 0) {
5887 fprintf(stderr
, "chroot failed\n");
5894 if (setgid(pwd
->pw_gid
) < 0) {
5895 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5898 if (setuid(pwd
->pw_uid
) < 0) {
5899 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5902 if (setuid(0) != -1) {
5903 fprintf(stderr
, "Dropping privileges failed\n");