4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <linux/rtc.h>
63 #include <sys/prctl.h>
65 /* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67 /* #include <linux/hpet.h> */
70 #include <linux/ppdev.h>
71 #include <linux/parport.h>
75 #include <sys/ethernet.h>
76 #include <sys/sockio.h>
77 #include <netinet/arp.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> // must come after ip.h
82 #include <netinet/udp.h>
83 #include <netinet/tcp.h>
87 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89 extern int madvise(caddr_t
, size_t, int);
94 #if defined(__OpenBSD__)
98 #if defined(CONFIG_VDE)
99 #include <libvdeplug.h>
104 #include <mmsystem.h>
108 #if defined(__APPLE__) || defined(main)
110 int qemu_main(int argc
, char **argv
, char **envp
);
111 int main(int argc
, char **argv
)
113 return qemu_main(argc
, argv
, NULL
);
116 #define main qemu_main
118 #endif /* CONFIG_SDL */
122 #define main qemu_main
123 #endif /* CONFIG_COCOA */
126 #include "hw/boards.h"
128 #include "hw/pcmcia.h"
130 #include "hw/audiodev.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "block_int.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
157 #include "qemu-option.h"
158 #include "qemu-config.h"
159 #include "qemu-objects.h"
163 #include "exec-all.h"
165 #include "qemu_socket.h"
167 #include "slirp/libslirp.h"
169 #include "qemu-queue.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 #define MAX_VIRTIO_CONSOLES 1
178 static const char *data_dir
;
179 const char *bios_name
= NULL
;
180 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
181 to store the VM snapshots */
182 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
183 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
184 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
185 static DisplayState
*display_state
;
186 DisplayType display_type
= DT_DEFAULT
;
187 const char* keyboard_layout
= NULL
;
190 NICInfo nd_table
[MAX_NICS
];
193 static int rtc_utc
= 1;
194 static int rtc_date_offset
= -1; /* -1 means no change */
195 QEMUClock
*rtc_clock
;
196 int vga_interface_type
= VGA_NONE
;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static int timer_alarm_pending
= 1;
259 /* Conversion factor from emulated instructions to virtual clock ticks. */
260 static int icount_time_shift
;
261 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
262 #define MAX_ICOUNT_SHIFT 10
263 /* Compensate for varying guest execution speed. */
264 static int64_t qemu_icount_bias
;
265 static QEMUTimer
*icount_rt_timer
;
266 static QEMUTimer
*icount_vm_timer
;
267 static QEMUTimer
*nographic_timer
;
269 uint8_t qemu_uuid
[16];
271 static QEMUBootSetHandler
*boot_set_handler
;
272 static void *boot_set_opaque
;
274 static int default_serial
= 1;
275 static int default_parallel
= 1;
276 static int default_virtcon
= 1;
277 static int default_monitor
= 1;
278 static int default_vga
= 1;
279 static int default_floppy
= 1;
280 static int default_cdrom
= 1;
281 static int default_sdcard
= 1;
287 { .driver
= "isa-serial", .flag
= &default_serial
},
288 { .driver
= "isa-parallel", .flag
= &default_parallel
},
289 { .driver
= "isa-fdc", .flag
= &default_floppy
},
290 { .driver
= "ide-drive", .flag
= &default_cdrom
},
291 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
293 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
294 { .driver
= "VGA", .flag
= &default_vga
},
295 { .driver
= "cirrus-vga", .flag
= &default_vga
},
296 { .driver
= "vmware-svga", .flag
= &default_vga
},
299 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
301 const char *driver
= qemu_opt_get(opts
, "driver");
306 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
307 if (strcmp(default_list
[i
].driver
, driver
) != 0)
309 *(default_list
[i
].flag
) = 0;
314 /***********************************************************/
315 /* x86 ISA bus support */
317 target_phys_addr_t isa_mem_base
= 0;
320 /***********************************************************/
321 void hw_error(const char *fmt
, ...)
327 fprintf(stderr
, "qemu: hardware error: ");
328 vfprintf(stderr
, fmt
, ap
);
329 fprintf(stderr
, "\n");
330 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
331 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
333 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
335 cpu_dump_state(env
, stderr
, fprintf
, 0);
342 static void set_proc_name(const char *s
)
344 #if defined(__linux__) && defined(PR_SET_NAME)
348 name
[sizeof(name
) - 1] = 0;
349 strncpy(name
, s
, sizeof(name
));
350 /* Could rewrite argv[0] too, but that's a bit more complicated.
351 This simple way is enough for `top'. */
352 prctl(PR_SET_NAME
, name
);
359 static QEMUBalloonEvent
*qemu_balloon_event
;
360 void *qemu_balloon_event_opaque
;
362 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
364 qemu_balloon_event
= func
;
365 qemu_balloon_event_opaque
= opaque
;
368 void qemu_balloon(ram_addr_t target
)
370 if (qemu_balloon_event
)
371 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
374 ram_addr_t
qemu_balloon_status(void)
376 if (qemu_balloon_event
)
377 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
382 /***********************************************************/
383 /* real time host monotonic timer */
385 /* compute with 96 bit intermediate result: (a*b)/c */
386 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
391 #ifdef HOST_WORDS_BIGENDIAN
401 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
402 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
405 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
409 static int64_t get_clock_realtime(void)
413 gettimeofday(&tv
, NULL
);
414 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
419 static int64_t clock_freq
;
421 static void init_get_clock(void)
425 ret
= QueryPerformanceFrequency(&freq
);
427 fprintf(stderr
, "Could not calibrate ticks\n");
430 clock_freq
= freq
.QuadPart
;
433 static int64_t get_clock(void)
436 QueryPerformanceCounter(&ti
);
437 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
442 static int use_rt_clock
;
444 static void init_get_clock(void)
447 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
448 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
451 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
458 static int64_t get_clock(void)
460 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
461 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
464 clock_gettime(CLOCK_MONOTONIC
, &ts
);
465 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
469 /* XXX: using gettimeofday leads to problems if the date
470 changes, so it should be avoided. */
471 return get_clock_realtime();
476 /* Return the virtual CPU time, based on the instruction counter. */
477 static int64_t cpu_get_icount(void)
480 CPUState
*env
= cpu_single_env
;;
481 icount
= qemu_icount
;
484 fprintf(stderr
, "Bad clock read\n");
485 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
487 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
490 /***********************************************************/
491 /* guest cycle counter */
493 typedef struct TimersState
{
494 int64_t cpu_ticks_prev
;
495 int64_t cpu_ticks_offset
;
496 int64_t cpu_clock_offset
;
497 int32_t cpu_ticks_enabled
;
501 TimersState timers_state
;
503 /* return the host CPU cycle counter and handle stop/restart */
504 int64_t cpu_get_ticks(void)
507 return cpu_get_icount();
509 if (!timers_state
.cpu_ticks_enabled
) {
510 return timers_state
.cpu_ticks_offset
;
513 ticks
= cpu_get_real_ticks();
514 if (timers_state
.cpu_ticks_prev
> ticks
) {
515 /* Note: non increasing ticks may happen if the host uses
517 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
519 timers_state
.cpu_ticks_prev
= ticks
;
520 return ticks
+ timers_state
.cpu_ticks_offset
;
524 /* return the host CPU monotonic timer and handle stop/restart */
525 static int64_t cpu_get_clock(void)
528 if (!timers_state
.cpu_ticks_enabled
) {
529 return timers_state
.cpu_clock_offset
;
532 return ti
+ timers_state
.cpu_clock_offset
;
536 /* enable cpu_get_ticks() */
537 void cpu_enable_ticks(void)
539 if (!timers_state
.cpu_ticks_enabled
) {
540 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
541 timers_state
.cpu_clock_offset
-= get_clock();
542 timers_state
.cpu_ticks_enabled
= 1;
546 /* disable cpu_get_ticks() : the clock is stopped. You must not call
547 cpu_get_ticks() after that. */
548 void cpu_disable_ticks(void)
550 if (timers_state
.cpu_ticks_enabled
) {
551 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
552 timers_state
.cpu_clock_offset
= cpu_get_clock();
553 timers_state
.cpu_ticks_enabled
= 0;
557 /***********************************************************/
560 #define QEMU_CLOCK_REALTIME 0
561 #define QEMU_CLOCK_VIRTUAL 1
562 #define QEMU_CLOCK_HOST 2
566 /* XXX: add frequency */
574 struct QEMUTimer
*next
;
577 struct qemu_alarm_timer
{
581 int (*start
)(struct qemu_alarm_timer
*t
);
582 void (*stop
)(struct qemu_alarm_timer
*t
);
583 void (*rearm
)(struct qemu_alarm_timer
*t
);
587 #define ALARM_FLAG_DYNTICKS 0x1
588 #define ALARM_FLAG_EXPIRED 0x2
590 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
592 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
595 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
597 if (!alarm_has_dynticks(t
))
603 /* TODO: MIN_TIMER_REARM_US should be optimized */
604 #define MIN_TIMER_REARM_US 250
606 static struct qemu_alarm_timer
*alarm_timer
;
610 struct qemu_alarm_win32
{
613 } alarm_win32_data
= {0, -1};
615 static int win32_start_timer(struct qemu_alarm_timer
*t
);
616 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
617 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
621 static int unix_start_timer(struct qemu_alarm_timer
*t
);
622 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
626 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
627 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
628 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
630 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
631 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
633 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
634 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
636 #endif /* __linux__ */
640 /* Correlation between real and virtual time is always going to be
641 fairly approximate, so ignore small variation.
642 When the guest is idle real and virtual time will be aligned in
644 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
646 static void icount_adjust(void)
651 static int64_t last_delta
;
652 /* If the VM is not running, then do nothing. */
656 cur_time
= cpu_get_clock();
657 cur_icount
= qemu_get_clock(vm_clock
);
658 delta
= cur_icount
- cur_time
;
659 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
661 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
662 && icount_time_shift
> 0) {
663 /* The guest is getting too far ahead. Slow time down. */
667 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
668 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
669 /* The guest is getting too far behind. Speed time up. */
673 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
676 static void icount_adjust_rt(void * opaque
)
678 qemu_mod_timer(icount_rt_timer
,
679 qemu_get_clock(rt_clock
) + 1000);
683 static void icount_adjust_vm(void * opaque
)
685 qemu_mod_timer(icount_vm_timer
,
686 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
690 static void init_icount_adjust(void)
692 /* Have both realtime and virtual time triggers for speed adjustment.
693 The realtime trigger catches emulated time passing too slowly,
694 the virtual time trigger catches emulated time passing too fast.
695 Realtime triggers occur even when idle, so use them less frequently
697 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
698 qemu_mod_timer(icount_rt_timer
,
699 qemu_get_clock(rt_clock
) + 1000);
700 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
701 qemu_mod_timer(icount_vm_timer
,
702 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
705 static struct qemu_alarm_timer alarm_timers
[] = {
708 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
709 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
710 /* HPET - if available - is preferred */
711 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
712 /* ...otherwise try RTC */
713 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
715 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
717 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
718 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
719 {"win32", 0, win32_start_timer
,
720 win32_stop_timer
, NULL
, &alarm_win32_data
},
725 static void show_available_alarms(void)
729 printf("Available alarm timers, in order of precedence:\n");
730 for (i
= 0; alarm_timers
[i
].name
; i
++)
731 printf("%s\n", alarm_timers
[i
].name
);
734 static void configure_alarms(char const *opt
)
738 int count
= ARRAY_SIZE(alarm_timers
) - 1;
741 struct qemu_alarm_timer tmp
;
743 if (!strcmp(opt
, "?")) {
744 show_available_alarms();
748 arg
= qemu_strdup(opt
);
750 /* Reorder the array */
751 name
= strtok(arg
, ",");
753 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
754 if (!strcmp(alarm_timers
[i
].name
, name
))
759 fprintf(stderr
, "Unknown clock %s\n", name
);
768 tmp
= alarm_timers
[i
];
769 alarm_timers
[i
] = alarm_timers
[cur
];
770 alarm_timers
[cur
] = tmp
;
774 name
= strtok(NULL
, ",");
780 /* Disable remaining timers */
781 for (i
= cur
; i
< count
; i
++)
782 alarm_timers
[i
].name
= NULL
;
784 show_available_alarms();
789 #define QEMU_NUM_CLOCKS 3
793 QEMUClock
*host_clock
;
795 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
797 static QEMUClock
*qemu_new_clock(int type
)
800 clock
= qemu_mallocz(sizeof(QEMUClock
));
805 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
809 ts
= qemu_mallocz(sizeof(QEMUTimer
));
816 void qemu_free_timer(QEMUTimer
*ts
)
821 /* stop a timer, but do not dealloc it */
822 void qemu_del_timer(QEMUTimer
*ts
)
826 /* NOTE: this code must be signal safe because
827 qemu_timer_expired() can be called from a signal. */
828 pt
= &active_timers
[ts
->clock
->type
];
841 /* modify the current timer so that it will be fired when current_time
842 >= expire_time. The corresponding callback will be called. */
843 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
849 /* add the timer in the sorted list */
850 /* NOTE: this code must be signal safe because
851 qemu_timer_expired() can be called from a signal. */
852 pt
= &active_timers
[ts
->clock
->type
];
857 if (t
->expire_time
> expire_time
)
861 ts
->expire_time
= expire_time
;
865 /* Rearm if necessary */
866 if (pt
== &active_timers
[ts
->clock
->type
]) {
867 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
868 qemu_rearm_alarm_timer(alarm_timer
);
870 /* Interrupt execution to force deadline recalculation. */
876 int qemu_timer_pending(QEMUTimer
*ts
)
879 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
886 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
890 return (timer_head
->expire_time
<= current_time
);
893 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
899 if (!ts
|| ts
->expire_time
> current_time
)
901 /* remove timer from the list before calling the callback */
902 *ptimer_head
= ts
->next
;
905 /* run the callback (the timer list can be modified) */
910 int64_t qemu_get_clock(QEMUClock
*clock
)
912 switch(clock
->type
) {
913 case QEMU_CLOCK_REALTIME
:
914 return get_clock() / 1000000;
916 case QEMU_CLOCK_VIRTUAL
:
918 return cpu_get_icount();
920 return cpu_get_clock();
922 case QEMU_CLOCK_HOST
:
923 return get_clock_realtime();
927 static void init_clocks(void)
930 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
931 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
932 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
934 rtc_clock
= host_clock
;
938 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
940 uint64_t expire_time
;
942 if (qemu_timer_pending(ts
)) {
943 expire_time
= ts
->expire_time
;
947 qemu_put_be64(f
, expire_time
);
950 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
952 uint64_t expire_time
;
954 expire_time
= qemu_get_be64(f
);
955 if (expire_time
!= -1) {
956 qemu_mod_timer(ts
, expire_time
);
962 static const VMStateDescription vmstate_timers
= {
965 .minimum_version_id
= 1,
966 .minimum_version_id_old
= 1,
967 .fields
= (VMStateField
[]) {
968 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
969 VMSTATE_INT64(dummy
, TimersState
),
970 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
971 VMSTATE_END_OF_LIST()
975 static void qemu_event_increment(void);
978 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
979 DWORD_PTR dwUser
, DWORD_PTR dw1
,
982 static void host_alarm_handler(int host_signum
)
986 #define DISP_FREQ 1000
988 static int64_t delta_min
= INT64_MAX
;
989 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
991 ti
= qemu_get_clock(vm_clock
);
992 if (last_clock
!= 0) {
993 delta
= ti
- last_clock
;
994 if (delta
< delta_min
)
996 if (delta
> delta_max
)
999 if (++count
== DISP_FREQ
) {
1000 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1001 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1002 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1003 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1004 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1006 delta_min
= INT64_MAX
;
1014 if (alarm_has_dynticks(alarm_timer
) ||
1016 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1017 qemu_get_clock(vm_clock
))) ||
1018 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1019 qemu_get_clock(rt_clock
)) ||
1020 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1021 qemu_get_clock(host_clock
))) {
1022 qemu_event_increment();
1023 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1025 #ifndef CONFIG_IOTHREAD
1027 /* stop the currently executing cpu because a timer occured */
1031 timer_alarm_pending
= 1;
1032 qemu_notify_event();
1036 static int64_t qemu_next_deadline(void)
1038 /* To avoid problems with overflow limit this to 2^32. */
1039 int64_t delta
= INT32_MAX
;
1041 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1042 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1043 qemu_get_clock(vm_clock
);
1045 if (active_timers
[QEMU_CLOCK_HOST
]) {
1046 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1047 qemu_get_clock(host_clock
);
1058 #if defined(__linux__)
1059 static uint64_t qemu_next_deadline_dyntick(void)
1067 delta
= (qemu_next_deadline() + 999) / 1000;
1069 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1070 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1071 qemu_get_clock(rt_clock
))*1000;
1072 if (rtdelta
< delta
)
1076 if (delta
< MIN_TIMER_REARM_US
)
1077 delta
= MIN_TIMER_REARM_US
;
1085 /* Sets a specific flag */
1086 static int fcntl_setfl(int fd
, int flag
)
1090 flags
= fcntl(fd
, F_GETFL
);
1094 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1100 #if defined(__linux__)
1102 #define RTC_FREQ 1024
1104 static void enable_sigio_timer(int fd
)
1106 struct sigaction act
;
1109 sigfillset(&act
.sa_mask
);
1111 act
.sa_handler
= host_alarm_handler
;
1113 sigaction(SIGIO
, &act
, NULL
);
1114 fcntl_setfl(fd
, O_ASYNC
);
1115 fcntl(fd
, F_SETOWN
, getpid());
1118 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1120 struct hpet_info info
;
1123 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1128 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1130 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1131 "error, but for better emulation accuracy type:\n"
1132 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1136 /* Check capabilities */
1137 r
= ioctl(fd
, HPET_INFO
, &info
);
1141 /* Enable periodic mode */
1142 r
= ioctl(fd
, HPET_EPI
, 0);
1143 if (info
.hi_flags
&& (r
< 0))
1146 /* Enable interrupt */
1147 r
= ioctl(fd
, HPET_IE_ON
, 0);
1151 enable_sigio_timer(fd
);
1152 t
->priv
= (void *)(long)fd
;
1160 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1162 int fd
= (long)t
->priv
;
1167 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1170 unsigned long current_rtc_freq
= 0;
1172 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1175 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1176 if (current_rtc_freq
!= RTC_FREQ
&&
1177 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1178 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1179 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1180 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1183 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1189 enable_sigio_timer(rtc_fd
);
1191 t
->priv
= (void *)(long)rtc_fd
;
1196 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1198 int rtc_fd
= (long)t
->priv
;
1203 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1207 struct sigaction act
;
1209 sigfillset(&act
.sa_mask
);
1211 act
.sa_handler
= host_alarm_handler
;
1213 sigaction(SIGALRM
, &act
, NULL
);
1216 * Initialize ev struct to 0 to avoid valgrind complaining
1217 * about uninitialized data in timer_create call
1219 memset(&ev
, 0, sizeof(ev
));
1220 ev
.sigev_value
.sival_int
= 0;
1221 ev
.sigev_notify
= SIGEV_SIGNAL
;
1222 ev
.sigev_signo
= SIGALRM
;
1224 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1225 perror("timer_create");
1227 /* disable dynticks */
1228 fprintf(stderr
, "Dynamic Ticks disabled\n");
1233 t
->priv
= (void *)(long)host_timer
;
1238 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1240 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1242 timer_delete(host_timer
);
1245 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1247 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1248 struct itimerspec timeout
;
1249 int64_t nearest_delta_us
= INT64_MAX
;
1252 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1253 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1254 !active_timers
[QEMU_CLOCK_HOST
])
1257 nearest_delta_us
= qemu_next_deadline_dyntick();
1259 /* check whether a timer is already running */
1260 if (timer_gettime(host_timer
, &timeout
)) {
1262 fprintf(stderr
, "Internal timer error: aborting\n");
1265 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1266 if (current_us
&& current_us
<= nearest_delta_us
)
1269 timeout
.it_interval
.tv_sec
= 0;
1270 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1271 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1272 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1273 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1275 fprintf(stderr
, "Internal timer error: aborting\n");
1280 #endif /* defined(__linux__) */
1282 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1284 struct sigaction act
;
1285 struct itimerval itv
;
1289 sigfillset(&act
.sa_mask
);
1291 act
.sa_handler
= host_alarm_handler
;
1293 sigaction(SIGALRM
, &act
, NULL
);
1295 itv
.it_interval
.tv_sec
= 0;
1296 /* for i386 kernel 2.6 to get 1 ms */
1297 itv
.it_interval
.tv_usec
= 999;
1298 itv
.it_value
.tv_sec
= 0;
1299 itv
.it_value
.tv_usec
= 10 * 1000;
1301 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1308 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1310 struct itimerval itv
;
1312 memset(&itv
, 0, sizeof(itv
));
1313 setitimer(ITIMER_REAL
, &itv
, NULL
);
1316 #endif /* !defined(_WIN32) */
1321 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1324 struct qemu_alarm_win32
*data
= t
->priv
;
1327 memset(&tc
, 0, sizeof(tc
));
1328 timeGetDevCaps(&tc
, sizeof(tc
));
1330 if (data
->period
< tc
.wPeriodMin
)
1331 data
->period
= tc
.wPeriodMin
;
1333 timeBeginPeriod(data
->period
);
1335 flags
= TIME_CALLBACK_FUNCTION
;
1336 if (alarm_has_dynticks(t
))
1337 flags
|= TIME_ONESHOT
;
1339 flags
|= TIME_PERIODIC
;
1341 data
->timerId
= timeSetEvent(1, // interval (ms)
1342 data
->period
, // resolution
1343 host_alarm_handler
, // function
1344 (DWORD
)t
, // parameter
1347 if (!data
->timerId
) {
1348 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1350 timeEndPeriod(data
->period
);
1357 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1359 struct qemu_alarm_win32
*data
= t
->priv
;
1361 timeKillEvent(data
->timerId
);
1362 timeEndPeriod(data
->period
);
1365 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1367 struct qemu_alarm_win32
*data
= t
->priv
;
1369 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1370 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1371 !active_timers
[QEMU_CLOCK_HOST
])
1374 timeKillEvent(data
->timerId
);
1376 data
->timerId
= timeSetEvent(1,
1380 TIME_ONESHOT
| TIME_PERIODIC
);
1382 if (!data
->timerId
) {
1383 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1386 timeEndPeriod(data
->period
);
1393 static int init_timer_alarm(void)
1395 struct qemu_alarm_timer
*t
= NULL
;
1398 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1399 t
= &alarm_timers
[i
];
1419 static void quit_timers(void)
1421 alarm_timer
->stop(alarm_timer
);
1425 /***********************************************************/
1426 /* host time/date access */
1427 void qemu_get_timedate(struct tm
*tm
, int offset
)
1434 if (rtc_date_offset
== -1) {
1438 ret
= localtime(&ti
);
1440 ti
-= rtc_date_offset
;
1444 memcpy(tm
, ret
, sizeof(struct tm
));
1447 int qemu_timedate_diff(struct tm
*tm
)
1451 if (rtc_date_offset
== -1)
1453 seconds
= mktimegm(tm
);
1455 seconds
= mktime(tm
);
1457 seconds
= mktimegm(tm
) + rtc_date_offset
;
1459 return seconds
- time(NULL
);
1462 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1464 time_t rtc_start_date
;
1467 if (!strcmp(startdate
, "now") && legacy
) {
1468 rtc_date_offset
= -1;
1470 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1478 } else if (sscanf(startdate
, "%d-%d-%d",
1481 &tm
.tm_mday
) == 3) {
1490 rtc_start_date
= mktimegm(&tm
);
1491 if (rtc_start_date
== -1) {
1493 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1494 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1497 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1501 static void configure_rtc(QemuOpts
*opts
)
1505 value
= qemu_opt_get(opts
, "base");
1507 if (!strcmp(value
, "utc")) {
1509 } else if (!strcmp(value
, "localtime")) {
1512 configure_rtc_date_offset(value
, 0);
1515 value
= qemu_opt_get(opts
, "clock");
1517 if (!strcmp(value
, "host")) {
1518 rtc_clock
= host_clock
;
1519 } else if (!strcmp(value
, "vm")) {
1520 rtc_clock
= vm_clock
;
1522 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1526 #ifdef CONFIG_TARGET_I386
1527 value
= qemu_opt_get(opts
, "driftfix");
1529 if (!strcmp(buf
, "slew")) {
1531 } else if (!strcmp(buf
, "none")) {
1534 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1542 static void socket_cleanup(void)
1547 static int socket_init(void)
1552 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1554 err
= WSAGetLastError();
1555 fprintf(stderr
, "WSAStartup: %d\n", err
);
1558 atexit(socket_cleanup
);
1563 /***********************************************************/
1564 /* Bluetooth support */
1567 static struct HCIInfo
*hci_table
[MAX_NICS
];
1569 static struct bt_vlan_s
{
1570 struct bt_scatternet_s net
;
1572 struct bt_vlan_s
*next
;
1575 /* find or alloc a new bluetooth "VLAN" */
1576 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1578 struct bt_vlan_s
**pvlan
, *vlan
;
1579 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1583 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1585 pvlan
= &first_bt_vlan
;
1586 while (*pvlan
!= NULL
)
1587 pvlan
= &(*pvlan
)->next
;
1592 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1596 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1601 static struct HCIInfo null_hci
= {
1602 .cmd_send
= null_hci_send
,
1603 .sco_send
= null_hci_send
,
1604 .acl_send
= null_hci_send
,
1605 .bdaddr_set
= null_hci_addr_set
,
1608 struct HCIInfo
*qemu_next_hci(void)
1610 if (cur_hci
== nb_hcis
)
1613 return hci_table
[cur_hci
++];
1616 static struct HCIInfo
*hci_init(const char *str
)
1619 struct bt_scatternet_s
*vlan
= 0;
1621 if (!strcmp(str
, "null"))
1624 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1626 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1627 else if (!strncmp(str
, "hci", 3)) {
1630 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1631 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1636 vlan
= qemu_find_bt_vlan(0);
1638 return bt_new_hci(vlan
);
1641 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1646 static int bt_hci_parse(const char *str
)
1648 struct HCIInfo
*hci
;
1651 if (nb_hcis
>= MAX_NICS
) {
1652 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1656 hci
= hci_init(str
);
1665 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1666 hci
->bdaddr_set(hci
, bdaddr
.b
);
1668 hci_table
[nb_hcis
++] = hci
;
1673 static void bt_vhci_add(int vlan_id
)
1675 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1678 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1679 "an empty scatternet %i\n", vlan_id
);
1681 bt_vhci_init(bt_new_hci(vlan
));
1684 static struct bt_device_s
*bt_device_add(const char *opt
)
1686 struct bt_scatternet_s
*vlan
;
1688 char *endp
= strstr(opt
, ",vlan=");
1689 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1692 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1695 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1697 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1702 vlan
= qemu_find_bt_vlan(vlan_id
);
1705 fprintf(stderr
, "qemu: warning: adding a slave device to "
1706 "an empty scatternet %i\n", vlan_id
);
1708 if (!strcmp(devname
, "keyboard"))
1709 return bt_keyboard_init(vlan
);
1711 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1715 static int bt_parse(const char *opt
)
1717 const char *endp
, *p
;
1720 if (strstart(opt
, "hci", &endp
)) {
1721 if (!*endp
|| *endp
== ',') {
1723 if (!strstart(endp
, ",vlan=", 0))
1726 return bt_hci_parse(opt
);
1728 } else if (strstart(opt
, "vhci", &endp
)) {
1729 if (!*endp
|| *endp
== ',') {
1731 if (strstart(endp
, ",vlan=", &p
)) {
1732 vlan
= strtol(p
, (char **) &endp
, 0);
1734 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1738 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1747 } else if (strstart(opt
, "device:", &endp
))
1748 return !bt_device_add(endp
);
1750 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1754 /***********************************************************/
1755 /* QEMU Block devices */
1757 #define HD_ALIAS "index=%d,media=disk"
1758 #define CDROM_ALIAS "index=2,media=cdrom"
1759 #define FD_ALIAS "index=%d,if=floppy"
1760 #define PFLASH_ALIAS "if=pflash"
1761 #define MTD_ALIAS "if=mtd"
1762 #define SD_ALIAS "index=0,if=sd"
1764 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1771 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1774 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1776 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1777 __FUNCTION__
, optstr
);
1781 qemu_opt_set(opts
, "file", file
);
1785 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1789 /* seek interface, bus and unit */
1791 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1792 if (dinfo
->type
== type
&&
1793 dinfo
->bus
== bus
&&
1794 dinfo
->unit
== unit
)
1801 DriveInfo
*drive_get_by_id(const char *id
)
1805 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1806 if (strcmp(id
, dinfo
->id
))
1813 int drive_get_max_bus(BlockInterfaceType type
)
1819 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1820 if(dinfo
->type
== type
&&
1821 dinfo
->bus
> max_bus
)
1822 max_bus
= dinfo
->bus
;
1827 const char *drive_get_serial(BlockDriverState
*bdrv
)
1831 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1832 if (dinfo
->bdrv
== bdrv
)
1833 return dinfo
->serial
;
1839 BlockInterfaceErrorAction
drive_get_on_error(
1840 BlockDriverState
*bdrv
, int is_read
)
1844 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1845 if (dinfo
->bdrv
== bdrv
)
1846 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1849 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1852 static void bdrv_format_print(void *opaque
, const char *name
)
1854 fprintf(stderr
, " %s", name
);
1857 void drive_uninit(DriveInfo
*dinfo
)
1859 qemu_opts_del(dinfo
->opts
);
1860 bdrv_delete(dinfo
->bdrv
);
1861 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1865 static int parse_block_error_action(const char *buf
, int is_read
)
1867 if (!strcmp(buf
, "ignore")) {
1868 return BLOCK_ERR_IGNORE
;
1869 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1870 return BLOCK_ERR_STOP_ENOSPC
;
1871 } else if (!strcmp(buf
, "stop")) {
1872 return BLOCK_ERR_STOP_ANY
;
1873 } else if (!strcmp(buf
, "report")) {
1874 return BLOCK_ERR_REPORT
;
1876 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1877 buf
, is_read
? "read" : "write");
1882 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1886 const char *file
= NULL
;
1889 const char *mediastr
= "";
1890 BlockInterfaceType type
;
1891 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1892 int bus_id
, unit_id
;
1893 int cyls
, heads
, secs
, translation
;
1894 BlockDriver
*drv
= NULL
;
1895 QEMUMachine
*machine
= opaque
;
1902 int on_read_error
, on_write_error
;
1903 const char *devaddr
;
1909 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1912 if (machine
&& machine
->use_scsi
) {
1914 max_devs
= MAX_SCSI_DEVS
;
1915 pstrcpy(devname
, sizeof(devname
), "scsi");
1918 max_devs
= MAX_IDE_DEVS
;
1919 pstrcpy(devname
, sizeof(devname
), "ide");
1923 /* extract parameters */
1924 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1925 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1926 index
= qemu_opt_get_number(opts
, "index", -1);
1928 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1929 heads
= qemu_opt_get_number(opts
, "heads", 0);
1930 secs
= qemu_opt_get_number(opts
, "secs", 0);
1932 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1933 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1935 file
= qemu_opt_get(opts
, "file");
1936 serial
= qemu_opt_get(opts
, "serial");
1938 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1939 pstrcpy(devname
, sizeof(devname
), buf
);
1940 if (!strcmp(buf
, "ide")) {
1942 max_devs
= MAX_IDE_DEVS
;
1943 } else if (!strcmp(buf
, "scsi")) {
1945 max_devs
= MAX_SCSI_DEVS
;
1946 } else if (!strcmp(buf
, "floppy")) {
1949 } else if (!strcmp(buf
, "pflash")) {
1952 } else if (!strcmp(buf
, "mtd")) {
1955 } else if (!strcmp(buf
, "sd")) {
1958 } else if (!strcmp(buf
, "virtio")) {
1961 } else if (!strcmp(buf
, "xen")) {
1964 } else if (!strcmp(buf
, "none")) {
1968 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1973 if (cyls
|| heads
|| secs
) {
1974 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
1975 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1978 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
1979 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1982 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1983 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1988 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1991 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1995 if (!strcmp(buf
, "none"))
1996 translation
= BIOS_ATA_TRANSLATION_NONE
;
1997 else if (!strcmp(buf
, "lba"))
1998 translation
= BIOS_ATA_TRANSLATION_LBA
;
1999 else if (!strcmp(buf
, "auto"))
2000 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2002 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2007 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2008 if (!strcmp(buf
, "disk")) {
2010 } else if (!strcmp(buf
, "cdrom")) {
2011 if (cyls
|| secs
|| heads
) {
2013 "qemu: '%s' invalid physical CHS format\n", buf
);
2016 media
= MEDIA_CDROM
;
2018 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2023 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2024 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2026 else if (!strcmp(buf
, "writethrough"))
2028 else if (!strcmp(buf
, "writeback"))
2031 fprintf(stderr
, "qemu: invalid cache option\n");
2036 #ifdef CONFIG_LINUX_AIO
2037 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2038 if (!strcmp(buf
, "threads"))
2040 else if (!strcmp(buf
, "native"))
2043 fprintf(stderr
, "qemu: invalid aio option\n");
2049 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2050 if (strcmp(buf
, "?") == 0) {
2051 fprintf(stderr
, "qemu: Supported formats:");
2052 bdrv_iterate_format(bdrv_format_print
, NULL
);
2053 fprintf(stderr
, "\n");
2056 drv
= bdrv_find_whitelisted_format(buf
);
2058 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2063 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2064 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2065 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2066 fprintf(stderr
, "werror is no supported by this format\n");
2070 on_write_error
= parse_block_error_action(buf
, 0);
2071 if (on_write_error
< 0) {
2076 on_read_error
= BLOCK_ERR_REPORT
;
2077 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2078 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2079 fprintf(stderr
, "rerror is no supported by this format\n");
2083 on_read_error
= parse_block_error_action(buf
, 1);
2084 if (on_read_error
< 0) {
2089 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2090 if (type
!= IF_VIRTIO
) {
2091 fprintf(stderr
, "addr is not supported\n");
2096 /* compute bus and unit according index */
2099 if (bus_id
!= 0 || unit_id
!= -1) {
2101 "qemu: index cannot be used with bus and unit\n");
2109 unit_id
= index
% max_devs
;
2110 bus_id
= index
/ max_devs
;
2114 /* if user doesn't specify a unit_id,
2115 * try to find the first free
2118 if (unit_id
== -1) {
2120 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2122 if (max_devs
&& unit_id
>= max_devs
) {
2123 unit_id
-= max_devs
;
2131 if (max_devs
&& unit_id
>= max_devs
) {
2132 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2133 unit_id
, max_devs
- 1);
2138 * ignore multiple definitions
2141 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2148 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2149 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2150 dinfo
->id
= qemu_strdup(buf
);
2152 /* no id supplied -> create one */
2153 dinfo
->id
= qemu_mallocz(32);
2154 if (type
== IF_IDE
|| type
== IF_SCSI
)
2155 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2157 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2158 devname
, bus_id
, mediastr
, unit_id
);
2160 snprintf(dinfo
->id
, 32, "%s%s%i",
2161 devname
, mediastr
, unit_id
);
2163 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2164 dinfo
->devaddr
= devaddr
;
2166 dinfo
->bus
= bus_id
;
2167 dinfo
->unit
= unit_id
;
2168 dinfo
->on_read_error
= on_read_error
;
2169 dinfo
->on_write_error
= on_write_error
;
2172 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2173 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2183 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2184 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2188 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2193 /* FIXME: This isn't really a floppy, but it's a reasonable
2196 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2202 /* add virtio block device */
2203 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2204 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2205 qemu_opt_set(opts
, "drive", dinfo
->id
);
2207 qemu_opt_set(opts
, "addr", devaddr
);
2218 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2219 cache
= 2; /* always use write-back with snapshot */
2221 if (cache
== 0) /* no caching */
2222 bdrv_flags
|= BDRV_O_NOCACHE
;
2223 else if (cache
== 2) /* write-back */
2224 bdrv_flags
|= BDRV_O_CACHE_WB
;
2227 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2229 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2233 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2234 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2239 * cdrom is read-only. Set it now, after above interface checking
2240 * since readonly attribute not explicitly required, so no error.
2242 if (media
== MEDIA_CDROM
) {
2245 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2247 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2248 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2249 file
, strerror(errno
));
2253 if (bdrv_key_required(dinfo
->bdrv
))
2259 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2261 QEMUMachine
*machine
= opaque
;
2262 int fatal_error
= 0;
2264 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2271 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2273 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2274 qemu_opt_set(opts
, "snapshot", "on");
2279 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2281 boot_set_handler
= func
;
2282 boot_set_opaque
= opaque
;
2285 int qemu_boot_set(const char *boot_devices
)
2287 if (!boot_set_handler
) {
2290 return boot_set_handler(boot_set_opaque
, boot_devices
);
2293 static int parse_bootdevices(char *devices
)
2295 /* We just do some generic consistency checks */
2299 for (p
= devices
; *p
!= '\0'; p
++) {
2300 /* Allowed boot devices are:
2301 * a-b: floppy disk drives
2302 * c-f: IDE disk drives
2303 * g-m: machine implementation dependant drives
2304 * n-p: network devices
2305 * It's up to each machine implementation to check if the given boot
2306 * devices match the actual hardware implementation and firmware
2309 if (*p
< 'a' || *p
> 'p') {
2310 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2313 if (bitmap
& (1 << (*p
- 'a'))) {
2314 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2317 bitmap
|= 1 << (*p
- 'a');
2322 static void restore_boot_devices(void *opaque
)
2324 char *standard_boot_devices
= opaque
;
2326 qemu_boot_set(standard_boot_devices
);
2328 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2329 qemu_free(standard_boot_devices
);
2332 static void numa_add(const char *optarg
)
2336 unsigned long long value
, endvalue
;
2339 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2340 if (!strcmp(option
, "node")) {
2341 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2342 nodenr
= nb_numa_nodes
;
2344 nodenr
= strtoull(option
, NULL
, 10);
2347 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2348 node_mem
[nodenr
] = 0;
2350 value
= strtoull(option
, &endptr
, 0);
2352 case 0: case 'M': case 'm':
2359 node_mem
[nodenr
] = value
;
2361 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2362 node_cpumask
[nodenr
] = 0;
2364 value
= strtoull(option
, &endptr
, 10);
2367 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2369 if (*endptr
== '-') {
2370 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2371 if (endvalue
>= 63) {
2374 "only 63 CPUs in NUMA mode supported.\n");
2376 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2381 node_cpumask
[nodenr
] = value
;
2388 static void smp_parse(const char *optarg
)
2390 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2394 smp
= strtoul(optarg
, &endptr
, 10);
2395 if (endptr
!= optarg
) {
2396 if (*endptr
== ',') {
2400 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2401 sockets
= strtoull(option
, NULL
, 10);
2402 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2403 cores
= strtoull(option
, NULL
, 10);
2404 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2405 threads
= strtoull(option
, NULL
, 10);
2406 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2407 max_cpus
= strtoull(option
, NULL
, 10);
2409 /* compute missing values, prefer sockets over cores over threads */
2410 if (smp
== 0 || sockets
== 0) {
2411 sockets
= sockets
> 0 ? sockets
: 1;
2412 cores
= cores
> 0 ? cores
: 1;
2413 threads
= threads
> 0 ? threads
: 1;
2415 smp
= cores
* threads
* sockets
;
2419 threads
= threads
> 0 ? threads
: 1;
2420 cores
= smp
/ (sockets
* threads
);
2423 threads
= smp
/ (cores
* sockets
);
2428 smp_cores
= cores
> 0 ? cores
: 1;
2429 smp_threads
= threads
> 0 ? threads
: 1;
2431 max_cpus
= smp_cpus
;
2434 /***********************************************************/
2437 static int usb_device_add(const char *devname
, int is_hotplug
)
2440 USBDevice
*dev
= NULL
;
2445 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2446 dev
= usbdevice_create(devname
);
2450 /* the other ones */
2451 if (strstart(devname
, "host:", &p
)) {
2452 dev
= usb_host_device_open(p
);
2453 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2454 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2455 bt_new_hci(qemu_find_bt_vlan(0)));
2466 static int usb_device_del(const char *devname
)
2471 if (strstart(devname
, "host:", &p
))
2472 return usb_host_device_close(p
);
2477 p
= strchr(devname
, '.');
2480 bus_num
= strtoul(devname
, NULL
, 0);
2481 addr
= strtoul(p
+ 1, NULL
, 0);
2483 return usb_device_delete_addr(bus_num
, addr
);
2486 static int usb_parse(const char *cmdline
)
2489 r
= usb_device_add(cmdline
, 0);
2491 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2496 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2498 const char *devname
= qdict_get_str(qdict
, "devname");
2499 if (usb_device_add(devname
, 1) < 0) {
2500 qemu_error("could not add USB device '%s'\n", devname
);
2504 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2506 const char *devname
= qdict_get_str(qdict
, "devname");
2507 if (usb_device_del(devname
) < 0) {
2508 qemu_error("could not delete USB device '%s'\n", devname
);
2512 /***********************************************************/
2513 /* PCMCIA/Cardbus */
2515 static struct pcmcia_socket_entry_s
{
2516 PCMCIASocket
*socket
;
2517 struct pcmcia_socket_entry_s
*next
;
2518 } *pcmcia_sockets
= 0;
2520 void pcmcia_socket_register(PCMCIASocket
*socket
)
2522 struct pcmcia_socket_entry_s
*entry
;
2524 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2525 entry
->socket
= socket
;
2526 entry
->next
= pcmcia_sockets
;
2527 pcmcia_sockets
= entry
;
2530 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2532 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2534 ptr
= &pcmcia_sockets
;
2535 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2536 if (entry
->socket
== socket
) {
2542 void pcmcia_info(Monitor
*mon
)
2544 struct pcmcia_socket_entry_s
*iter
;
2546 if (!pcmcia_sockets
)
2547 monitor_printf(mon
, "No PCMCIA sockets\n");
2549 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2550 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2551 iter
->socket
->attached
? iter
->socket
->card_string
:
2555 /***********************************************************/
2556 /* register display */
2558 struct DisplayAllocator default_allocator
= {
2559 defaultallocator_create_displaysurface
,
2560 defaultallocator_resize_displaysurface
,
2561 defaultallocator_free_displaysurface
2564 void register_displaystate(DisplayState
*ds
)
2574 DisplayState
*get_displaystate(void)
2576 return display_state
;
2579 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2581 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2582 return ds
->allocator
;
2587 static void dumb_display_init(void)
2589 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2590 ds
->allocator
= &default_allocator
;
2591 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2592 register_displaystate(ds
);
2595 /***********************************************************/
2598 typedef struct IOHandlerRecord
{
2600 IOCanRWHandler
*fd_read_poll
;
2602 IOHandler
*fd_write
;
2605 /* temporary data */
2607 struct IOHandlerRecord
*next
;
2610 static IOHandlerRecord
*first_io_handler
;
2612 /* XXX: fd_read_poll should be suppressed, but an API change is
2613 necessary in the character devices to suppress fd_can_read(). */
2614 int qemu_set_fd_handler2(int fd
,
2615 IOCanRWHandler
*fd_read_poll
,
2617 IOHandler
*fd_write
,
2620 IOHandlerRecord
**pioh
, *ioh
;
2622 if (!fd_read
&& !fd_write
) {
2623 pioh
= &first_io_handler
;
2628 if (ioh
->fd
== fd
) {
2635 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2639 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2640 ioh
->next
= first_io_handler
;
2641 first_io_handler
= ioh
;
2644 ioh
->fd_read_poll
= fd_read_poll
;
2645 ioh
->fd_read
= fd_read
;
2646 ioh
->fd_write
= fd_write
;
2647 ioh
->opaque
= opaque
;
2653 int qemu_set_fd_handler(int fd
,
2655 IOHandler
*fd_write
,
2658 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2662 /***********************************************************/
2663 /* Polling handling */
2665 typedef struct PollingEntry
{
2668 struct PollingEntry
*next
;
2671 static PollingEntry
*first_polling_entry
;
2673 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2675 PollingEntry
**ppe
, *pe
;
2676 pe
= qemu_mallocz(sizeof(PollingEntry
));
2678 pe
->opaque
= opaque
;
2679 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2684 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2686 PollingEntry
**ppe
, *pe
;
2687 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2689 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2697 /***********************************************************/
2698 /* Wait objects support */
2699 typedef struct WaitObjects
{
2701 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2702 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2703 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2706 static WaitObjects wait_objects
= {0};
2708 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2710 WaitObjects
*w
= &wait_objects
;
2712 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2714 w
->events
[w
->num
] = handle
;
2715 w
->func
[w
->num
] = func
;
2716 w
->opaque
[w
->num
] = opaque
;
2721 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2724 WaitObjects
*w
= &wait_objects
;
2727 for (i
= 0; i
< w
->num
; i
++) {
2728 if (w
->events
[i
] == handle
)
2731 w
->events
[i
] = w
->events
[i
+ 1];
2732 w
->func
[i
] = w
->func
[i
+ 1];
2733 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2741 /***********************************************************/
2742 /* ram save/restore */
2744 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2745 #define RAM_SAVE_FLAG_COMPRESS 0x02
2746 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2747 #define RAM_SAVE_FLAG_PAGE 0x08
2748 #define RAM_SAVE_FLAG_EOS 0x10
2750 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2752 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2753 uint32_t *array
= (uint32_t *)page
;
2756 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2757 if (array
[i
] != val
)
2764 static int ram_save_block(QEMUFile
*f
)
2766 static ram_addr_t current_addr
= 0;
2767 ram_addr_t saved_addr
= current_addr
;
2768 ram_addr_t addr
= 0;
2771 while (addr
< last_ram_offset
) {
2772 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2775 cpu_physical_memory_reset_dirty(current_addr
,
2776 current_addr
+ TARGET_PAGE_SIZE
,
2777 MIGRATION_DIRTY_FLAG
);
2779 p
= qemu_get_ram_ptr(current_addr
);
2781 if (is_dup_page(p
, *p
)) {
2782 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2783 qemu_put_byte(f
, *p
);
2785 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2786 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2792 addr
+= TARGET_PAGE_SIZE
;
2793 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2799 static uint64_t bytes_transferred
;
2801 static ram_addr_t
ram_save_remaining(void)
2804 ram_addr_t count
= 0;
2806 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2807 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2814 uint64_t ram_bytes_remaining(void)
2816 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2819 uint64_t ram_bytes_transferred(void)
2821 return bytes_transferred
;
2824 uint64_t ram_bytes_total(void)
2826 return last_ram_offset
;
2829 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2832 uint64_t bytes_transferred_last
;
2834 uint64_t expected_time
= 0;
2837 cpu_physical_memory_set_dirty_tracking(0);
2841 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2842 qemu_file_set_error(f
);
2847 bytes_transferred
= 0;
2849 /* Make sure all dirty bits are set */
2850 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2851 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2852 cpu_physical_memory_set_dirty(addr
);
2855 /* Enable dirty memory tracking */
2856 cpu_physical_memory_set_dirty_tracking(1);
2858 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2861 bytes_transferred_last
= bytes_transferred
;
2862 bwidth
= get_clock();
2864 while (!qemu_file_rate_limit(f
)) {
2867 ret
= ram_save_block(f
);
2868 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2869 if (ret
== 0) /* no more blocks */
2873 bwidth
= get_clock() - bwidth
;
2874 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2876 /* if we haven't transferred anything this round, force expected_time to a
2877 * a very high value, but without crashing */
2881 /* try transferring iterative blocks of memory */
2883 /* flush all remaining blocks regardless of rate limiting */
2884 while (ram_save_block(f
) != 0) {
2885 bytes_transferred
+= TARGET_PAGE_SIZE
;
2887 cpu_physical_memory_set_dirty_tracking(0);
2890 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2892 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2894 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2897 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2902 if (version_id
!= 3)
2906 addr
= qemu_get_be64(f
);
2908 flags
= addr
& ~TARGET_PAGE_MASK
;
2909 addr
&= TARGET_PAGE_MASK
;
2911 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2912 if (addr
!= last_ram_offset
)
2916 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2917 uint8_t ch
= qemu_get_byte(f
);
2918 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2921 (!kvm_enabled() || kvm_has_sync_mmu())) {
2922 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2925 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2926 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2928 if (qemu_file_has_error(f
)) {
2931 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2936 void qemu_service_io(void)
2938 qemu_notify_event();
2941 /***********************************************************/
2942 /* machine registration */
2944 static QEMUMachine
*first_machine
= NULL
;
2945 QEMUMachine
*current_machine
= NULL
;
2947 int qemu_register_machine(QEMUMachine
*m
)
2950 pm
= &first_machine
;
2958 static QEMUMachine
*find_machine(const char *name
)
2962 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2963 if (!strcmp(m
->name
, name
))
2965 if (m
->alias
&& !strcmp(m
->alias
, name
))
2971 static QEMUMachine
*find_default_machine(void)
2975 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2976 if (m
->is_default
) {
2983 /***********************************************************/
2984 /* main execution loop */
2986 static void gui_update(void *opaque
)
2988 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2989 DisplayState
*ds
= opaque
;
2990 DisplayChangeListener
*dcl
= ds
->listeners
;
2994 while (dcl
!= NULL
) {
2995 if (dcl
->gui_timer_interval
&&
2996 dcl
->gui_timer_interval
< interval
)
2997 interval
= dcl
->gui_timer_interval
;
3000 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3003 static void nographic_update(void *opaque
)
3005 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3007 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3010 struct vm_change_state_entry
{
3011 VMChangeStateHandler
*cb
;
3013 QLIST_ENTRY (vm_change_state_entry
) entries
;
3016 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3018 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3021 VMChangeStateEntry
*e
;
3023 e
= qemu_mallocz(sizeof (*e
));
3027 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3031 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3033 QLIST_REMOVE (e
, entries
);
3037 static void vm_state_notify(int running
, int reason
)
3039 VMChangeStateEntry
*e
;
3041 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3042 e
->cb(e
->opaque
, running
, reason
);
3046 static void resume_all_vcpus(void);
3047 static void pause_all_vcpus(void);
3054 vm_state_notify(1, 0);
3055 qemu_rearm_alarm_timer(alarm_timer
);
3060 /* reset/shutdown handler */
3062 typedef struct QEMUResetEntry
{
3063 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3064 QEMUResetHandler
*func
;
3068 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3069 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3070 static int reset_requested
;
3071 static int shutdown_requested
;
3072 static int powerdown_requested
;
3073 static int debug_requested
;
3074 static int vmstop_requested
;
3076 int qemu_shutdown_requested(void)
3078 int r
= shutdown_requested
;
3079 shutdown_requested
= 0;
3083 int qemu_reset_requested(void)
3085 int r
= reset_requested
;
3086 reset_requested
= 0;
3090 int qemu_powerdown_requested(void)
3092 int r
= powerdown_requested
;
3093 powerdown_requested
= 0;
3097 static int qemu_debug_requested(void)
3099 int r
= debug_requested
;
3100 debug_requested
= 0;
3104 static int qemu_vmstop_requested(void)
3106 int r
= vmstop_requested
;
3107 vmstop_requested
= 0;
3111 static void do_vm_stop(int reason
)
3114 cpu_disable_ticks();
3117 vm_state_notify(0, reason
);
3121 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3123 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3126 re
->opaque
= opaque
;
3127 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3130 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3134 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3135 if (re
->func
== func
&& re
->opaque
== opaque
) {
3136 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3143 void qemu_system_reset(void)
3145 QEMUResetEntry
*re
, *nre
;
3147 /* reset all devices */
3148 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3149 re
->func(re
->opaque
);
3153 void qemu_system_reset_request(void)
3156 shutdown_requested
= 1;
3158 reset_requested
= 1;
3160 qemu_notify_event();
3163 void qemu_system_shutdown_request(void)
3165 shutdown_requested
= 1;
3166 qemu_notify_event();
3169 void qemu_system_powerdown_request(void)
3171 powerdown_requested
= 1;
3172 qemu_notify_event();
3175 #ifdef CONFIG_IOTHREAD
3176 static void qemu_system_vmstop_request(int reason
)
3178 vmstop_requested
= reason
;
3179 qemu_notify_event();
3184 static int io_thread_fd
= -1;
3186 static void qemu_event_increment(void)
3188 static const char byte
= 0;
3191 if (io_thread_fd
== -1)
3194 ret
= write(io_thread_fd
, &byte
, sizeof(byte
));
3195 if (ret
< 0 && (errno
!= EINTR
&& errno
!= EAGAIN
)) {
3196 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3202 static void qemu_event_read(void *opaque
)
3204 int fd
= (unsigned long)opaque
;
3207 /* Drain the notify pipe */
3210 len
= read(fd
, buffer
, sizeof(buffer
));
3211 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3214 static int qemu_event_init(void)
3219 err
= qemu_pipe(fds
);
3223 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3227 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3231 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3232 (void *)(unsigned long)fds
[0]);
3234 io_thread_fd
= fds
[1];
3243 HANDLE qemu_event_handle
;
3245 static void dummy_event_handler(void *opaque
)
3249 static int qemu_event_init(void)
3251 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3252 if (!qemu_event_handle
) {
3253 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3256 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3260 static void qemu_event_increment(void)
3262 if (!SetEvent(qemu_event_handle
)) {
3263 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3270 static int cpu_can_run(CPUState
*env
)
3279 #ifndef CONFIG_IOTHREAD
3280 static int qemu_init_main_loop(void)
3282 return qemu_event_init();
3285 void qemu_init_vcpu(void *_env
)
3287 CPUState
*env
= _env
;
3289 env
->nr_cores
= smp_cores
;
3290 env
->nr_threads
= smp_threads
;
3296 int qemu_cpu_self(void *env
)
3301 static void resume_all_vcpus(void)
3305 static void pause_all_vcpus(void)
3309 void qemu_cpu_kick(void *env
)
3314 void qemu_notify_event(void)
3316 CPUState
*env
= cpu_single_env
;
3323 void qemu_mutex_lock_iothread(void) {}
3324 void qemu_mutex_unlock_iothread(void) {}
3326 void vm_stop(int reason
)
3331 #else /* CONFIG_IOTHREAD */
3333 #include "qemu-thread.h"
3335 QemuMutex qemu_global_mutex
;
3336 static QemuMutex qemu_fair_mutex
;
3338 static QemuThread io_thread
;
3340 static QemuThread
*tcg_cpu_thread
;
3341 static QemuCond
*tcg_halt_cond
;
3343 static int qemu_system_ready
;
3345 static QemuCond qemu_cpu_cond
;
3347 static QemuCond qemu_system_cond
;
3348 static QemuCond qemu_pause_cond
;
3350 static void block_io_signals(void);
3351 static void unblock_io_signals(void);
3352 static int tcg_has_work(void);
3354 static int qemu_init_main_loop(void)
3358 ret
= qemu_event_init();
3362 qemu_cond_init(&qemu_pause_cond
);
3363 qemu_mutex_init(&qemu_fair_mutex
);
3364 qemu_mutex_init(&qemu_global_mutex
);
3365 qemu_mutex_lock(&qemu_global_mutex
);
3367 unblock_io_signals();
3368 qemu_thread_self(&io_thread
);
3373 static void qemu_wait_io_event(CPUState
*env
)
3375 while (!tcg_has_work())
3376 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3378 qemu_mutex_unlock(&qemu_global_mutex
);
3381 * Users of qemu_global_mutex can be starved, having no chance
3382 * to acquire it since this path will get to it first.
3383 * So use another lock to provide fairness.
3385 qemu_mutex_lock(&qemu_fair_mutex
);
3386 qemu_mutex_unlock(&qemu_fair_mutex
);
3388 qemu_mutex_lock(&qemu_global_mutex
);
3392 qemu_cond_signal(&qemu_pause_cond
);
3396 static int qemu_cpu_exec(CPUState
*env
);
3398 static void *kvm_cpu_thread_fn(void *arg
)
3400 CPUState
*env
= arg
;
3403 qemu_thread_self(env
->thread
);
3407 /* signal CPU creation */
3408 qemu_mutex_lock(&qemu_global_mutex
);
3410 qemu_cond_signal(&qemu_cpu_cond
);
3412 /* and wait for machine initialization */
3413 while (!qemu_system_ready
)
3414 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3417 if (cpu_can_run(env
))
3419 qemu_wait_io_event(env
);
3425 static void tcg_cpu_exec(void);
3427 static void *tcg_cpu_thread_fn(void *arg
)
3429 CPUState
*env
= arg
;
3432 qemu_thread_self(env
->thread
);
3434 /* signal CPU creation */
3435 qemu_mutex_lock(&qemu_global_mutex
);
3436 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3438 qemu_cond_signal(&qemu_cpu_cond
);
3440 /* and wait for machine initialization */
3441 while (!qemu_system_ready
)
3442 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3446 qemu_wait_io_event(cur_cpu
);
3452 void qemu_cpu_kick(void *_env
)
3454 CPUState
*env
= _env
;
3455 qemu_cond_broadcast(env
->halt_cond
);
3457 qemu_thread_signal(env
->thread
, SIGUSR1
);
3460 int qemu_cpu_self(void *_env
)
3462 CPUState
*env
= _env
;
3465 qemu_thread_self(&this);
3467 return qemu_thread_equal(&this, env
->thread
);
3470 static void cpu_signal(int sig
)
3473 cpu_exit(cpu_single_env
);
3476 static void block_io_signals(void)
3479 struct sigaction sigact
;
3482 sigaddset(&set
, SIGUSR2
);
3483 sigaddset(&set
, SIGIO
);
3484 sigaddset(&set
, SIGALRM
);
3485 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3488 sigaddset(&set
, SIGUSR1
);
3489 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3491 memset(&sigact
, 0, sizeof(sigact
));
3492 sigact
.sa_handler
= cpu_signal
;
3493 sigaction(SIGUSR1
, &sigact
, NULL
);
3496 static void unblock_io_signals(void)
3501 sigaddset(&set
, SIGUSR2
);
3502 sigaddset(&set
, SIGIO
);
3503 sigaddset(&set
, SIGALRM
);
3504 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3507 sigaddset(&set
, SIGUSR1
);
3508 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3511 static void qemu_signal_lock(unsigned int msecs
)
3513 qemu_mutex_lock(&qemu_fair_mutex
);
3515 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3516 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3517 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3520 qemu_mutex_unlock(&qemu_fair_mutex
);
3523 void qemu_mutex_lock_iothread(void)
3525 if (kvm_enabled()) {
3526 qemu_mutex_lock(&qemu_fair_mutex
);
3527 qemu_mutex_lock(&qemu_global_mutex
);
3528 qemu_mutex_unlock(&qemu_fair_mutex
);
3530 qemu_signal_lock(100);
3533 void qemu_mutex_unlock_iothread(void)
3535 qemu_mutex_unlock(&qemu_global_mutex
);
3538 static int all_vcpus_paused(void)
3540 CPUState
*penv
= first_cpu
;
3545 penv
= (CPUState
*)penv
->next_cpu
;
3551 static void pause_all_vcpus(void)
3553 CPUState
*penv
= first_cpu
;
3557 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3558 qemu_cpu_kick(penv
);
3559 penv
= (CPUState
*)penv
->next_cpu
;
3562 while (!all_vcpus_paused()) {
3563 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3566 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3567 penv
= (CPUState
*)penv
->next_cpu
;
3572 static void resume_all_vcpus(void)
3574 CPUState
*penv
= first_cpu
;
3579 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3580 qemu_cpu_kick(penv
);
3581 penv
= (CPUState
*)penv
->next_cpu
;
3585 static void tcg_init_vcpu(void *_env
)
3587 CPUState
*env
= _env
;
3588 /* share a single thread for all cpus with TCG */
3589 if (!tcg_cpu_thread
) {
3590 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3591 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3592 qemu_cond_init(env
->halt_cond
);
3593 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3594 while (env
->created
== 0)
3595 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3596 tcg_cpu_thread
= env
->thread
;
3597 tcg_halt_cond
= env
->halt_cond
;
3599 env
->thread
= tcg_cpu_thread
;
3600 env
->halt_cond
= tcg_halt_cond
;
3604 static void kvm_start_vcpu(CPUState
*env
)
3606 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3607 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3608 qemu_cond_init(env
->halt_cond
);
3609 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3610 while (env
->created
== 0)
3611 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3614 void qemu_init_vcpu(void *_env
)
3616 CPUState
*env
= _env
;
3618 env
->nr_cores
= smp_cores
;
3619 env
->nr_threads
= smp_threads
;
3621 kvm_start_vcpu(env
);
3626 void qemu_notify_event(void)
3628 qemu_event_increment();
3631 void vm_stop(int reason
)
3634 qemu_thread_self(&me
);
3636 if (!qemu_thread_equal(&me
, &io_thread
)) {
3637 qemu_system_vmstop_request(reason
);
3639 * FIXME: should not return to device code in case
3640 * vm_stop() has been requested.
3642 if (cpu_single_env
) {
3643 cpu_exit(cpu_single_env
);
3644 cpu_single_env
->stop
= 1;
3655 static void host_main_loop_wait(int *timeout
)
3661 /* XXX: need to suppress polling by better using win32 events */
3663 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3664 ret
|= pe
->func(pe
->opaque
);
3668 WaitObjects
*w
= &wait_objects
;
3670 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3671 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3672 if (w
->func
[ret
- WAIT_OBJECT_0
])
3673 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3675 /* Check for additional signaled events */
3676 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3678 /* Check if event is signaled */
3679 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3680 if(ret2
== WAIT_OBJECT_0
) {
3682 w
->func
[i
](w
->opaque
[i
]);
3683 } else if (ret2
== WAIT_TIMEOUT
) {
3685 err
= GetLastError();
3686 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3689 } else if (ret
== WAIT_TIMEOUT
) {
3691 err
= GetLastError();
3692 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3699 static void host_main_loop_wait(int *timeout
)
3704 void main_loop_wait(int timeout
)
3706 IOHandlerRecord
*ioh
;
3707 fd_set rfds
, wfds
, xfds
;
3711 qemu_bh_update_timeout(&timeout
);
3713 host_main_loop_wait(&timeout
);
3715 /* poll any events */
3716 /* XXX: separate device handlers from system ones */
3721 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3725 (!ioh
->fd_read_poll
||
3726 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3727 FD_SET(ioh
->fd
, &rfds
);
3731 if (ioh
->fd_write
) {
3732 FD_SET(ioh
->fd
, &wfds
);
3738 tv
.tv_sec
= timeout
/ 1000;
3739 tv
.tv_usec
= (timeout
% 1000) * 1000;
3741 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3743 qemu_mutex_unlock_iothread();
3744 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3745 qemu_mutex_lock_iothread();
3747 IOHandlerRecord
**pioh
;
3749 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3750 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3751 ioh
->fd_read(ioh
->opaque
);
3753 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3754 ioh
->fd_write(ioh
->opaque
);
3758 /* remove deleted IO handlers */
3759 pioh
= &first_io_handler
;
3770 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3772 /* rearm timer, if not periodic */
3773 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3774 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3775 qemu_rearm_alarm_timer(alarm_timer
);
3778 /* vm time timers */
3780 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3781 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3782 qemu_get_clock(vm_clock
));
3785 /* real time timers */
3786 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3787 qemu_get_clock(rt_clock
));
3789 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3790 qemu_get_clock(host_clock
));
3792 /* Check bottom-halves last in case any of the earlier events triggered
3798 static int qemu_cpu_exec(CPUState
*env
)
3801 #ifdef CONFIG_PROFILER
3805 #ifdef CONFIG_PROFILER
3806 ti
= profile_getclock();
3811 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3812 env
->icount_decr
.u16
.low
= 0;
3813 env
->icount_extra
= 0;
3814 count
= qemu_next_deadline();
3815 count
= (count
+ (1 << icount_time_shift
) - 1)
3816 >> icount_time_shift
;
3817 qemu_icount
+= count
;
3818 decr
= (count
> 0xffff) ? 0xffff : count
;
3820 env
->icount_decr
.u16
.low
= decr
;
3821 env
->icount_extra
= count
;
3823 ret
= cpu_exec(env
);
3824 #ifdef CONFIG_PROFILER
3825 qemu_time
+= profile_getclock() - ti
;
3828 /* Fold pending instructions back into the
3829 instruction counter, and clear the interrupt flag. */
3830 qemu_icount
-= (env
->icount_decr
.u16
.low
3831 + env
->icount_extra
);
3832 env
->icount_decr
.u32
= 0;
3833 env
->icount_extra
= 0;
3838 static void tcg_cpu_exec(void)
3842 if (next_cpu
== NULL
)
3843 next_cpu
= first_cpu
;
3844 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3845 CPUState
*env
= cur_cpu
= next_cpu
;
3849 if (timer_alarm_pending
) {
3850 timer_alarm_pending
= 0;
3853 if (cpu_can_run(env
))
3854 ret
= qemu_cpu_exec(env
);
3855 if (ret
== EXCP_DEBUG
) {
3856 gdb_set_stop_cpu(env
);
3857 debug_requested
= 1;
3863 static int cpu_has_work(CPUState
*env
)
3871 if (qemu_cpu_has_work(env
))
3876 static int tcg_has_work(void)
3880 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3881 if (cpu_has_work(env
))
3886 static int qemu_calculate_timeout(void)
3888 #ifndef CONFIG_IOTHREAD
3893 else if (tcg_has_work())
3895 else if (!use_icount
)
3898 /* XXX: use timeout computed from timers */
3901 /* Advance virtual time to the next event. */
3902 if (use_icount
== 1) {
3903 /* When not using an adaptive execution frequency
3904 we tend to get badly out of sync with real time,
3905 so just delay for a reasonable amount of time. */
3908 delta
= cpu_get_icount() - cpu_get_clock();
3911 /* If virtual time is ahead of real time then just
3913 timeout
= (delta
/ 1000000) + 1;
3915 /* Wait for either IO to occur or the next
3917 add
= qemu_next_deadline();
3918 /* We advance the timer before checking for IO.
3919 Limit the amount we advance so that early IO
3920 activity won't get the guest too far ahead. */
3924 add
= (add
+ (1 << icount_time_shift
) - 1)
3925 >> icount_time_shift
;
3927 timeout
= delta
/ 1000000;
3934 #else /* CONFIG_IOTHREAD */
3939 static int vm_can_run(void)
3941 if (powerdown_requested
)
3943 if (reset_requested
)
3945 if (shutdown_requested
)
3947 if (debug_requested
)
3952 qemu_irq qemu_system_powerdown
;
3954 static void main_loop(void)
3958 #ifdef CONFIG_IOTHREAD
3959 qemu_system_ready
= 1;
3960 qemu_cond_broadcast(&qemu_system_cond
);
3965 #ifdef CONFIG_PROFILER
3968 #ifndef CONFIG_IOTHREAD
3971 #ifdef CONFIG_PROFILER
3972 ti
= profile_getclock();
3974 main_loop_wait(qemu_calculate_timeout());
3975 #ifdef CONFIG_PROFILER
3976 dev_time
+= profile_getclock() - ti
;
3978 } while (vm_can_run());
3980 if (qemu_debug_requested()) {
3981 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
3982 vm_stop(EXCP_DEBUG
);
3984 if (qemu_shutdown_requested()) {
3985 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3992 if (qemu_reset_requested()) {
3993 monitor_protocol_event(QEVENT_RESET
, NULL
);
3995 qemu_system_reset();
3998 if (qemu_powerdown_requested()) {
3999 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4000 qemu_irq_raise(qemu_system_powerdown
);
4002 if ((r
= qemu_vmstop_requested())) {
4003 monitor_protocol_event(QEVENT_STOP
, NULL
);
4010 static void version(void)
4012 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4015 static void help(int exitcode
)
4018 printf("usage: %s [options] [disk_image]\n"
4020 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4022 #define DEF(option, opt_arg, opt_enum, opt_help) \
4024 #define DEFHEADING(text) stringify(text) "\n"
4025 #include "qemu-options.h"
4030 "During emulation, the following keys are useful:\n"
4031 "ctrl-alt-f toggle full screen\n"
4032 "ctrl-alt-n switch to virtual console 'n'\n"
4033 "ctrl-alt toggle mouse and keyboard grab\n"
4035 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4040 DEFAULT_NETWORK_SCRIPT
,
4041 DEFAULT_NETWORK_DOWN_SCRIPT
,
4043 DEFAULT_GDBSTUB_PORT
,
4048 #define HAS_ARG 0x0001
4051 #define DEF(option, opt_arg, opt_enum, opt_help) \
4053 #define DEFHEADING(text)
4054 #include "qemu-options.h"
4060 typedef struct QEMUOption
{
4066 static const QEMUOption qemu_options
[] = {
4067 { "h", 0, QEMU_OPTION_h
},
4068 #define DEF(option, opt_arg, opt_enum, opt_help) \
4069 { option, opt_arg, opt_enum },
4070 #define DEFHEADING(text)
4071 #include "qemu-options.h"
4079 struct soundhw soundhw
[] = {
4080 #ifdef HAS_AUDIO_CHOICE
4081 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4087 { .init_isa
= pcspk_audio_init
}
4094 "Creative Sound Blaster 16",
4097 { .init_isa
= SB16_init
}
4101 #ifdef CONFIG_CS4231A
4107 { .init_isa
= cs4231a_init
}
4115 "Yamaha YMF262 (OPL3)",
4117 "Yamaha YM3812 (OPL2)",
4121 { .init_isa
= Adlib_init
}
4128 "Gravis Ultrasound GF1",
4131 { .init_isa
= GUS_init
}
4138 "Intel 82801AA AC97 Audio",
4141 { .init_pci
= ac97_init
}
4145 #ifdef CONFIG_ES1370
4148 "ENSONIQ AudioPCI ES1370",
4151 { .init_pci
= es1370_init
}
4155 #endif /* HAS_AUDIO_CHOICE */
4157 { NULL
, NULL
, 0, 0, { NULL
} }
4160 static void select_soundhw (const char *optarg
)
4164 if (*optarg
== '?') {
4167 printf ("Valid sound card names (comma separated):\n");
4168 for (c
= soundhw
; c
->name
; ++c
) {
4169 printf ("%-11s %s\n", c
->name
, c
->descr
);
4171 printf ("\n-soundhw all will enable all of the above\n");
4172 exit (*optarg
!= '?');
4180 if (!strcmp (optarg
, "all")) {
4181 for (c
= soundhw
; c
->name
; ++c
) {
4189 e
= strchr (p
, ',');
4190 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4192 for (c
= soundhw
; c
->name
; ++c
) {
4193 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4202 "Unknown sound card name (too big to show)\n");
4205 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4210 p
+= l
+ (e
!= NULL
);
4214 goto show_valid_cards
;
4219 static void select_vgahw (const char *p
)
4224 vga_interface_type
= VGA_NONE
;
4225 if (strstart(p
, "std", &opts
)) {
4226 vga_interface_type
= VGA_STD
;
4227 } else if (strstart(p
, "cirrus", &opts
)) {
4228 vga_interface_type
= VGA_CIRRUS
;
4229 } else if (strstart(p
, "vmware", &opts
)) {
4230 vga_interface_type
= VGA_VMWARE
;
4231 } else if (strstart(p
, "xenfb", &opts
)) {
4232 vga_interface_type
= VGA_XENFB
;
4233 } else if (!strstart(p
, "none", &opts
)) {
4235 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4239 const char *nextopt
;
4241 if (strstart(opts
, ",retrace=", &nextopt
)) {
4243 if (strstart(opts
, "dumb", &nextopt
))
4244 vga_retrace_method
= VGA_RETRACE_DUMB
;
4245 else if (strstart(opts
, "precise", &nextopt
))
4246 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4247 else goto invalid_vga
;
4248 } else goto invalid_vga
;
4254 static int balloon_parse(const char *arg
)
4258 if (strcmp(arg
, "none") == 0) {
4262 if (!strncmp(arg
, "virtio", 6)) {
4263 if (arg
[6] == ',') {
4264 /* have params -> parse them */
4265 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4269 /* create empty opts */
4270 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4272 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4281 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4283 exit(STATUS_CONTROL_C_EXIT
);
4288 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4292 if(strlen(str
) != 36)
4295 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4296 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4297 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4303 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4311 static void termsig_handler(int signal
)
4313 qemu_system_shutdown_request();
4316 static void sigchld_handler(int signal
)
4318 waitpid(-1, NULL
, WNOHANG
);
4321 static void sighandler_setup(void)
4323 struct sigaction act
;
4325 memset(&act
, 0, sizeof(act
));
4326 act
.sa_handler
= termsig_handler
;
4327 sigaction(SIGINT
, &act
, NULL
);
4328 sigaction(SIGHUP
, &act
, NULL
);
4329 sigaction(SIGTERM
, &act
, NULL
);
4331 act
.sa_handler
= sigchld_handler
;
4332 act
.sa_flags
= SA_NOCLDSTOP
;
4333 sigaction(SIGCHLD
, &act
, NULL
);
4339 /* Look for support files in the same directory as the executable. */
4340 static char *find_datadir(const char *argv0
)
4346 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4353 while (p
!= buf
&& *p
!= '\\')
4356 if (access(buf
, R_OK
) == 0) {
4357 return qemu_strdup(buf
);
4363 /* Find a likely location for support files using the location of the binary.
4364 For installed binaries this will be "$bindir/../share/qemu". When
4365 running from the build tree this will be "$bindir/../pc-bios". */
4366 #define SHARE_SUFFIX "/share/qemu"
4367 #define BUILD_SUFFIX "/pc-bios"
4368 static char *find_datadir(const char *argv0
)
4376 #if defined(__linux__)
4379 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4385 #elif defined(__FreeBSD__)
4388 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4395 /* If we don't have any way of figuring out the actual executable
4396 location then try argv[0]. */
4398 p
= realpath(argv0
, buf
);
4406 max_len
= strlen(dir
) +
4407 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4408 res
= qemu_mallocz(max_len
);
4409 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4410 if (access(res
, R_OK
)) {
4411 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4412 if (access(res
, R_OK
)) {
4424 char *qemu_find_file(int type
, const char *name
)
4430 /* If name contains path separators then try it as a straight path. */
4431 if ((strchr(name
, '/') || strchr(name
, '\\'))
4432 && access(name
, R_OK
) == 0) {
4433 return qemu_strdup(name
);
4436 case QEMU_FILE_TYPE_BIOS
:
4439 case QEMU_FILE_TYPE_KEYMAP
:
4440 subdir
= "keymaps/";
4445 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4446 buf
= qemu_mallocz(len
);
4447 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4448 if (access(buf
, R_OK
)) {
4455 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4459 dev
= qdev_device_add(opts
);
4465 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4467 CharDriverState
*chr
;
4469 chr
= qemu_chr_open_opts(opts
, NULL
);
4475 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4477 CharDriverState
*chr
;
4478 const char *chardev
;
4482 mode
= qemu_opt_get(opts
, "mode");
4486 if (strcmp(mode
, "readline") == 0) {
4487 flags
= MONITOR_USE_READLINE
;
4488 } else if (strcmp(mode
, "control") == 0) {
4489 flags
= MONITOR_USE_CONTROL
;
4491 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4495 if (qemu_opt_get_bool(opts
, "default", 0))
4496 flags
|= MONITOR_IS_DEFAULT
;
4498 chardev
= qemu_opt_get(opts
, "chardev");
4499 chr
= qemu_chr_find(chardev
);
4501 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4505 monitor_init(chr
, flags
);
4509 static void monitor_parse(const char *optarg
, const char *mode
)
4511 static int monitor_device_index
= 0;
4517 if (strstart(optarg
, "chardev:", &p
)) {
4518 snprintf(label
, sizeof(label
), "%s", p
);
4520 if (monitor_device_index
) {
4521 snprintf(label
, sizeof(label
), "monitor%d",
4522 monitor_device_index
);
4524 snprintf(label
, sizeof(label
), "monitor");
4527 opts
= qemu_chr_parse_compat(label
, optarg
);
4529 fprintf(stderr
, "parse error: %s\n", optarg
);
4534 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4536 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4539 qemu_opt_set(opts
, "mode", mode
);
4540 qemu_opt_set(opts
, "chardev", label
);
4542 qemu_opt_set(opts
, "default", "on");
4543 monitor_device_index
++;
4546 struct device_config
{
4548 DEV_USB
, /* -usbdevice */
4550 DEV_SERIAL
, /* -serial */
4551 DEV_PARALLEL
, /* -parallel */
4552 DEV_VIRTCON
, /* -virtioconsole */
4553 DEV_DEBUGCON
, /* -debugcon */
4555 const char *cmdline
;
4556 QTAILQ_ENTRY(device_config
) next
;
4558 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4560 static void add_device_config(int type
, const char *cmdline
)
4562 struct device_config
*conf
;
4564 conf
= qemu_mallocz(sizeof(*conf
));
4566 conf
->cmdline
= cmdline
;
4567 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4570 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4572 struct device_config
*conf
;
4575 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4576 if (conf
->type
!= type
)
4578 rc
= func(conf
->cmdline
);
4585 static int serial_parse(const char *devname
)
4587 static int index
= 0;
4590 if (strcmp(devname
, "none") == 0)
4592 if (index
== MAX_SERIAL_PORTS
) {
4593 fprintf(stderr
, "qemu: too many serial ports\n");
4596 snprintf(label
, sizeof(label
), "serial%d", index
);
4597 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4598 if (!serial_hds
[index
]) {
4599 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4600 devname
, strerror(errno
));
4607 static int parallel_parse(const char *devname
)
4609 static int index
= 0;
4612 if (strcmp(devname
, "none") == 0)
4614 if (index
== MAX_PARALLEL_PORTS
) {
4615 fprintf(stderr
, "qemu: too many parallel ports\n");
4618 snprintf(label
, sizeof(label
), "parallel%d", index
);
4619 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4620 if (!parallel_hds
[index
]) {
4621 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4622 devname
, strerror(errno
));
4629 static int virtcon_parse(const char *devname
)
4631 static int index
= 0;
4633 QemuOpts
*bus_opts
, *dev_opts
;
4635 if (strcmp(devname
, "none") == 0)
4637 if (index
== MAX_VIRTIO_CONSOLES
) {
4638 fprintf(stderr
, "qemu: too many virtio consoles\n");
4642 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4643 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4645 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4646 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4648 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4649 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4650 if (!virtcon_hds
[index
]) {
4651 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4652 devname
, strerror(errno
));
4655 qemu_opt_set(dev_opts
, "chardev", label
);
4661 static int debugcon_parse(const char *devname
)
4665 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4668 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4670 fprintf(stderr
, "qemu: already have a debugcon device\n");
4673 qemu_opt_set(opts
, "driver", "isa-debugcon");
4674 qemu_opt_set(opts
, "chardev", "debugcon");
4678 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4679 const char **poptarg
, int *poptind
)
4681 const QEMUOption
*popt
;
4682 int optind
= *poptind
;
4683 char *r
= argv
[optind
];
4687 /* Treat --foo the same as -foo. */
4690 popt
= qemu_options
;
4693 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4697 if (!strcmp(popt
->name
, r
+ 1))
4701 if (popt
->flags
& HAS_ARG
) {
4702 if (optind
>= argc
) {
4703 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4707 optarg
= argv
[optind
++];
4718 int main(int argc
, char **argv
, char **envp
)
4720 const char *gdbstub_dev
= NULL
;
4721 uint32_t boot_devices_bitmap
= 0;
4723 int snapshot
, linux_boot
, net_boot
;
4724 const char *initrd_filename
;
4725 const char *kernel_filename
, *kernel_cmdline
;
4726 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4728 DisplayChangeListener
*dcl
;
4729 int cyls
, heads
, secs
, translation
;
4730 QemuOpts
*hda_opts
= NULL
, *opts
;
4733 const char *loadvm
= NULL
;
4734 QEMUMachine
*machine
;
4735 const char *cpu_model
;
4740 const char *pid_file
= NULL
;
4741 const char *incoming
= NULL
;
4744 struct passwd
*pwd
= NULL
;
4745 const char *chroot_dir
= NULL
;
4746 const char *run_as
= NULL
;
4749 int show_vnc_port
= 0;
4754 qemu_errors_to_file(stderr
);
4755 qemu_cache_utils_init(envp
);
4757 QLIST_INIT (&vm_change_state_head
);
4760 struct sigaction act
;
4761 sigfillset(&act
.sa_mask
);
4763 act
.sa_handler
= SIG_IGN
;
4764 sigaction(SIGPIPE
, &act
, NULL
);
4767 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4768 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4769 QEMU to run on a single CPU */
4774 h
= GetCurrentProcess();
4775 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4776 for(i
= 0; i
< 32; i
++) {
4777 if (mask
& (1 << i
))
4782 SetProcessAffinityMask(h
, mask
);
4788 module_call_init(MODULE_INIT_MACHINE
);
4789 machine
= find_default_machine();
4791 initrd_filename
= NULL
;
4794 kernel_filename
= NULL
;
4795 kernel_cmdline
= "";
4796 cyls
= heads
= secs
= 0;
4797 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4799 for (i
= 0; i
< MAX_NODES
; i
++) {
4801 node_cpumask
[i
] = 0;
4810 /* first pass of option parsing */
4812 while (optind
< argc
) {
4813 if (argv
[optind
][0] != '-') {
4817 const QEMUOption
*popt
;
4819 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4820 switch (popt
->index
) {
4821 case QEMU_OPTION_nodefconfig
:
4830 fp
= fopen(CONFIG_QEMU_CONFDIR
"/qemu.conf", "r");
4832 if (qemu_config_parse(fp
) != 0) {
4838 fp
= fopen(CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", "r");
4840 if (qemu_config_parse(fp
) != 0) {
4847 /* second pass of option parsing */
4852 if (argv
[optind
][0] != '-') {
4853 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4855 const QEMUOption
*popt
;
4857 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4858 switch(popt
->index
) {
4860 machine
= find_machine(optarg
);
4863 printf("Supported machines are:\n");
4864 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4866 printf("%-10s %s (alias of %s)\n",
4867 m
->alias
, m
->desc
, m
->name
);
4868 printf("%-10s %s%s\n",
4870 m
->is_default
? " (default)" : "");
4872 exit(*optarg
!= '?');
4875 case QEMU_OPTION_cpu
:
4876 /* hw initialization will check this */
4877 if (*optarg
== '?') {
4878 /* XXX: implement xxx_cpu_list for targets that still miss it */
4879 #if defined(cpu_list)
4880 cpu_list(stdout
, &fprintf
);
4887 case QEMU_OPTION_initrd
:
4888 initrd_filename
= optarg
;
4890 case QEMU_OPTION_hda
:
4892 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4894 hda_opts
= drive_add(optarg
, HD_ALIAS
4895 ",cyls=%d,heads=%d,secs=%d%s",
4896 0, cyls
, heads
, secs
,
4897 translation
== BIOS_ATA_TRANSLATION_LBA
?
4899 translation
== BIOS_ATA_TRANSLATION_NONE
?
4900 ",trans=none" : "");
4902 case QEMU_OPTION_hdb
:
4903 case QEMU_OPTION_hdc
:
4904 case QEMU_OPTION_hdd
:
4905 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4907 case QEMU_OPTION_drive
:
4908 drive_add(NULL
, "%s", optarg
);
4910 case QEMU_OPTION_set
:
4911 if (qemu_set_option(optarg
) != 0)
4914 case QEMU_OPTION_global
:
4915 if (qemu_global_option(optarg
) != 0)
4918 case QEMU_OPTION_mtdblock
:
4919 drive_add(optarg
, MTD_ALIAS
);
4921 case QEMU_OPTION_sd
:
4922 drive_add(optarg
, SD_ALIAS
);
4924 case QEMU_OPTION_pflash
:
4925 drive_add(optarg
, PFLASH_ALIAS
);
4927 case QEMU_OPTION_snapshot
:
4930 case QEMU_OPTION_hdachs
:
4934 cyls
= strtol(p
, (char **)&p
, 0);
4935 if (cyls
< 1 || cyls
> 16383)
4940 heads
= strtol(p
, (char **)&p
, 0);
4941 if (heads
< 1 || heads
> 16)
4946 secs
= strtol(p
, (char **)&p
, 0);
4947 if (secs
< 1 || secs
> 63)
4951 if (!strcmp(p
, "none"))
4952 translation
= BIOS_ATA_TRANSLATION_NONE
;
4953 else if (!strcmp(p
, "lba"))
4954 translation
= BIOS_ATA_TRANSLATION_LBA
;
4955 else if (!strcmp(p
, "auto"))
4956 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4959 } else if (*p
!= '\0') {
4961 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4964 if (hda_opts
!= NULL
) {
4966 snprintf(num
, sizeof(num
), "%d", cyls
);
4967 qemu_opt_set(hda_opts
, "cyls", num
);
4968 snprintf(num
, sizeof(num
), "%d", heads
);
4969 qemu_opt_set(hda_opts
, "heads", num
);
4970 snprintf(num
, sizeof(num
), "%d", secs
);
4971 qemu_opt_set(hda_opts
, "secs", num
);
4972 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4973 qemu_opt_set(hda_opts
, "trans", "lba");
4974 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4975 qemu_opt_set(hda_opts
, "trans", "none");
4979 case QEMU_OPTION_numa
:
4980 if (nb_numa_nodes
>= MAX_NODES
) {
4981 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4986 case QEMU_OPTION_nographic
:
4987 display_type
= DT_NOGRAPHIC
;
4989 #ifdef CONFIG_CURSES
4990 case QEMU_OPTION_curses
:
4991 display_type
= DT_CURSES
;
4994 case QEMU_OPTION_portrait
:
4997 case QEMU_OPTION_kernel
:
4998 kernel_filename
= optarg
;
5000 case QEMU_OPTION_append
:
5001 kernel_cmdline
= optarg
;
5003 case QEMU_OPTION_cdrom
:
5004 drive_add(optarg
, CDROM_ALIAS
);
5006 case QEMU_OPTION_boot
:
5008 static const char * const params
[] = {
5009 "order", "once", "menu", NULL
5011 char buf
[sizeof(boot_devices
)];
5012 char *standard_boot_devices
;
5015 if (!strchr(optarg
, '=')) {
5017 pstrcpy(buf
, sizeof(buf
), optarg
);
5018 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5020 "qemu: unknown boot parameter '%s' in '%s'\n",
5026 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5027 boot_devices_bitmap
= parse_bootdevices(buf
);
5028 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5031 if (get_param_value(buf
, sizeof(buf
),
5033 boot_devices_bitmap
|= parse_bootdevices(buf
);
5034 standard_boot_devices
= qemu_strdup(boot_devices
);
5035 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5036 qemu_register_reset(restore_boot_devices
,
5037 standard_boot_devices
);
5039 if (get_param_value(buf
, sizeof(buf
),
5041 if (!strcmp(buf
, "on")) {
5043 } else if (!strcmp(buf
, "off")) {
5047 "qemu: invalid option value '%s'\n",
5055 case QEMU_OPTION_fda
:
5056 case QEMU_OPTION_fdb
:
5057 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5060 case QEMU_OPTION_no_fd_bootchk
:
5064 case QEMU_OPTION_netdev
:
5065 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5069 case QEMU_OPTION_net
:
5070 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5075 case QEMU_OPTION_tftp
:
5076 legacy_tftp_prefix
= optarg
;
5078 case QEMU_OPTION_bootp
:
5079 legacy_bootp_filename
= optarg
;
5082 case QEMU_OPTION_smb
:
5083 if (net_slirp_smb(optarg
) < 0)
5087 case QEMU_OPTION_redir
:
5088 if (net_slirp_redir(optarg
) < 0)
5092 case QEMU_OPTION_bt
:
5093 add_device_config(DEV_BT
, optarg
);
5096 case QEMU_OPTION_audio_help
:
5100 case QEMU_OPTION_soundhw
:
5101 select_soundhw (optarg
);
5107 case QEMU_OPTION_version
:
5111 case QEMU_OPTION_m
: {
5115 value
= strtoul(optarg
, &ptr
, 10);
5117 case 0: case 'M': case 'm':
5124 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5128 /* On 32-bit hosts, QEMU is limited by virtual address space */
5129 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5130 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5133 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5134 fprintf(stderr
, "qemu: ram size too large\n");
5143 const CPULogItem
*item
;
5145 mask
= cpu_str_to_log_mask(optarg
);
5147 printf("Log items (comma separated):\n");
5148 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5149 printf("%-10s %s\n", item
->name
, item
->help
);
5157 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5159 case QEMU_OPTION_gdb
:
5160 gdbstub_dev
= optarg
;
5165 case QEMU_OPTION_bios
:
5168 case QEMU_OPTION_singlestep
:
5175 keyboard_layout
= optarg
;
5177 case QEMU_OPTION_localtime
:
5180 case QEMU_OPTION_vga
:
5181 select_vgahw (optarg
);
5183 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5189 w
= strtol(p
, (char **)&p
, 10);
5192 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5198 h
= strtol(p
, (char **)&p
, 10);
5203 depth
= strtol(p
, (char **)&p
, 10);
5204 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5205 depth
!= 24 && depth
!= 32)
5207 } else if (*p
== '\0') {
5208 depth
= graphic_depth
;
5215 graphic_depth
= depth
;
5219 case QEMU_OPTION_echr
:
5222 term_escape_char
= strtol(optarg
, &r
, 0);
5224 printf("Bad argument to echr\n");
5227 case QEMU_OPTION_monitor
:
5228 monitor_parse(optarg
, "readline");
5229 default_monitor
= 0;
5231 case QEMU_OPTION_qmp
:
5232 monitor_parse(optarg
, "control");
5233 default_monitor
= 0;
5235 case QEMU_OPTION_mon
:
5236 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5238 fprintf(stderr
, "parse error: %s\n", optarg
);
5241 default_monitor
= 0;
5243 case QEMU_OPTION_chardev
:
5244 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5246 fprintf(stderr
, "parse error: %s\n", optarg
);
5250 case QEMU_OPTION_serial
:
5251 add_device_config(DEV_SERIAL
, optarg
);
5254 case QEMU_OPTION_watchdog
:
5257 "qemu: only one watchdog option may be given\n");
5262 case QEMU_OPTION_watchdog_action
:
5263 if (select_watchdog_action(optarg
) == -1) {
5264 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5268 case QEMU_OPTION_virtiocon
:
5269 add_device_config(DEV_VIRTCON
, optarg
);
5270 default_virtcon
= 0;
5272 case QEMU_OPTION_parallel
:
5273 add_device_config(DEV_PARALLEL
, optarg
);
5274 default_parallel
= 0;
5276 case QEMU_OPTION_debugcon
:
5277 add_device_config(DEV_DEBUGCON
, optarg
);
5279 case QEMU_OPTION_loadvm
:
5282 case QEMU_OPTION_full_screen
:
5286 case QEMU_OPTION_no_frame
:
5289 case QEMU_OPTION_alt_grab
:
5292 case QEMU_OPTION_ctrl_grab
:
5295 case QEMU_OPTION_no_quit
:
5298 case QEMU_OPTION_sdl
:
5299 display_type
= DT_SDL
;
5302 case QEMU_OPTION_pidfile
:
5306 case QEMU_OPTION_win2k_hack
:
5307 win2k_install_hack
= 1;
5309 case QEMU_OPTION_rtc_td_hack
:
5312 case QEMU_OPTION_acpitable
:
5313 if(acpi_table_add(optarg
) < 0) {
5314 fprintf(stderr
, "Wrong acpi table provided\n");
5318 case QEMU_OPTION_smbios
:
5319 if(smbios_entry_add(optarg
) < 0) {
5320 fprintf(stderr
, "Wrong smbios provided\n");
5326 case QEMU_OPTION_enable_kvm
:
5330 case QEMU_OPTION_usb
:
5333 case QEMU_OPTION_usbdevice
:
5335 add_device_config(DEV_USB
, optarg
);
5337 case QEMU_OPTION_device
:
5338 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5342 case QEMU_OPTION_smp
:
5345 fprintf(stderr
, "Invalid number of CPUs\n");
5348 if (max_cpus
< smp_cpus
) {
5349 fprintf(stderr
, "maxcpus must be equal to or greater than "
5353 if (max_cpus
> 255) {
5354 fprintf(stderr
, "Unsupported number of maxcpus\n");
5358 case QEMU_OPTION_vnc
:
5359 display_type
= DT_VNC
;
5360 vnc_display
= optarg
;
5363 case QEMU_OPTION_no_acpi
:
5366 case QEMU_OPTION_no_hpet
:
5369 case QEMU_OPTION_balloon
:
5370 if (balloon_parse(optarg
) < 0) {
5371 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5376 case QEMU_OPTION_no_reboot
:
5379 case QEMU_OPTION_no_shutdown
:
5382 case QEMU_OPTION_show_cursor
:
5385 case QEMU_OPTION_uuid
:
5386 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5387 fprintf(stderr
, "Fail to parse UUID string."
5388 " Wrong format.\n");
5393 case QEMU_OPTION_daemonize
:
5397 case QEMU_OPTION_option_rom
:
5398 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5399 fprintf(stderr
, "Too many option ROMs\n");
5402 option_rom
[nb_option_roms
] = optarg
;
5405 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5406 case QEMU_OPTION_semihosting
:
5407 semihosting_enabled
= 1;
5410 case QEMU_OPTION_name
:
5411 qemu_name
= qemu_strdup(optarg
);
5413 char *p
= strchr(qemu_name
, ',');
5416 if (strncmp(p
, "process=", 8)) {
5417 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5425 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5426 case QEMU_OPTION_prom_env
:
5427 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5428 fprintf(stderr
, "Too many prom variables\n");
5431 prom_envs
[nb_prom_envs
] = optarg
;
5436 case QEMU_OPTION_old_param
:
5440 case QEMU_OPTION_clock
:
5441 configure_alarms(optarg
);
5443 case QEMU_OPTION_startdate
:
5444 configure_rtc_date_offset(optarg
, 1);
5446 case QEMU_OPTION_rtc
:
5447 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5449 fprintf(stderr
, "parse error: %s\n", optarg
);
5452 configure_rtc(opts
);
5454 case QEMU_OPTION_tb_size
:
5455 tb_size
= strtol(optarg
, NULL
, 0);
5459 case QEMU_OPTION_icount
:
5461 if (strcmp(optarg
, "auto") == 0) {
5462 icount_time_shift
= -1;
5464 icount_time_shift
= strtol(optarg
, NULL
, 0);
5467 case QEMU_OPTION_incoming
:
5470 case QEMU_OPTION_nodefaults
:
5472 default_parallel
= 0;
5473 default_virtcon
= 0;
5474 default_monitor
= 0;
5482 case QEMU_OPTION_chroot
:
5483 chroot_dir
= optarg
;
5485 case QEMU_OPTION_runas
:
5490 case QEMU_OPTION_xen_domid
:
5491 xen_domid
= atoi(optarg
);
5493 case QEMU_OPTION_xen_create
:
5494 xen_mode
= XEN_CREATE
;
5496 case QEMU_OPTION_xen_attach
:
5497 xen_mode
= XEN_ATTACH
;
5500 case QEMU_OPTION_readconfig
:
5503 fp
= fopen(optarg
, "r");
5505 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5508 if (qemu_config_parse(fp
) != 0) {
5514 case QEMU_OPTION_writeconfig
:
5517 if (strcmp(optarg
, "-") == 0) {
5520 fp
= fopen(optarg
, "w");
5522 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5526 qemu_config_write(fp
);
5534 /* If no data_dir is specified then try to find it relative to the
5537 data_dir
= find_datadir(argv
[0]);
5539 /* If all else fails use the install patch specified when building. */
5541 data_dir
= CONFIG_QEMU_SHAREDIR
;
5545 * Default to max_cpus = smp_cpus, in case the user doesn't
5546 * specify a max_cpus value.
5549 max_cpus
= smp_cpus
;
5551 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5552 if (smp_cpus
> machine
->max_cpus
) {
5553 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5554 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5559 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5560 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5562 if (machine
->no_serial
) {
5565 if (machine
->no_parallel
) {
5566 default_parallel
= 0;
5568 if (!machine
->use_virtcon
) {
5569 default_virtcon
= 0;
5571 if (machine
->no_vga
) {
5574 if (machine
->no_floppy
) {
5577 if (machine
->no_cdrom
) {
5580 if (machine
->no_sdcard
) {
5584 if (display_type
== DT_NOGRAPHIC
) {
5585 if (default_parallel
)
5586 add_device_config(DEV_PARALLEL
, "null");
5587 if (default_serial
&& default_monitor
) {
5588 add_device_config(DEV_SERIAL
, "mon:stdio");
5589 } else if (default_virtcon
&& default_monitor
) {
5590 add_device_config(DEV_VIRTCON
, "mon:stdio");
5593 add_device_config(DEV_SERIAL
, "stdio");
5594 if (default_virtcon
)
5595 add_device_config(DEV_VIRTCON
, "stdio");
5596 if (default_monitor
)
5597 monitor_parse("stdio", "readline");
5601 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5602 if (default_parallel
)
5603 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5604 if (default_monitor
)
5605 monitor_parse("vc:80Cx24C", "readline");
5606 if (default_virtcon
)
5607 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5610 vga_interface_type
= VGA_CIRRUS
;
5612 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5619 if (pipe(fds
) == -1)
5630 len
= read(fds
[0], &status
, 1);
5631 if (len
== -1 && (errno
== EINTR
))
5636 else if (status
== 1) {
5637 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5645 qemu_set_cloexec(fds
[1]);
5657 signal(SIGTSTP
, SIG_IGN
);
5658 signal(SIGTTOU
, SIG_IGN
);
5659 signal(SIGTTIN
, SIG_IGN
);
5663 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5667 if (write(fds
[1], &status
, 1) != 1) {
5668 perror("daemonize. Writing to pipe\n");
5672 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5676 if (kvm_enabled()) {
5679 ret
= kvm_init(smp_cpus
);
5681 fprintf(stderr
, "failed to initialize KVM\n");
5686 if (qemu_init_main_loop()) {
5687 fprintf(stderr
, "qemu_init_main_loop failed\n");
5690 linux_boot
= (kernel_filename
!= NULL
);
5692 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5693 fprintf(stderr
, "-append only allowed with -kernel option\n");
5697 if (!linux_boot
&& initrd_filename
!= NULL
) {
5698 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5703 /* Win32 doesn't support line-buffering and requires size >= 2 */
5704 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5707 if (init_timer_alarm() < 0) {
5708 fprintf(stderr
, "could not initialize alarm timer\n");
5711 if (use_icount
&& icount_time_shift
< 0) {
5713 /* 125MIPS seems a reasonable initial guess at the guest speed.
5714 It will be corrected fairly quickly anyway. */
5715 icount_time_shift
= 3;
5716 init_icount_adjust();
5723 if (net_init_clients() < 0) {
5727 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5728 net_set_boot_mask(net_boot
);
5730 /* init the bluetooth world */
5731 if (foreach_device_config(DEV_BT
, bt_parse
))
5734 /* init the memory */
5736 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5738 /* init the dynamic translator */
5739 cpu_exec_init_all(tb_size
* 1024 * 1024);
5741 bdrv_init_with_whitelist();
5745 if (default_cdrom
) {
5746 /* we always create the cdrom drive, even if no disk is there */
5747 drive_add(NULL
, CDROM_ALIAS
);
5750 if (default_floppy
) {
5751 /* we always create at least one floppy */
5752 drive_add(NULL
, FD_ALIAS
, 0);
5755 if (default_sdcard
) {
5756 /* we always create one sd slot, even if no card is in it */
5757 drive_add(NULL
, SD_ALIAS
);
5760 /* open the virtual block devices */
5762 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5763 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5766 vmstate_register(0, &vmstate_timers
,&timers_state
);
5767 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5770 if (nb_numa_nodes
> 0) {
5773 if (nb_numa_nodes
> smp_cpus
) {
5774 nb_numa_nodes
= smp_cpus
;
5777 /* If no memory size if given for any node, assume the default case
5778 * and distribute the available memory equally across all nodes
5780 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5781 if (node_mem
[i
] != 0)
5784 if (i
== nb_numa_nodes
) {
5785 uint64_t usedmem
= 0;
5787 /* On Linux, the each node's border has to be 8MB aligned,
5788 * the final node gets the rest.
5790 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5791 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5792 usedmem
+= node_mem
[i
];
5794 node_mem
[i
] = ram_size
- usedmem
;
5797 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5798 if (node_cpumask
[i
] != 0)
5801 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5802 * must cope with this anyway, because there are BIOSes out there in
5803 * real machines which also use this scheme.
5805 if (i
== nb_numa_nodes
) {
5806 for (i
= 0; i
< smp_cpus
; i
++) {
5807 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5812 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5814 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5816 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5818 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5821 module_call_init(MODULE_INIT_DEVICE
);
5824 i
= select_watchdog(watchdog
);
5826 exit (i
== 1 ? 1 : 0);
5829 if (machine
->compat_props
) {
5830 qdev_prop_register_global_list(machine
->compat_props
);
5834 machine
->init(ram_size
, boot_devices
,
5835 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5839 /* must be after terminal init, SDL library changes signal handlers */
5843 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5844 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5845 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5851 current_machine
= machine
;
5853 /* init USB devices */
5855 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5859 /* init generic devices */
5860 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5864 dumb_display_init();
5865 /* just use the first displaystate for the moment */
5868 if (display_type
== DT_DEFAULT
) {
5869 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5870 display_type
= DT_SDL
;
5872 display_type
= DT_VNC
;
5873 vnc_display
= "localhost:0,to=99";
5879 switch (display_type
) {
5882 #if defined(CONFIG_CURSES)
5884 curses_display_init(ds
, full_screen
);
5887 #if defined(CONFIG_SDL)
5889 sdl_display_init(ds
, full_screen
, no_frame
);
5891 #elif defined(CONFIG_COCOA)
5893 cocoa_display_init(ds
, full_screen
);
5897 vnc_display_init(ds
);
5898 if (vnc_display_open(ds
, vnc_display
) < 0)
5901 if (show_vnc_port
) {
5902 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5910 dcl
= ds
->listeners
;
5911 while (dcl
!= NULL
) {
5912 if (dcl
->dpy_refresh
!= NULL
) {
5913 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5914 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5919 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5920 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5921 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5924 text_consoles_set_display(display_state
);
5926 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5929 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5930 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5935 qdev_machine_creation_done();
5937 if (rom_load_all() != 0) {
5938 fprintf(stderr
, "rom loading failed\n");
5942 qemu_system_reset();
5944 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5950 qemu_start_incoming_migration(incoming
);
5951 } else if (autostart
) {
5961 len
= write(fds
[1], &status
, 1);
5962 if (len
== -1 && (errno
== EINTR
))
5969 perror("not able to chdir to /");
5972 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5978 pwd
= getpwnam(run_as
);
5980 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5986 if (chroot(chroot_dir
) < 0) {
5987 fprintf(stderr
, "chroot failed\n");
5991 perror("not able to chdir to /");
5997 if (setgid(pwd
->pw_gid
) < 0) {
5998 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6001 if (setuid(pwd
->pw_uid
) < 0) {
6002 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6005 if (setuid(0) != -1) {
6006 fprintf(stderr
, "Dropping privileges failed\n");