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 DisplayType display_type
= DT_DEFAULT
;
186 const char* keyboard_layout
= NULL
;
189 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 QEMUClock
*rtc_clock
;
195 int vga_interface_type
= VGA_NONE
;
197 int graphic_width
= 1024;
198 int graphic_height
= 768;
199 int graphic_depth
= 8;
201 int graphic_width
= 800;
202 int graphic_height
= 600;
203 int graphic_depth
= 15;
205 static int full_screen
= 0;
207 static int no_frame
= 0;
210 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
211 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
212 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
214 int win2k_install_hack
= 0;
223 const char *vnc_display
;
224 int acpi_enabled
= 1;
230 int graphic_rotate
= 0;
231 uint8_t irq0override
= 1;
235 const char *watchdog
;
236 const char *option_rom
[MAX_OPTION_ROMS
];
238 int semihosting_enabled
= 0;
242 const char *qemu_name
;
245 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
246 unsigned int nb_prom_envs
= 0;
247 const char *prom_envs
[MAX_PROM_ENVS
];
252 uint64_t node_mem
[MAX_NODES
];
253 uint64_t node_cpumask
[MAX_NODES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int timer_alarm_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
266 static QEMUTimer
*nographic_timer
;
268 uint8_t qemu_uuid
[16];
270 static QEMUBootSetHandler
*boot_set_handler
;
271 static void *boot_set_opaque
;
273 static int default_serial
= 1;
274 static int default_parallel
= 1;
275 static int default_virtcon
= 1;
276 static int default_monitor
= 1;
277 static int default_vga
= 1;
278 static int default_floppy
= 1;
279 static int default_cdrom
= 1;
280 static int default_sdcard
= 1;
286 { .driver
= "isa-serial", .flag
= &default_serial
},
287 { .driver
= "isa-parallel", .flag
= &default_parallel
},
288 { .driver
= "isa-fdc", .flag
= &default_floppy
},
289 { .driver
= "ide-drive", .flag
= &default_cdrom
},
290 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 { .driver
= "VGA", .flag
= &default_vga
},
294 { .driver
= "cirrus-vga", .flag
= &default_vga
},
295 { .driver
= "vmware-svga", .flag
= &default_vga
},
298 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
300 const char *driver
= qemu_opt_get(opts
, "driver");
305 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
306 if (strcmp(default_list
[i
].driver
, driver
) != 0)
308 *(default_list
[i
].flag
) = 0;
313 /***********************************************************/
314 /* x86 ISA bus support */
316 target_phys_addr_t isa_mem_base
= 0;
319 /***********************************************************/
320 void hw_error(const char *fmt
, ...)
326 fprintf(stderr
, "qemu: hardware error: ");
327 vfprintf(stderr
, fmt
, ap
);
328 fprintf(stderr
, "\n");
329 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
330 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
332 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
334 cpu_dump_state(env
, stderr
, fprintf
, 0);
341 static void set_proc_name(const char *s
)
343 #if defined(__linux__) && defined(PR_SET_NAME)
347 name
[sizeof(name
) - 1] = 0;
348 strncpy(name
, s
, sizeof(name
));
349 /* Could rewrite argv[0] too, but that's a bit more complicated.
350 This simple way is enough for `top'. */
351 prctl(PR_SET_NAME
, name
);
358 static QEMUBalloonEvent
*qemu_balloon_event
;
359 void *qemu_balloon_event_opaque
;
361 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
363 qemu_balloon_event
= func
;
364 qemu_balloon_event_opaque
= opaque
;
367 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
369 if (qemu_balloon_event
) {
370 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
377 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
379 if (qemu_balloon_event
) {
380 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
388 /***********************************************************/
389 /* real time host monotonic timer */
391 /* compute with 96 bit intermediate result: (a*b)/c */
392 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
397 #ifdef HOST_WORDS_BIGENDIAN
407 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
408 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
411 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
415 static int64_t get_clock_realtime(void)
419 gettimeofday(&tv
, NULL
);
420 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
425 static int64_t clock_freq
;
427 static void init_get_clock(void)
431 ret
= QueryPerformanceFrequency(&freq
);
433 fprintf(stderr
, "Could not calibrate ticks\n");
436 clock_freq
= freq
.QuadPart
;
439 static int64_t get_clock(void)
442 QueryPerformanceCounter(&ti
);
443 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
448 static int use_rt_clock
;
450 static void init_get_clock(void)
453 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
454 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
457 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
464 static int64_t get_clock(void)
466 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
467 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
470 clock_gettime(CLOCK_MONOTONIC
, &ts
);
471 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
475 /* XXX: using gettimeofday leads to problems if the date
476 changes, so it should be avoided. */
477 return get_clock_realtime();
482 /* Return the virtual CPU time, based on the instruction counter. */
483 static int64_t cpu_get_icount(void)
486 CPUState
*env
= cpu_single_env
;;
487 icount
= qemu_icount
;
490 fprintf(stderr
, "Bad clock read\n");
491 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
493 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
496 /***********************************************************/
497 /* guest cycle counter */
499 typedef struct TimersState
{
500 int64_t cpu_ticks_prev
;
501 int64_t cpu_ticks_offset
;
502 int64_t cpu_clock_offset
;
503 int32_t cpu_ticks_enabled
;
507 TimersState timers_state
;
509 /* return the host CPU cycle counter and handle stop/restart */
510 int64_t cpu_get_ticks(void)
513 return cpu_get_icount();
515 if (!timers_state
.cpu_ticks_enabled
) {
516 return timers_state
.cpu_ticks_offset
;
519 ticks
= cpu_get_real_ticks();
520 if (timers_state
.cpu_ticks_prev
> ticks
) {
521 /* Note: non increasing ticks may happen if the host uses
523 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
525 timers_state
.cpu_ticks_prev
= ticks
;
526 return ticks
+ timers_state
.cpu_ticks_offset
;
530 /* return the host CPU monotonic timer and handle stop/restart */
531 static int64_t cpu_get_clock(void)
534 if (!timers_state
.cpu_ticks_enabled
) {
535 return timers_state
.cpu_clock_offset
;
538 return ti
+ timers_state
.cpu_clock_offset
;
542 /* enable cpu_get_ticks() */
543 void cpu_enable_ticks(void)
545 if (!timers_state
.cpu_ticks_enabled
) {
546 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
547 timers_state
.cpu_clock_offset
-= get_clock();
548 timers_state
.cpu_ticks_enabled
= 1;
552 /* disable cpu_get_ticks() : the clock is stopped. You must not call
553 cpu_get_ticks() after that. */
554 void cpu_disable_ticks(void)
556 if (timers_state
.cpu_ticks_enabled
) {
557 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
558 timers_state
.cpu_clock_offset
= cpu_get_clock();
559 timers_state
.cpu_ticks_enabled
= 0;
563 /***********************************************************/
566 #define QEMU_CLOCK_REALTIME 0
567 #define QEMU_CLOCK_VIRTUAL 1
568 #define QEMU_CLOCK_HOST 2
572 /* XXX: add frequency */
580 struct QEMUTimer
*next
;
583 struct qemu_alarm_timer
{
587 int (*start
)(struct qemu_alarm_timer
*t
);
588 void (*stop
)(struct qemu_alarm_timer
*t
);
589 void (*rearm
)(struct qemu_alarm_timer
*t
);
593 #define ALARM_FLAG_DYNTICKS 0x1
594 #define ALARM_FLAG_EXPIRED 0x2
596 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
598 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
601 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
603 if (!alarm_has_dynticks(t
))
609 /* TODO: MIN_TIMER_REARM_US should be optimized */
610 #define MIN_TIMER_REARM_US 250
612 static struct qemu_alarm_timer
*alarm_timer
;
616 struct qemu_alarm_win32
{
619 } alarm_win32_data
= {0, -1};
621 static int win32_start_timer(struct qemu_alarm_timer
*t
);
622 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
623 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
627 static int unix_start_timer(struct qemu_alarm_timer
*t
);
628 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
632 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
633 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
634 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
636 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
637 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
639 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
640 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
642 #endif /* __linux__ */
646 /* Correlation between real and virtual time is always going to be
647 fairly approximate, so ignore small variation.
648 When the guest is idle real and virtual time will be aligned in
650 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
652 static void icount_adjust(void)
657 static int64_t last_delta
;
658 /* If the VM is not running, then do nothing. */
662 cur_time
= cpu_get_clock();
663 cur_icount
= qemu_get_clock(vm_clock
);
664 delta
= cur_icount
- cur_time
;
665 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
667 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
668 && icount_time_shift
> 0) {
669 /* The guest is getting too far ahead. Slow time down. */
673 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
674 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
675 /* The guest is getting too far behind. Speed time up. */
679 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
682 static void icount_adjust_rt(void * opaque
)
684 qemu_mod_timer(icount_rt_timer
,
685 qemu_get_clock(rt_clock
) + 1000);
689 static void icount_adjust_vm(void * opaque
)
691 qemu_mod_timer(icount_vm_timer
,
692 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
696 static void init_icount_adjust(void)
698 /* Have both realtime and virtual time triggers for speed adjustment.
699 The realtime trigger catches emulated time passing too slowly,
700 the virtual time trigger catches emulated time passing too fast.
701 Realtime triggers occur even when idle, so use them less frequently
703 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
704 qemu_mod_timer(icount_rt_timer
,
705 qemu_get_clock(rt_clock
) + 1000);
706 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
707 qemu_mod_timer(icount_vm_timer
,
708 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
711 static struct qemu_alarm_timer alarm_timers
[] = {
714 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
715 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
716 /* HPET - if available - is preferred */
717 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
718 /* ...otherwise try RTC */
719 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
721 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
723 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
724 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
725 {"win32", 0, win32_start_timer
,
726 win32_stop_timer
, NULL
, &alarm_win32_data
},
731 static void show_available_alarms(void)
735 printf("Available alarm timers, in order of precedence:\n");
736 for (i
= 0; alarm_timers
[i
].name
; i
++)
737 printf("%s\n", alarm_timers
[i
].name
);
740 static void configure_alarms(char const *opt
)
744 int count
= ARRAY_SIZE(alarm_timers
) - 1;
747 struct qemu_alarm_timer tmp
;
749 if (!strcmp(opt
, "?")) {
750 show_available_alarms();
754 arg
= qemu_strdup(opt
);
756 /* Reorder the array */
757 name
= strtok(arg
, ",");
759 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
760 if (!strcmp(alarm_timers
[i
].name
, name
))
765 fprintf(stderr
, "Unknown clock %s\n", name
);
774 tmp
= alarm_timers
[i
];
775 alarm_timers
[i
] = alarm_timers
[cur
];
776 alarm_timers
[cur
] = tmp
;
780 name
= strtok(NULL
, ",");
786 /* Disable remaining timers */
787 for (i
= cur
; i
< count
; i
++)
788 alarm_timers
[i
].name
= NULL
;
790 show_available_alarms();
795 #define QEMU_NUM_CLOCKS 3
799 QEMUClock
*host_clock
;
801 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
803 static QEMUClock
*qemu_new_clock(int type
)
806 clock
= qemu_mallocz(sizeof(QEMUClock
));
811 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
815 ts
= qemu_mallocz(sizeof(QEMUTimer
));
822 void qemu_free_timer(QEMUTimer
*ts
)
827 /* stop a timer, but do not dealloc it */
828 void qemu_del_timer(QEMUTimer
*ts
)
832 /* NOTE: this code must be signal safe because
833 qemu_timer_expired() can be called from a signal. */
834 pt
= &active_timers
[ts
->clock
->type
];
847 /* modify the current timer so that it will be fired when current_time
848 >= expire_time. The corresponding callback will be called. */
849 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
855 /* add the timer in the sorted list */
856 /* NOTE: this code must be signal safe because
857 qemu_timer_expired() can be called from a signal. */
858 pt
= &active_timers
[ts
->clock
->type
];
863 if (t
->expire_time
> expire_time
)
867 ts
->expire_time
= expire_time
;
871 /* Rearm if necessary */
872 if (pt
== &active_timers
[ts
->clock
->type
]) {
873 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
874 qemu_rearm_alarm_timer(alarm_timer
);
876 /* Interrupt execution to force deadline recalculation. */
882 int qemu_timer_pending(QEMUTimer
*ts
)
885 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
892 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
896 return (timer_head
->expire_time
<= current_time
);
899 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
905 if (!ts
|| ts
->expire_time
> current_time
)
907 /* remove timer from the list before calling the callback */
908 *ptimer_head
= ts
->next
;
911 /* run the callback (the timer list can be modified) */
916 int64_t qemu_get_clock(QEMUClock
*clock
)
918 switch(clock
->type
) {
919 case QEMU_CLOCK_REALTIME
:
920 return get_clock() / 1000000;
922 case QEMU_CLOCK_VIRTUAL
:
924 return cpu_get_icount();
926 return cpu_get_clock();
928 case QEMU_CLOCK_HOST
:
929 return get_clock_realtime();
933 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
935 switch(clock
->type
) {
936 case QEMU_CLOCK_REALTIME
:
939 case QEMU_CLOCK_VIRTUAL
:
941 return cpu_get_icount();
943 return cpu_get_clock();
945 case QEMU_CLOCK_HOST
:
946 return get_clock_realtime();
950 static void init_clocks(void)
953 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
954 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
955 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
957 rtc_clock
= host_clock
;
961 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
963 uint64_t expire_time
;
965 if (qemu_timer_pending(ts
)) {
966 expire_time
= ts
->expire_time
;
970 qemu_put_be64(f
, expire_time
);
973 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
975 uint64_t expire_time
;
977 expire_time
= qemu_get_be64(f
);
978 if (expire_time
!= -1) {
979 qemu_mod_timer(ts
, expire_time
);
985 static const VMStateDescription vmstate_timers
= {
988 .minimum_version_id
= 1,
989 .minimum_version_id_old
= 1,
990 .fields
= (VMStateField
[]) {
991 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
992 VMSTATE_INT64(dummy
, TimersState
),
993 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
994 VMSTATE_END_OF_LIST()
998 static void qemu_event_increment(void);
1001 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1002 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1005 static void host_alarm_handler(int host_signum
)
1009 #define DISP_FREQ 1000
1011 static int64_t delta_min
= INT64_MAX
;
1012 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1014 ti
= qemu_get_clock(vm_clock
);
1015 if (last_clock
!= 0) {
1016 delta
= ti
- last_clock
;
1017 if (delta
< delta_min
)
1019 if (delta
> delta_max
)
1022 if (++count
== DISP_FREQ
) {
1023 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1024 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1025 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1026 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1027 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1029 delta_min
= INT64_MAX
;
1037 if (alarm_has_dynticks(alarm_timer
) ||
1039 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1040 qemu_get_clock(vm_clock
))) ||
1041 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1042 qemu_get_clock(rt_clock
)) ||
1043 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1044 qemu_get_clock(host_clock
))) {
1045 qemu_event_increment();
1046 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1048 #ifndef CONFIG_IOTHREAD
1050 /* stop the currently executing cpu because a timer occured */
1054 timer_alarm_pending
= 1;
1055 qemu_notify_event();
1059 static int64_t qemu_next_deadline(void)
1061 /* To avoid problems with overflow limit this to 2^32. */
1062 int64_t delta
= INT32_MAX
;
1064 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1065 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1066 qemu_get_clock(vm_clock
);
1068 if (active_timers
[QEMU_CLOCK_HOST
]) {
1069 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1070 qemu_get_clock(host_clock
);
1081 #if defined(__linux__)
1082 static uint64_t qemu_next_deadline_dyntick(void)
1090 delta
= (qemu_next_deadline() + 999) / 1000;
1092 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1093 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1094 qemu_get_clock(rt_clock
))*1000;
1095 if (rtdelta
< delta
)
1099 if (delta
< MIN_TIMER_REARM_US
)
1100 delta
= MIN_TIMER_REARM_US
;
1108 /* Sets a specific flag */
1109 static int fcntl_setfl(int fd
, int flag
)
1113 flags
= fcntl(fd
, F_GETFL
);
1117 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1123 #if defined(__linux__)
1125 #define RTC_FREQ 1024
1127 static void enable_sigio_timer(int fd
)
1129 struct sigaction act
;
1132 sigfillset(&act
.sa_mask
);
1134 act
.sa_handler
= host_alarm_handler
;
1136 sigaction(SIGIO
, &act
, NULL
);
1137 fcntl_setfl(fd
, O_ASYNC
);
1138 fcntl(fd
, F_SETOWN
, getpid());
1141 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1143 struct hpet_info info
;
1146 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1151 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1153 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1154 "error, but for better emulation accuracy type:\n"
1155 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1159 /* Check capabilities */
1160 r
= ioctl(fd
, HPET_INFO
, &info
);
1164 /* Enable periodic mode */
1165 r
= ioctl(fd
, HPET_EPI
, 0);
1166 if (info
.hi_flags
&& (r
< 0))
1169 /* Enable interrupt */
1170 r
= ioctl(fd
, HPET_IE_ON
, 0);
1174 enable_sigio_timer(fd
);
1175 t
->priv
= (void *)(long)fd
;
1183 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1185 int fd
= (long)t
->priv
;
1190 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1193 unsigned long current_rtc_freq
= 0;
1195 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1198 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1199 if (current_rtc_freq
!= RTC_FREQ
&&
1200 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1201 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1202 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1203 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1206 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1212 enable_sigio_timer(rtc_fd
);
1214 t
->priv
= (void *)(long)rtc_fd
;
1219 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1221 int rtc_fd
= (long)t
->priv
;
1226 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1230 struct sigaction act
;
1232 sigfillset(&act
.sa_mask
);
1234 act
.sa_handler
= host_alarm_handler
;
1236 sigaction(SIGALRM
, &act
, NULL
);
1239 * Initialize ev struct to 0 to avoid valgrind complaining
1240 * about uninitialized data in timer_create call
1242 memset(&ev
, 0, sizeof(ev
));
1243 ev
.sigev_value
.sival_int
= 0;
1244 ev
.sigev_notify
= SIGEV_SIGNAL
;
1245 ev
.sigev_signo
= SIGALRM
;
1247 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1248 perror("timer_create");
1250 /* disable dynticks */
1251 fprintf(stderr
, "Dynamic Ticks disabled\n");
1256 t
->priv
= (void *)(long)host_timer
;
1261 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1263 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1265 timer_delete(host_timer
);
1268 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1270 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1271 struct itimerspec timeout
;
1272 int64_t nearest_delta_us
= INT64_MAX
;
1275 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1276 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1277 !active_timers
[QEMU_CLOCK_HOST
])
1280 nearest_delta_us
= qemu_next_deadline_dyntick();
1282 /* check whether a timer is already running */
1283 if (timer_gettime(host_timer
, &timeout
)) {
1285 fprintf(stderr
, "Internal timer error: aborting\n");
1288 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1289 if (current_us
&& current_us
<= nearest_delta_us
)
1292 timeout
.it_interval
.tv_sec
= 0;
1293 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1294 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1295 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1296 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1298 fprintf(stderr
, "Internal timer error: aborting\n");
1303 #endif /* defined(__linux__) */
1305 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1307 struct sigaction act
;
1308 struct itimerval itv
;
1312 sigfillset(&act
.sa_mask
);
1314 act
.sa_handler
= host_alarm_handler
;
1316 sigaction(SIGALRM
, &act
, NULL
);
1318 itv
.it_interval
.tv_sec
= 0;
1319 /* for i386 kernel 2.6 to get 1 ms */
1320 itv
.it_interval
.tv_usec
= 999;
1321 itv
.it_value
.tv_sec
= 0;
1322 itv
.it_value
.tv_usec
= 10 * 1000;
1324 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1331 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1333 struct itimerval itv
;
1335 memset(&itv
, 0, sizeof(itv
));
1336 setitimer(ITIMER_REAL
, &itv
, NULL
);
1339 #endif /* !defined(_WIN32) */
1344 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1347 struct qemu_alarm_win32
*data
= t
->priv
;
1350 memset(&tc
, 0, sizeof(tc
));
1351 timeGetDevCaps(&tc
, sizeof(tc
));
1353 if (data
->period
< tc
.wPeriodMin
)
1354 data
->period
= tc
.wPeriodMin
;
1356 timeBeginPeriod(data
->period
);
1358 flags
= TIME_CALLBACK_FUNCTION
;
1359 if (alarm_has_dynticks(t
))
1360 flags
|= TIME_ONESHOT
;
1362 flags
|= TIME_PERIODIC
;
1364 data
->timerId
= timeSetEvent(1, // interval (ms)
1365 data
->period
, // resolution
1366 host_alarm_handler
, // function
1367 (DWORD
)t
, // parameter
1370 if (!data
->timerId
) {
1371 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1373 timeEndPeriod(data
->period
);
1380 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1382 struct qemu_alarm_win32
*data
= t
->priv
;
1384 timeKillEvent(data
->timerId
);
1385 timeEndPeriod(data
->period
);
1388 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1390 struct qemu_alarm_win32
*data
= t
->priv
;
1392 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1393 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1394 !active_timers
[QEMU_CLOCK_HOST
])
1397 timeKillEvent(data
->timerId
);
1399 data
->timerId
= timeSetEvent(1,
1403 TIME_ONESHOT
| TIME_PERIODIC
);
1405 if (!data
->timerId
) {
1406 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1409 timeEndPeriod(data
->period
);
1416 static int init_timer_alarm(void)
1418 struct qemu_alarm_timer
*t
= NULL
;
1421 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1422 t
= &alarm_timers
[i
];
1442 static void quit_timers(void)
1444 alarm_timer
->stop(alarm_timer
);
1448 /***********************************************************/
1449 /* host time/date access */
1450 void qemu_get_timedate(struct tm
*tm
, int offset
)
1457 if (rtc_date_offset
== -1) {
1461 ret
= localtime(&ti
);
1463 ti
-= rtc_date_offset
;
1467 memcpy(tm
, ret
, sizeof(struct tm
));
1470 int qemu_timedate_diff(struct tm
*tm
)
1474 if (rtc_date_offset
== -1)
1476 seconds
= mktimegm(tm
);
1478 seconds
= mktime(tm
);
1480 seconds
= mktimegm(tm
) + rtc_date_offset
;
1482 return seconds
- time(NULL
);
1485 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1487 time_t rtc_start_date
;
1490 if (!strcmp(startdate
, "now") && legacy
) {
1491 rtc_date_offset
= -1;
1493 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1501 } else if (sscanf(startdate
, "%d-%d-%d",
1504 &tm
.tm_mday
) == 3) {
1513 rtc_start_date
= mktimegm(&tm
);
1514 if (rtc_start_date
== -1) {
1516 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1517 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1520 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1524 static void configure_rtc(QemuOpts
*opts
)
1528 value
= qemu_opt_get(opts
, "base");
1530 if (!strcmp(value
, "utc")) {
1532 } else if (!strcmp(value
, "localtime")) {
1535 configure_rtc_date_offset(value
, 0);
1538 value
= qemu_opt_get(opts
, "clock");
1540 if (!strcmp(value
, "host")) {
1541 rtc_clock
= host_clock
;
1542 } else if (!strcmp(value
, "vm")) {
1543 rtc_clock
= vm_clock
;
1545 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1549 #ifdef CONFIG_TARGET_I386
1550 value
= qemu_opt_get(opts
, "driftfix");
1552 if (!strcmp(buf
, "slew")) {
1554 } else if (!strcmp(buf
, "none")) {
1557 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1565 static void socket_cleanup(void)
1570 static int socket_init(void)
1575 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1577 err
= WSAGetLastError();
1578 fprintf(stderr
, "WSAStartup: %d\n", err
);
1581 atexit(socket_cleanup
);
1586 /***********************************************************/
1587 /* Bluetooth support */
1590 static struct HCIInfo
*hci_table
[MAX_NICS
];
1592 static struct bt_vlan_s
{
1593 struct bt_scatternet_s net
;
1595 struct bt_vlan_s
*next
;
1598 /* find or alloc a new bluetooth "VLAN" */
1599 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1601 struct bt_vlan_s
**pvlan
, *vlan
;
1602 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1606 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1608 pvlan
= &first_bt_vlan
;
1609 while (*pvlan
!= NULL
)
1610 pvlan
= &(*pvlan
)->next
;
1615 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1619 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1624 static struct HCIInfo null_hci
= {
1625 .cmd_send
= null_hci_send
,
1626 .sco_send
= null_hci_send
,
1627 .acl_send
= null_hci_send
,
1628 .bdaddr_set
= null_hci_addr_set
,
1631 struct HCIInfo
*qemu_next_hci(void)
1633 if (cur_hci
== nb_hcis
)
1636 return hci_table
[cur_hci
++];
1639 static struct HCIInfo
*hci_init(const char *str
)
1642 struct bt_scatternet_s
*vlan
= 0;
1644 if (!strcmp(str
, "null"))
1647 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1649 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1650 else if (!strncmp(str
, "hci", 3)) {
1653 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1654 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1659 vlan
= qemu_find_bt_vlan(0);
1661 return bt_new_hci(vlan
);
1664 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1669 static int bt_hci_parse(const char *str
)
1671 struct HCIInfo
*hci
;
1674 if (nb_hcis
>= MAX_NICS
) {
1675 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1679 hci
= hci_init(str
);
1688 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1689 hci
->bdaddr_set(hci
, bdaddr
.b
);
1691 hci_table
[nb_hcis
++] = hci
;
1696 static void bt_vhci_add(int vlan_id
)
1698 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1701 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1702 "an empty scatternet %i\n", vlan_id
);
1704 bt_vhci_init(bt_new_hci(vlan
));
1707 static struct bt_device_s
*bt_device_add(const char *opt
)
1709 struct bt_scatternet_s
*vlan
;
1711 char *endp
= strstr(opt
, ",vlan=");
1712 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1715 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1718 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1720 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1725 vlan
= qemu_find_bt_vlan(vlan_id
);
1728 fprintf(stderr
, "qemu: warning: adding a slave device to "
1729 "an empty scatternet %i\n", vlan_id
);
1731 if (!strcmp(devname
, "keyboard"))
1732 return bt_keyboard_init(vlan
);
1734 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1738 static int bt_parse(const char *opt
)
1740 const char *endp
, *p
;
1743 if (strstart(opt
, "hci", &endp
)) {
1744 if (!*endp
|| *endp
== ',') {
1746 if (!strstart(endp
, ",vlan=", 0))
1749 return bt_hci_parse(opt
);
1751 } else if (strstart(opt
, "vhci", &endp
)) {
1752 if (!*endp
|| *endp
== ',') {
1754 if (strstart(endp
, ",vlan=", &p
)) {
1755 vlan
= strtol(p
, (char **) &endp
, 0);
1757 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1761 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1770 } else if (strstart(opt
, "device:", &endp
))
1771 return !bt_device_add(endp
);
1773 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1777 /***********************************************************/
1778 /* QEMU Block devices */
1780 #define HD_ALIAS "index=%d,media=disk"
1781 #define CDROM_ALIAS "index=2,media=cdrom"
1782 #define FD_ALIAS "index=%d,if=floppy"
1783 #define PFLASH_ALIAS "if=pflash"
1784 #define MTD_ALIAS "if=mtd"
1785 #define SD_ALIAS "index=0,if=sd"
1787 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1794 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1797 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1799 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1800 __FUNCTION__
, optstr
);
1804 qemu_opt_set(opts
, "file", file
);
1808 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1812 /* seek interface, bus and unit */
1814 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1815 if (dinfo
->type
== type
&&
1816 dinfo
->bus
== bus
&&
1817 dinfo
->unit
== unit
)
1824 DriveInfo
*drive_get_by_id(const char *id
)
1828 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1829 if (strcmp(id
, dinfo
->id
))
1836 int drive_get_max_bus(BlockInterfaceType type
)
1842 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1843 if(dinfo
->type
== type
&&
1844 dinfo
->bus
> max_bus
)
1845 max_bus
= dinfo
->bus
;
1850 const char *drive_get_serial(BlockDriverState
*bdrv
)
1854 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1855 if (dinfo
->bdrv
== bdrv
)
1856 return dinfo
->serial
;
1862 BlockInterfaceErrorAction
drive_get_on_error(
1863 BlockDriverState
*bdrv
, int is_read
)
1867 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1868 if (dinfo
->bdrv
== bdrv
)
1869 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1872 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1875 static void bdrv_format_print(void *opaque
, const char *name
)
1877 fprintf(stderr
, " %s", name
);
1880 void drive_uninit(DriveInfo
*dinfo
)
1882 qemu_opts_del(dinfo
->opts
);
1883 bdrv_delete(dinfo
->bdrv
);
1884 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1888 static int parse_block_error_action(const char *buf
, int is_read
)
1890 if (!strcmp(buf
, "ignore")) {
1891 return BLOCK_ERR_IGNORE
;
1892 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1893 return BLOCK_ERR_STOP_ENOSPC
;
1894 } else if (!strcmp(buf
, "stop")) {
1895 return BLOCK_ERR_STOP_ANY
;
1896 } else if (!strcmp(buf
, "report")) {
1897 return BLOCK_ERR_REPORT
;
1899 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1900 buf
, is_read
? "read" : "write");
1905 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1909 const char *file
= NULL
;
1912 const char *mediastr
= "";
1913 BlockInterfaceType type
;
1914 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1915 int bus_id
, unit_id
;
1916 int cyls
, heads
, secs
, translation
;
1917 BlockDriver
*drv
= NULL
;
1918 QEMUMachine
*machine
= opaque
;
1925 int on_read_error
, on_write_error
;
1926 const char *devaddr
;
1932 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1935 if (machine
&& machine
->use_scsi
) {
1937 max_devs
= MAX_SCSI_DEVS
;
1938 pstrcpy(devname
, sizeof(devname
), "scsi");
1941 max_devs
= MAX_IDE_DEVS
;
1942 pstrcpy(devname
, sizeof(devname
), "ide");
1946 /* extract parameters */
1947 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1948 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1949 index
= qemu_opt_get_number(opts
, "index", -1);
1951 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1952 heads
= qemu_opt_get_number(opts
, "heads", 0);
1953 secs
= qemu_opt_get_number(opts
, "secs", 0);
1955 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1956 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1958 file
= qemu_opt_get(opts
, "file");
1959 serial
= qemu_opt_get(opts
, "serial");
1961 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1962 pstrcpy(devname
, sizeof(devname
), buf
);
1963 if (!strcmp(buf
, "ide")) {
1965 max_devs
= MAX_IDE_DEVS
;
1966 } else if (!strcmp(buf
, "scsi")) {
1968 max_devs
= MAX_SCSI_DEVS
;
1969 } else if (!strcmp(buf
, "floppy")) {
1972 } else if (!strcmp(buf
, "pflash")) {
1975 } else if (!strcmp(buf
, "mtd")) {
1978 } else if (!strcmp(buf
, "sd")) {
1981 } else if (!strcmp(buf
, "virtio")) {
1984 } else if (!strcmp(buf
, "xen")) {
1987 } else if (!strcmp(buf
, "none")) {
1991 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1996 if (cyls
|| heads
|| secs
) {
1997 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
1998 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2001 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2002 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2005 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2006 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2011 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2014 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2018 if (!strcmp(buf
, "none"))
2019 translation
= BIOS_ATA_TRANSLATION_NONE
;
2020 else if (!strcmp(buf
, "lba"))
2021 translation
= BIOS_ATA_TRANSLATION_LBA
;
2022 else if (!strcmp(buf
, "auto"))
2023 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2025 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2030 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2031 if (!strcmp(buf
, "disk")) {
2033 } else if (!strcmp(buf
, "cdrom")) {
2034 if (cyls
|| secs
|| heads
) {
2036 "qemu: '%s' invalid physical CHS format\n", buf
);
2039 media
= MEDIA_CDROM
;
2041 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2046 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2047 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2049 else if (!strcmp(buf
, "writethrough"))
2051 else if (!strcmp(buf
, "writeback"))
2054 fprintf(stderr
, "qemu: invalid cache option\n");
2059 #ifdef CONFIG_LINUX_AIO
2060 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2061 if (!strcmp(buf
, "threads"))
2063 else if (!strcmp(buf
, "native"))
2066 fprintf(stderr
, "qemu: invalid aio option\n");
2072 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2073 if (strcmp(buf
, "?") == 0) {
2074 fprintf(stderr
, "qemu: Supported formats:");
2075 bdrv_iterate_format(bdrv_format_print
, NULL
);
2076 fprintf(stderr
, "\n");
2079 drv
= bdrv_find_whitelisted_format(buf
);
2081 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2086 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2087 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2088 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2089 fprintf(stderr
, "werror is no supported by this format\n");
2093 on_write_error
= parse_block_error_action(buf
, 0);
2094 if (on_write_error
< 0) {
2099 on_read_error
= BLOCK_ERR_REPORT
;
2100 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2101 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2102 fprintf(stderr
, "rerror is no supported by this format\n");
2106 on_read_error
= parse_block_error_action(buf
, 1);
2107 if (on_read_error
< 0) {
2112 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2113 if (type
!= IF_VIRTIO
) {
2114 fprintf(stderr
, "addr is not supported\n");
2119 /* compute bus and unit according index */
2122 if (bus_id
!= 0 || unit_id
!= -1) {
2124 "qemu: index cannot be used with bus and unit\n");
2132 unit_id
= index
% max_devs
;
2133 bus_id
= index
/ max_devs
;
2137 /* if user doesn't specify a unit_id,
2138 * try to find the first free
2141 if (unit_id
== -1) {
2143 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2145 if (max_devs
&& unit_id
>= max_devs
) {
2146 unit_id
-= max_devs
;
2154 if (max_devs
&& unit_id
>= max_devs
) {
2155 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2156 unit_id
, max_devs
- 1);
2161 * ignore multiple definitions
2164 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2171 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2172 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2173 dinfo
->id
= qemu_strdup(buf
);
2175 /* no id supplied -> create one */
2176 dinfo
->id
= qemu_mallocz(32);
2177 if (type
== IF_IDE
|| type
== IF_SCSI
)
2178 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2180 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2181 devname
, bus_id
, mediastr
, unit_id
);
2183 snprintf(dinfo
->id
, 32, "%s%s%i",
2184 devname
, mediastr
, unit_id
);
2186 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2187 dinfo
->devaddr
= devaddr
;
2189 dinfo
->bus
= bus_id
;
2190 dinfo
->unit
= unit_id
;
2191 dinfo
->on_read_error
= on_read_error
;
2192 dinfo
->on_write_error
= on_write_error
;
2195 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2196 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2206 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2207 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2211 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2216 /* FIXME: This isn't really a floppy, but it's a reasonable
2219 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2225 /* add virtio block device */
2226 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2227 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2228 qemu_opt_set(opts
, "drive", dinfo
->id
);
2230 qemu_opt_set(opts
, "addr", devaddr
);
2241 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2242 cache
= 2; /* always use write-back with snapshot */
2244 if (cache
== 0) /* no caching */
2245 bdrv_flags
|= BDRV_O_NOCACHE
;
2246 else if (cache
== 2) /* write-back */
2247 bdrv_flags
|= BDRV_O_CACHE_WB
;
2250 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2252 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2256 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2257 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2262 * cdrom is read-only. Set it now, after above interface checking
2263 * since readonly attribute not explicitly required, so no error.
2265 if (media
== MEDIA_CDROM
) {
2268 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2270 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2271 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2272 file
, strerror(errno
));
2276 if (bdrv_key_required(dinfo
->bdrv
))
2282 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2284 QEMUMachine
*machine
= opaque
;
2285 int fatal_error
= 0;
2287 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2294 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2296 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2297 qemu_opt_set(opts
, "snapshot", "on");
2302 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2304 boot_set_handler
= func
;
2305 boot_set_opaque
= opaque
;
2308 int qemu_boot_set(const char *boot_devices
)
2310 if (!boot_set_handler
) {
2313 return boot_set_handler(boot_set_opaque
, boot_devices
);
2316 static int parse_bootdevices(char *devices
)
2318 /* We just do some generic consistency checks */
2322 for (p
= devices
; *p
!= '\0'; p
++) {
2323 /* Allowed boot devices are:
2324 * a-b: floppy disk drives
2325 * c-f: IDE disk drives
2326 * g-m: machine implementation dependant drives
2327 * n-p: network devices
2328 * It's up to each machine implementation to check if the given boot
2329 * devices match the actual hardware implementation and firmware
2332 if (*p
< 'a' || *p
> 'p') {
2333 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2336 if (bitmap
& (1 << (*p
- 'a'))) {
2337 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2340 bitmap
|= 1 << (*p
- 'a');
2345 static void restore_boot_devices(void *opaque
)
2347 char *standard_boot_devices
= opaque
;
2349 qemu_boot_set(standard_boot_devices
);
2351 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2352 qemu_free(standard_boot_devices
);
2355 static void numa_add(const char *optarg
)
2359 unsigned long long value
, endvalue
;
2362 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2363 if (!strcmp(option
, "node")) {
2364 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2365 nodenr
= nb_numa_nodes
;
2367 nodenr
= strtoull(option
, NULL
, 10);
2370 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2371 node_mem
[nodenr
] = 0;
2373 value
= strtoull(option
, &endptr
, 0);
2375 case 0: case 'M': case 'm':
2382 node_mem
[nodenr
] = value
;
2384 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2385 node_cpumask
[nodenr
] = 0;
2387 value
= strtoull(option
, &endptr
, 10);
2390 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2392 if (*endptr
== '-') {
2393 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2394 if (endvalue
>= 63) {
2397 "only 63 CPUs in NUMA mode supported.\n");
2399 value
= (2ULL << endvalue
) - (1ULL << value
);
2401 value
= 1ULL << value
;
2404 node_cpumask
[nodenr
] = value
;
2411 static void smp_parse(const char *optarg
)
2413 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2417 smp
= strtoul(optarg
, &endptr
, 10);
2418 if (endptr
!= optarg
) {
2419 if (*endptr
== ',') {
2423 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2424 sockets
= strtoull(option
, NULL
, 10);
2425 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2426 cores
= strtoull(option
, NULL
, 10);
2427 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2428 threads
= strtoull(option
, NULL
, 10);
2429 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2430 max_cpus
= strtoull(option
, NULL
, 10);
2432 /* compute missing values, prefer sockets over cores over threads */
2433 if (smp
== 0 || sockets
== 0) {
2434 sockets
= sockets
> 0 ? sockets
: 1;
2435 cores
= cores
> 0 ? cores
: 1;
2436 threads
= threads
> 0 ? threads
: 1;
2438 smp
= cores
* threads
* sockets
;
2442 threads
= threads
> 0 ? threads
: 1;
2443 cores
= smp
/ (sockets
* threads
);
2446 threads
= smp
/ (cores
* sockets
);
2451 smp_cores
= cores
> 0 ? cores
: 1;
2452 smp_threads
= threads
> 0 ? threads
: 1;
2454 max_cpus
= smp_cpus
;
2457 /***********************************************************/
2460 static int usb_device_add(const char *devname
, int is_hotplug
)
2463 USBDevice
*dev
= NULL
;
2468 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2469 dev
= usbdevice_create(devname
);
2473 /* the other ones */
2474 if (strstart(devname
, "host:", &p
)) {
2475 dev
= usb_host_device_open(p
);
2476 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2477 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2478 bt_new_hci(qemu_find_bt_vlan(0)));
2489 static int usb_device_del(const char *devname
)
2494 if (strstart(devname
, "host:", &p
))
2495 return usb_host_device_close(p
);
2500 p
= strchr(devname
, '.');
2503 bus_num
= strtoul(devname
, NULL
, 0);
2504 addr
= strtoul(p
+ 1, NULL
, 0);
2506 return usb_device_delete_addr(bus_num
, addr
);
2509 static int usb_parse(const char *cmdline
)
2512 r
= usb_device_add(cmdline
, 0);
2514 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2519 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2521 const char *devname
= qdict_get_str(qdict
, "devname");
2522 if (usb_device_add(devname
, 1) < 0) {
2523 qemu_error("could not add USB device '%s'\n", devname
);
2527 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2529 const char *devname
= qdict_get_str(qdict
, "devname");
2530 if (usb_device_del(devname
) < 0) {
2531 qemu_error("could not delete USB device '%s'\n", devname
);
2535 /***********************************************************/
2536 /* PCMCIA/Cardbus */
2538 static struct pcmcia_socket_entry_s
{
2539 PCMCIASocket
*socket
;
2540 struct pcmcia_socket_entry_s
*next
;
2541 } *pcmcia_sockets
= 0;
2543 void pcmcia_socket_register(PCMCIASocket
*socket
)
2545 struct pcmcia_socket_entry_s
*entry
;
2547 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2548 entry
->socket
= socket
;
2549 entry
->next
= pcmcia_sockets
;
2550 pcmcia_sockets
= entry
;
2553 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2555 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2557 ptr
= &pcmcia_sockets
;
2558 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2559 if (entry
->socket
== socket
) {
2565 void pcmcia_info(Monitor
*mon
)
2567 struct pcmcia_socket_entry_s
*iter
;
2569 if (!pcmcia_sockets
)
2570 monitor_printf(mon
, "No PCMCIA sockets\n");
2572 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2573 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2574 iter
->socket
->attached
? iter
->socket
->card_string
:
2578 /***********************************************************/
2581 typedef struct IOHandlerRecord
{
2583 IOCanRWHandler
*fd_read_poll
;
2585 IOHandler
*fd_write
;
2588 /* temporary data */
2590 struct IOHandlerRecord
*next
;
2593 static IOHandlerRecord
*first_io_handler
;
2595 /* XXX: fd_read_poll should be suppressed, but an API change is
2596 necessary in the character devices to suppress fd_can_read(). */
2597 int qemu_set_fd_handler2(int fd
,
2598 IOCanRWHandler
*fd_read_poll
,
2600 IOHandler
*fd_write
,
2603 IOHandlerRecord
**pioh
, *ioh
;
2605 if (!fd_read
&& !fd_write
) {
2606 pioh
= &first_io_handler
;
2611 if (ioh
->fd
== fd
) {
2618 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2622 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2623 ioh
->next
= first_io_handler
;
2624 first_io_handler
= ioh
;
2627 ioh
->fd_read_poll
= fd_read_poll
;
2628 ioh
->fd_read
= fd_read
;
2629 ioh
->fd_write
= fd_write
;
2630 ioh
->opaque
= opaque
;
2636 int qemu_set_fd_handler(int fd
,
2638 IOHandler
*fd_write
,
2641 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2645 /***********************************************************/
2646 /* Polling handling */
2648 typedef struct PollingEntry
{
2651 struct PollingEntry
*next
;
2654 static PollingEntry
*first_polling_entry
;
2656 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2658 PollingEntry
**ppe
, *pe
;
2659 pe
= qemu_mallocz(sizeof(PollingEntry
));
2661 pe
->opaque
= opaque
;
2662 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2667 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2669 PollingEntry
**ppe
, *pe
;
2670 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2672 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2680 /***********************************************************/
2681 /* Wait objects support */
2682 typedef struct WaitObjects
{
2684 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2685 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2686 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2689 static WaitObjects wait_objects
= {0};
2691 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2693 WaitObjects
*w
= &wait_objects
;
2695 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2697 w
->events
[w
->num
] = handle
;
2698 w
->func
[w
->num
] = func
;
2699 w
->opaque
[w
->num
] = opaque
;
2704 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2707 WaitObjects
*w
= &wait_objects
;
2710 for (i
= 0; i
< w
->num
; i
++) {
2711 if (w
->events
[i
] == handle
)
2714 w
->events
[i
] = w
->events
[i
+ 1];
2715 w
->func
[i
] = w
->func
[i
+ 1];
2716 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2724 /***********************************************************/
2725 /* ram save/restore */
2727 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2728 #define RAM_SAVE_FLAG_COMPRESS 0x02
2729 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2730 #define RAM_SAVE_FLAG_PAGE 0x08
2731 #define RAM_SAVE_FLAG_EOS 0x10
2733 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2735 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2736 uint32_t *array
= (uint32_t *)page
;
2739 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2740 if (array
[i
] != val
)
2747 static int ram_save_block(QEMUFile
*f
)
2749 static ram_addr_t current_addr
= 0;
2750 ram_addr_t saved_addr
= current_addr
;
2751 ram_addr_t addr
= 0;
2754 while (addr
< last_ram_offset
) {
2755 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2758 cpu_physical_memory_reset_dirty(current_addr
,
2759 current_addr
+ TARGET_PAGE_SIZE
,
2760 MIGRATION_DIRTY_FLAG
);
2762 p
= qemu_get_ram_ptr(current_addr
);
2764 if (is_dup_page(p
, *p
)) {
2765 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2766 qemu_put_byte(f
, *p
);
2768 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2769 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2775 addr
+= TARGET_PAGE_SIZE
;
2776 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2782 static uint64_t bytes_transferred
;
2784 static ram_addr_t
ram_save_remaining(void)
2787 ram_addr_t count
= 0;
2789 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2790 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2797 uint64_t ram_bytes_remaining(void)
2799 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2802 uint64_t ram_bytes_transferred(void)
2804 return bytes_transferred
;
2807 uint64_t ram_bytes_total(void)
2809 return last_ram_offset
;
2812 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2815 uint64_t bytes_transferred_last
;
2817 uint64_t expected_time
= 0;
2820 cpu_physical_memory_set_dirty_tracking(0);
2824 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2825 qemu_file_set_error(f
);
2830 bytes_transferred
= 0;
2832 /* Make sure all dirty bits are set */
2833 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2834 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2835 cpu_physical_memory_set_dirty(addr
);
2838 /* Enable dirty memory tracking */
2839 cpu_physical_memory_set_dirty_tracking(1);
2841 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2844 bytes_transferred_last
= bytes_transferred
;
2845 bwidth
= qemu_get_clock_ns(rt_clock
);
2847 while (!qemu_file_rate_limit(f
)) {
2850 ret
= ram_save_block(f
);
2851 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2852 if (ret
== 0) /* no more blocks */
2856 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
2857 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2859 /* if we haven't transferred anything this round, force expected_time to a
2860 * a very high value, but without crashing */
2864 /* try transferring iterative blocks of memory */
2866 /* flush all remaining blocks regardless of rate limiting */
2867 while (ram_save_block(f
) != 0) {
2868 bytes_transferred
+= TARGET_PAGE_SIZE
;
2870 cpu_physical_memory_set_dirty_tracking(0);
2873 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2875 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2877 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2880 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2885 if (version_id
!= 3)
2889 addr
= qemu_get_be64(f
);
2891 flags
= addr
& ~TARGET_PAGE_MASK
;
2892 addr
&= TARGET_PAGE_MASK
;
2894 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2895 if (addr
!= last_ram_offset
)
2899 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2900 uint8_t ch
= qemu_get_byte(f
);
2901 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2904 (!kvm_enabled() || kvm_has_sync_mmu())) {
2905 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2908 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2909 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2911 if (qemu_file_has_error(f
)) {
2914 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2919 void qemu_service_io(void)
2921 qemu_notify_event();
2924 /***********************************************************/
2925 /* machine registration */
2927 static QEMUMachine
*first_machine
= NULL
;
2928 QEMUMachine
*current_machine
= NULL
;
2930 int qemu_register_machine(QEMUMachine
*m
)
2933 pm
= &first_machine
;
2941 static QEMUMachine
*find_machine(const char *name
)
2945 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2946 if (!strcmp(m
->name
, name
))
2948 if (m
->alias
&& !strcmp(m
->alias
, name
))
2954 static QEMUMachine
*find_default_machine(void)
2958 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2959 if (m
->is_default
) {
2966 /***********************************************************/
2967 /* main execution loop */
2969 static void gui_update(void *opaque
)
2971 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2972 DisplayState
*ds
= opaque
;
2973 DisplayChangeListener
*dcl
= ds
->listeners
;
2975 qemu_flush_coalesced_mmio_buffer();
2978 while (dcl
!= NULL
) {
2979 if (dcl
->gui_timer_interval
&&
2980 dcl
->gui_timer_interval
< interval
)
2981 interval
= dcl
->gui_timer_interval
;
2984 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
2987 static void nographic_update(void *opaque
)
2989 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2991 qemu_flush_coalesced_mmio_buffer();
2992 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
2995 struct vm_change_state_entry
{
2996 VMChangeStateHandler
*cb
;
2998 QLIST_ENTRY (vm_change_state_entry
) entries
;
3001 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3003 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3006 VMChangeStateEntry
*e
;
3008 e
= qemu_mallocz(sizeof (*e
));
3012 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3016 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3018 QLIST_REMOVE (e
, entries
);
3022 static void vm_state_notify(int running
, int reason
)
3024 VMChangeStateEntry
*e
;
3026 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3027 e
->cb(e
->opaque
, running
, reason
);
3031 static void resume_all_vcpus(void);
3032 static void pause_all_vcpus(void);
3039 vm_state_notify(1, 0);
3040 qemu_rearm_alarm_timer(alarm_timer
);
3045 /* reset/shutdown handler */
3047 typedef struct QEMUResetEntry
{
3048 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3049 QEMUResetHandler
*func
;
3053 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3054 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3055 static int reset_requested
;
3056 static int shutdown_requested
;
3057 static int powerdown_requested
;
3058 static int debug_requested
;
3059 static int vmstop_requested
;
3061 int qemu_shutdown_requested(void)
3063 int r
= shutdown_requested
;
3064 shutdown_requested
= 0;
3068 int qemu_reset_requested(void)
3070 int r
= reset_requested
;
3071 reset_requested
= 0;
3075 int qemu_powerdown_requested(void)
3077 int r
= powerdown_requested
;
3078 powerdown_requested
= 0;
3082 static int qemu_debug_requested(void)
3084 int r
= debug_requested
;
3085 debug_requested
= 0;
3089 static int qemu_vmstop_requested(void)
3091 int r
= vmstop_requested
;
3092 vmstop_requested
= 0;
3096 static void do_vm_stop(int reason
)
3099 cpu_disable_ticks();
3102 vm_state_notify(0, reason
);
3106 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3108 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3111 re
->opaque
= opaque
;
3112 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3115 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3119 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3120 if (re
->func
== func
&& re
->opaque
== opaque
) {
3121 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3128 void qemu_system_reset(void)
3130 QEMUResetEntry
*re
, *nre
;
3132 /* reset all devices */
3133 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3134 re
->func(re
->opaque
);
3138 void qemu_system_reset_request(void)
3141 shutdown_requested
= 1;
3143 reset_requested
= 1;
3145 qemu_notify_event();
3148 void qemu_system_shutdown_request(void)
3150 shutdown_requested
= 1;
3151 qemu_notify_event();
3154 void qemu_system_powerdown_request(void)
3156 powerdown_requested
= 1;
3157 qemu_notify_event();
3160 #ifdef CONFIG_IOTHREAD
3161 static void qemu_system_vmstop_request(int reason
)
3163 vmstop_requested
= reason
;
3164 qemu_notify_event();
3169 static int io_thread_fd
= -1;
3171 static void qemu_event_increment(void)
3173 static const char byte
= 0;
3176 if (io_thread_fd
== -1)
3180 ret
= write(io_thread_fd
, &byte
, sizeof(byte
));
3181 } while (ret
< 0 && errno
== EINTR
);
3183 /* EAGAIN is fine, a read must be pending. */
3184 if (ret
< 0 && errno
!= EAGAIN
) {
3185 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3191 static void qemu_event_read(void *opaque
)
3193 int fd
= (unsigned long)opaque
;
3197 /* Drain the notify pipe */
3199 len
= read(fd
, buffer
, sizeof(buffer
));
3200 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
3203 static int qemu_event_init(void)
3208 err
= qemu_pipe(fds
);
3212 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3216 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3220 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3221 (void *)(unsigned long)fds
[0]);
3223 io_thread_fd
= fds
[1];
3232 HANDLE qemu_event_handle
;
3234 static void dummy_event_handler(void *opaque
)
3238 static int qemu_event_init(void)
3240 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3241 if (!qemu_event_handle
) {
3242 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3245 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3249 static void qemu_event_increment(void)
3251 if (!SetEvent(qemu_event_handle
)) {
3252 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3259 static int cpu_can_run(CPUState
*env
)
3270 #ifndef CONFIG_IOTHREAD
3271 static int qemu_init_main_loop(void)
3273 return qemu_event_init();
3276 void qemu_init_vcpu(void *_env
)
3278 CPUState
*env
= _env
;
3280 env
->nr_cores
= smp_cores
;
3281 env
->nr_threads
= smp_threads
;
3287 int qemu_cpu_self(void *env
)
3292 static void resume_all_vcpus(void)
3296 static void pause_all_vcpus(void)
3300 void qemu_cpu_kick(void *env
)
3305 void qemu_notify_event(void)
3307 CPUState
*env
= cpu_single_env
;
3314 void qemu_mutex_lock_iothread(void) {}
3315 void qemu_mutex_unlock_iothread(void) {}
3317 void vm_stop(int reason
)
3322 #else /* CONFIG_IOTHREAD */
3324 #include "qemu-thread.h"
3326 QemuMutex qemu_global_mutex
;
3327 static QemuMutex qemu_fair_mutex
;
3329 static QemuThread io_thread
;
3331 static QemuThread
*tcg_cpu_thread
;
3332 static QemuCond
*tcg_halt_cond
;
3334 static int qemu_system_ready
;
3336 static QemuCond qemu_cpu_cond
;
3338 static QemuCond qemu_system_cond
;
3339 static QemuCond qemu_pause_cond
;
3341 static void block_io_signals(void);
3342 static void unblock_io_signals(void);
3343 static int tcg_has_work(void);
3345 static int qemu_init_main_loop(void)
3349 ret
= qemu_event_init();
3353 qemu_cond_init(&qemu_pause_cond
);
3354 qemu_mutex_init(&qemu_fair_mutex
);
3355 qemu_mutex_init(&qemu_global_mutex
);
3356 qemu_mutex_lock(&qemu_global_mutex
);
3358 unblock_io_signals();
3359 qemu_thread_self(&io_thread
);
3364 static void qemu_wait_io_event(CPUState
*env
)
3366 while (!tcg_has_work())
3367 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3369 qemu_mutex_unlock(&qemu_global_mutex
);
3372 * Users of qemu_global_mutex can be starved, having no chance
3373 * to acquire it since this path will get to it first.
3374 * So use another lock to provide fairness.
3376 qemu_mutex_lock(&qemu_fair_mutex
);
3377 qemu_mutex_unlock(&qemu_fair_mutex
);
3379 qemu_mutex_lock(&qemu_global_mutex
);
3383 qemu_cond_signal(&qemu_pause_cond
);
3387 static int qemu_cpu_exec(CPUState
*env
);
3389 static void *kvm_cpu_thread_fn(void *arg
)
3391 CPUState
*env
= arg
;
3394 qemu_thread_self(env
->thread
);
3398 /* signal CPU creation */
3399 qemu_mutex_lock(&qemu_global_mutex
);
3401 qemu_cond_signal(&qemu_cpu_cond
);
3403 /* and wait for machine initialization */
3404 while (!qemu_system_ready
)
3405 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3408 if (cpu_can_run(env
))
3410 qemu_wait_io_event(env
);
3416 static void tcg_cpu_exec(void);
3418 static void *tcg_cpu_thread_fn(void *arg
)
3420 CPUState
*env
= arg
;
3423 qemu_thread_self(env
->thread
);
3425 /* signal CPU creation */
3426 qemu_mutex_lock(&qemu_global_mutex
);
3427 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3429 qemu_cond_signal(&qemu_cpu_cond
);
3431 /* and wait for machine initialization */
3432 while (!qemu_system_ready
)
3433 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3437 qemu_wait_io_event(cur_cpu
);
3443 void qemu_cpu_kick(void *_env
)
3445 CPUState
*env
= _env
;
3446 qemu_cond_broadcast(env
->halt_cond
);
3448 qemu_thread_signal(env
->thread
, SIGUSR1
);
3451 int qemu_cpu_self(void *_env
)
3453 CPUState
*env
= _env
;
3456 qemu_thread_self(&this);
3458 return qemu_thread_equal(&this, env
->thread
);
3461 static void cpu_signal(int sig
)
3464 cpu_exit(cpu_single_env
);
3467 static void block_io_signals(void)
3470 struct sigaction sigact
;
3473 sigaddset(&set
, SIGUSR2
);
3474 sigaddset(&set
, SIGIO
);
3475 sigaddset(&set
, SIGALRM
);
3476 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3479 sigaddset(&set
, SIGUSR1
);
3480 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3482 memset(&sigact
, 0, sizeof(sigact
));
3483 sigact
.sa_handler
= cpu_signal
;
3484 sigaction(SIGUSR1
, &sigact
, NULL
);
3487 static void unblock_io_signals(void)
3492 sigaddset(&set
, SIGUSR2
);
3493 sigaddset(&set
, SIGIO
);
3494 sigaddset(&set
, SIGALRM
);
3495 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3498 sigaddset(&set
, SIGUSR1
);
3499 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3502 static void qemu_signal_lock(unsigned int msecs
)
3504 qemu_mutex_lock(&qemu_fair_mutex
);
3506 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3507 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3508 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3511 qemu_mutex_unlock(&qemu_fair_mutex
);
3514 void qemu_mutex_lock_iothread(void)
3516 if (kvm_enabled()) {
3517 qemu_mutex_lock(&qemu_fair_mutex
);
3518 qemu_mutex_lock(&qemu_global_mutex
);
3519 qemu_mutex_unlock(&qemu_fair_mutex
);
3521 qemu_signal_lock(100);
3524 void qemu_mutex_unlock_iothread(void)
3526 qemu_mutex_unlock(&qemu_global_mutex
);
3529 static int all_vcpus_paused(void)
3531 CPUState
*penv
= first_cpu
;
3536 penv
= (CPUState
*)penv
->next_cpu
;
3542 static void pause_all_vcpus(void)
3544 CPUState
*penv
= first_cpu
;
3548 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3549 qemu_cpu_kick(penv
);
3550 penv
= (CPUState
*)penv
->next_cpu
;
3553 while (!all_vcpus_paused()) {
3554 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3557 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3558 penv
= (CPUState
*)penv
->next_cpu
;
3563 static void resume_all_vcpus(void)
3565 CPUState
*penv
= first_cpu
;
3570 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3571 qemu_cpu_kick(penv
);
3572 penv
= (CPUState
*)penv
->next_cpu
;
3576 static void tcg_init_vcpu(void *_env
)
3578 CPUState
*env
= _env
;
3579 /* share a single thread for all cpus with TCG */
3580 if (!tcg_cpu_thread
) {
3581 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3582 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3583 qemu_cond_init(env
->halt_cond
);
3584 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3585 while (env
->created
== 0)
3586 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3587 tcg_cpu_thread
= env
->thread
;
3588 tcg_halt_cond
= env
->halt_cond
;
3590 env
->thread
= tcg_cpu_thread
;
3591 env
->halt_cond
= tcg_halt_cond
;
3595 static void kvm_start_vcpu(CPUState
*env
)
3597 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3598 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3599 qemu_cond_init(env
->halt_cond
);
3600 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3601 while (env
->created
== 0)
3602 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3605 void qemu_init_vcpu(void *_env
)
3607 CPUState
*env
= _env
;
3609 env
->nr_cores
= smp_cores
;
3610 env
->nr_threads
= smp_threads
;
3612 kvm_start_vcpu(env
);
3617 void qemu_notify_event(void)
3619 qemu_event_increment();
3622 void vm_stop(int reason
)
3625 qemu_thread_self(&me
);
3627 if (!qemu_thread_equal(&me
, &io_thread
)) {
3628 qemu_system_vmstop_request(reason
);
3630 * FIXME: should not return to device code in case
3631 * vm_stop() has been requested.
3633 if (cpu_single_env
) {
3634 cpu_exit(cpu_single_env
);
3635 cpu_single_env
->stop
= 1;
3646 static void host_main_loop_wait(int *timeout
)
3652 /* XXX: need to suppress polling by better using win32 events */
3654 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3655 ret
|= pe
->func(pe
->opaque
);
3659 WaitObjects
*w
= &wait_objects
;
3661 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3662 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3663 if (w
->func
[ret
- WAIT_OBJECT_0
])
3664 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3666 /* Check for additional signaled events */
3667 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3669 /* Check if event is signaled */
3670 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3671 if(ret2
== WAIT_OBJECT_0
) {
3673 w
->func
[i
](w
->opaque
[i
]);
3674 } else if (ret2
== WAIT_TIMEOUT
) {
3676 err
= GetLastError();
3677 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3680 } else if (ret
== WAIT_TIMEOUT
) {
3682 err
= GetLastError();
3683 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3690 static void host_main_loop_wait(int *timeout
)
3695 void main_loop_wait(int timeout
)
3697 IOHandlerRecord
*ioh
;
3698 fd_set rfds
, wfds
, xfds
;
3702 qemu_bh_update_timeout(&timeout
);
3704 host_main_loop_wait(&timeout
);
3706 /* poll any events */
3707 /* XXX: separate device handlers from system ones */
3712 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3716 (!ioh
->fd_read_poll
||
3717 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3718 FD_SET(ioh
->fd
, &rfds
);
3722 if (ioh
->fd_write
) {
3723 FD_SET(ioh
->fd
, &wfds
);
3729 tv
.tv_sec
= timeout
/ 1000;
3730 tv
.tv_usec
= (timeout
% 1000) * 1000;
3732 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3734 qemu_mutex_unlock_iothread();
3735 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3736 qemu_mutex_lock_iothread();
3738 IOHandlerRecord
**pioh
;
3740 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3741 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3742 ioh
->fd_read(ioh
->opaque
);
3744 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3745 ioh
->fd_write(ioh
->opaque
);
3749 /* remove deleted IO handlers */
3750 pioh
= &first_io_handler
;
3761 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3763 /* rearm timer, if not periodic */
3764 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3765 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3766 qemu_rearm_alarm_timer(alarm_timer
);
3769 /* vm time timers */
3771 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3772 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3773 qemu_get_clock(vm_clock
));
3776 /* real time timers */
3777 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3778 qemu_get_clock(rt_clock
));
3780 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3781 qemu_get_clock(host_clock
));
3783 /* Check bottom-halves last in case any of the earlier events triggered
3789 static int qemu_cpu_exec(CPUState
*env
)
3792 #ifdef CONFIG_PROFILER
3796 #ifdef CONFIG_PROFILER
3797 ti
= profile_getclock();
3802 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3803 env
->icount_decr
.u16
.low
= 0;
3804 env
->icount_extra
= 0;
3805 count
= qemu_next_deadline();
3806 count
= (count
+ (1 << icount_time_shift
) - 1)
3807 >> icount_time_shift
;
3808 qemu_icount
+= count
;
3809 decr
= (count
> 0xffff) ? 0xffff : count
;
3811 env
->icount_decr
.u16
.low
= decr
;
3812 env
->icount_extra
= count
;
3814 ret
= cpu_exec(env
);
3815 #ifdef CONFIG_PROFILER
3816 qemu_time
+= profile_getclock() - ti
;
3819 /* Fold pending instructions back into the
3820 instruction counter, and clear the interrupt flag. */
3821 qemu_icount
-= (env
->icount_decr
.u16
.low
3822 + env
->icount_extra
);
3823 env
->icount_decr
.u32
= 0;
3824 env
->icount_extra
= 0;
3829 static void tcg_cpu_exec(void)
3833 if (next_cpu
== NULL
)
3834 next_cpu
= first_cpu
;
3835 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3836 CPUState
*env
= cur_cpu
= next_cpu
;
3838 if (timer_alarm_pending
) {
3839 timer_alarm_pending
= 0;
3842 if (cpu_can_run(env
))
3843 ret
= qemu_cpu_exec(env
);
3847 if (ret
== EXCP_DEBUG
) {
3848 gdb_set_stop_cpu(env
);
3849 debug_requested
= 1;
3855 static int cpu_has_work(CPUState
*env
)
3863 if (qemu_cpu_has_work(env
))
3868 static int tcg_has_work(void)
3872 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3873 if (cpu_has_work(env
))
3878 static int qemu_calculate_timeout(void)
3880 #ifndef CONFIG_IOTHREAD
3885 else if (tcg_has_work())
3887 else if (!use_icount
)
3890 /* XXX: use timeout computed from timers */
3893 /* Advance virtual time to the next event. */
3894 if (use_icount
== 1) {
3895 /* When not using an adaptive execution frequency
3896 we tend to get badly out of sync with real time,
3897 so just delay for a reasonable amount of time. */
3900 delta
= cpu_get_icount() - cpu_get_clock();
3903 /* If virtual time is ahead of real time then just
3905 timeout
= (delta
/ 1000000) + 1;
3907 /* Wait for either IO to occur or the next
3909 add
= qemu_next_deadline();
3910 /* We advance the timer before checking for IO.
3911 Limit the amount we advance so that early IO
3912 activity won't get the guest too far ahead. */
3916 add
= (add
+ (1 << icount_time_shift
) - 1)
3917 >> icount_time_shift
;
3919 timeout
= delta
/ 1000000;
3926 #else /* CONFIG_IOTHREAD */
3931 static int vm_can_run(void)
3933 if (powerdown_requested
)
3935 if (reset_requested
)
3937 if (shutdown_requested
)
3939 if (debug_requested
)
3944 qemu_irq qemu_system_powerdown
;
3946 static void main_loop(void)
3950 #ifdef CONFIG_IOTHREAD
3951 qemu_system_ready
= 1;
3952 qemu_cond_broadcast(&qemu_system_cond
);
3957 #ifdef CONFIG_PROFILER
3960 #ifndef CONFIG_IOTHREAD
3963 #ifdef CONFIG_PROFILER
3964 ti
= profile_getclock();
3966 main_loop_wait(qemu_calculate_timeout());
3967 #ifdef CONFIG_PROFILER
3968 dev_time
+= profile_getclock() - ti
;
3970 } while (vm_can_run());
3972 if (qemu_debug_requested()) {
3973 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
3974 vm_stop(EXCP_DEBUG
);
3976 if (qemu_shutdown_requested()) {
3977 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3984 if (qemu_reset_requested()) {
3985 monitor_protocol_event(QEVENT_RESET
, NULL
);
3987 qemu_system_reset();
3990 if (qemu_powerdown_requested()) {
3991 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
3992 qemu_irq_raise(qemu_system_powerdown
);
3994 if ((r
= qemu_vmstop_requested())) {
3995 monitor_protocol_event(QEVENT_STOP
, NULL
);
4002 static void version(void)
4004 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4007 static void help(int exitcode
)
4009 const char *options_help
=
4010 #define DEF(option, opt_arg, opt_enum, opt_help) \
4012 #define DEFHEADING(text) stringify(text) "\n"
4013 #include "qemu-options.h"
4019 printf("usage: %s [options] [disk_image]\n"
4021 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4024 "During emulation, the following keys are useful:\n"
4025 "ctrl-alt-f toggle full screen\n"
4026 "ctrl-alt-n switch to virtual console 'n'\n"
4027 "ctrl-alt toggle mouse and keyboard grab\n"
4029 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4035 #define HAS_ARG 0x0001
4038 #define DEF(option, opt_arg, opt_enum, opt_help) \
4040 #define DEFHEADING(text)
4041 #include "qemu-options.h"
4047 typedef struct QEMUOption
{
4053 static const QEMUOption qemu_options
[] = {
4054 { "h", 0, QEMU_OPTION_h
},
4055 #define DEF(option, opt_arg, opt_enum, opt_help) \
4056 { option, opt_arg, opt_enum },
4057 #define DEFHEADING(text)
4058 #include "qemu-options.h"
4066 struct soundhw soundhw
[] = {
4067 #ifdef HAS_AUDIO_CHOICE
4068 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4074 { .init_isa
= pcspk_audio_init
}
4081 "Creative Sound Blaster 16",
4084 { .init_isa
= SB16_init
}
4088 #ifdef CONFIG_CS4231A
4094 { .init_isa
= cs4231a_init
}
4102 "Yamaha YMF262 (OPL3)",
4104 "Yamaha YM3812 (OPL2)",
4108 { .init_isa
= Adlib_init
}
4115 "Gravis Ultrasound GF1",
4118 { .init_isa
= GUS_init
}
4125 "Intel 82801AA AC97 Audio",
4128 { .init_pci
= ac97_init
}
4132 #ifdef CONFIG_ES1370
4135 "ENSONIQ AudioPCI ES1370",
4138 { .init_pci
= es1370_init
}
4142 #endif /* HAS_AUDIO_CHOICE */
4144 { NULL
, NULL
, 0, 0, { NULL
} }
4147 static void select_soundhw (const char *optarg
)
4151 if (*optarg
== '?') {
4154 printf ("Valid sound card names (comma separated):\n");
4155 for (c
= soundhw
; c
->name
; ++c
) {
4156 printf ("%-11s %s\n", c
->name
, c
->descr
);
4158 printf ("\n-soundhw all will enable all of the above\n");
4159 exit (*optarg
!= '?');
4167 if (!strcmp (optarg
, "all")) {
4168 for (c
= soundhw
; c
->name
; ++c
) {
4176 e
= strchr (p
, ',');
4177 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4179 for (c
= soundhw
; c
->name
; ++c
) {
4180 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4189 "Unknown sound card name (too big to show)\n");
4192 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4197 p
+= l
+ (e
!= NULL
);
4201 goto show_valid_cards
;
4206 static void select_vgahw (const char *p
)
4211 vga_interface_type
= VGA_NONE
;
4212 if (strstart(p
, "std", &opts
)) {
4213 vga_interface_type
= VGA_STD
;
4214 } else if (strstart(p
, "cirrus", &opts
)) {
4215 vga_interface_type
= VGA_CIRRUS
;
4216 } else if (strstart(p
, "vmware", &opts
)) {
4217 vga_interface_type
= VGA_VMWARE
;
4218 } else if (strstart(p
, "xenfb", &opts
)) {
4219 vga_interface_type
= VGA_XENFB
;
4220 } else if (!strstart(p
, "none", &opts
)) {
4222 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4226 const char *nextopt
;
4228 if (strstart(opts
, ",retrace=", &nextopt
)) {
4230 if (strstart(opts
, "dumb", &nextopt
))
4231 vga_retrace_method
= VGA_RETRACE_DUMB
;
4232 else if (strstart(opts
, "precise", &nextopt
))
4233 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4234 else goto invalid_vga
;
4235 } else goto invalid_vga
;
4241 static int balloon_parse(const char *arg
)
4245 if (strcmp(arg
, "none") == 0) {
4249 if (!strncmp(arg
, "virtio", 6)) {
4250 if (arg
[6] == ',') {
4251 /* have params -> parse them */
4252 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4256 /* create empty opts */
4257 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4259 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4268 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4270 exit(STATUS_CONTROL_C_EXIT
);
4275 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4279 if(strlen(str
) != 36)
4282 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4283 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4284 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4290 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4298 static void termsig_handler(int signal
)
4300 qemu_system_shutdown_request();
4303 static void sigchld_handler(int signal
)
4305 waitpid(-1, NULL
, WNOHANG
);
4308 static void sighandler_setup(void)
4310 struct sigaction act
;
4312 memset(&act
, 0, sizeof(act
));
4313 act
.sa_handler
= termsig_handler
;
4314 sigaction(SIGINT
, &act
, NULL
);
4315 sigaction(SIGHUP
, &act
, NULL
);
4316 sigaction(SIGTERM
, &act
, NULL
);
4318 act
.sa_handler
= sigchld_handler
;
4319 act
.sa_flags
= SA_NOCLDSTOP
;
4320 sigaction(SIGCHLD
, &act
, NULL
);
4326 /* Look for support files in the same directory as the executable. */
4327 static char *find_datadir(const char *argv0
)
4333 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4340 while (p
!= buf
&& *p
!= '\\')
4343 if (access(buf
, R_OK
) == 0) {
4344 return qemu_strdup(buf
);
4350 /* Find a likely location for support files using the location of the binary.
4351 For installed binaries this will be "$bindir/../share/qemu". When
4352 running from the build tree this will be "$bindir/../pc-bios". */
4353 #define SHARE_SUFFIX "/share/qemu"
4354 #define BUILD_SUFFIX "/pc-bios"
4355 static char *find_datadir(const char *argv0
)
4363 #if defined(__linux__)
4366 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4372 #elif defined(__FreeBSD__)
4375 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4382 /* If we don't have any way of figuring out the actual executable
4383 location then try argv[0]. */
4385 p
= realpath(argv0
, buf
);
4393 max_len
= strlen(dir
) +
4394 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4395 res
= qemu_mallocz(max_len
);
4396 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4397 if (access(res
, R_OK
)) {
4398 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4399 if (access(res
, R_OK
)) {
4411 char *qemu_find_file(int type
, const char *name
)
4417 /* If name contains path separators then try it as a straight path. */
4418 if ((strchr(name
, '/') || strchr(name
, '\\'))
4419 && access(name
, R_OK
) == 0) {
4420 return qemu_strdup(name
);
4423 case QEMU_FILE_TYPE_BIOS
:
4426 case QEMU_FILE_TYPE_KEYMAP
:
4427 subdir
= "keymaps/";
4432 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4433 buf
= qemu_mallocz(len
);
4434 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4435 if (access(buf
, R_OK
)) {
4442 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4444 return qdev_device_help(opts
);
4447 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4451 dev
= qdev_device_add(opts
);
4457 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4459 CharDriverState
*chr
;
4461 chr
= qemu_chr_open_opts(opts
, NULL
);
4467 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4469 CharDriverState
*chr
;
4470 const char *chardev
;
4474 mode
= qemu_opt_get(opts
, "mode");
4478 if (strcmp(mode
, "readline") == 0) {
4479 flags
= MONITOR_USE_READLINE
;
4480 } else if (strcmp(mode
, "control") == 0) {
4481 flags
= MONITOR_USE_CONTROL
;
4483 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4487 if (qemu_opt_get_bool(opts
, "default", 0))
4488 flags
|= MONITOR_IS_DEFAULT
;
4490 chardev
= qemu_opt_get(opts
, "chardev");
4491 chr
= qemu_chr_find(chardev
);
4493 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4497 monitor_init(chr
, flags
);
4501 static void monitor_parse(const char *optarg
, const char *mode
)
4503 static int monitor_device_index
= 0;
4509 if (strstart(optarg
, "chardev:", &p
)) {
4510 snprintf(label
, sizeof(label
), "%s", p
);
4512 if (monitor_device_index
) {
4513 snprintf(label
, sizeof(label
), "monitor%d",
4514 monitor_device_index
);
4516 snprintf(label
, sizeof(label
), "monitor");
4519 opts
= qemu_chr_parse_compat(label
, optarg
);
4521 fprintf(stderr
, "parse error: %s\n", optarg
);
4526 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4528 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4531 qemu_opt_set(opts
, "mode", mode
);
4532 qemu_opt_set(opts
, "chardev", label
);
4534 qemu_opt_set(opts
, "default", "on");
4535 monitor_device_index
++;
4538 struct device_config
{
4540 DEV_USB
, /* -usbdevice */
4542 DEV_SERIAL
, /* -serial */
4543 DEV_PARALLEL
, /* -parallel */
4544 DEV_VIRTCON
, /* -virtioconsole */
4545 DEV_DEBUGCON
, /* -debugcon */
4547 const char *cmdline
;
4548 QTAILQ_ENTRY(device_config
) next
;
4550 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4552 static void add_device_config(int type
, const char *cmdline
)
4554 struct device_config
*conf
;
4556 conf
= qemu_mallocz(sizeof(*conf
));
4558 conf
->cmdline
= cmdline
;
4559 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4562 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4564 struct device_config
*conf
;
4567 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4568 if (conf
->type
!= type
)
4570 rc
= func(conf
->cmdline
);
4577 static int serial_parse(const char *devname
)
4579 static int index
= 0;
4582 if (strcmp(devname
, "none") == 0)
4584 if (index
== MAX_SERIAL_PORTS
) {
4585 fprintf(stderr
, "qemu: too many serial ports\n");
4588 snprintf(label
, sizeof(label
), "serial%d", index
);
4589 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4590 if (!serial_hds
[index
]) {
4591 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4592 devname
, strerror(errno
));
4599 static int parallel_parse(const char *devname
)
4601 static int index
= 0;
4604 if (strcmp(devname
, "none") == 0)
4606 if (index
== MAX_PARALLEL_PORTS
) {
4607 fprintf(stderr
, "qemu: too many parallel ports\n");
4610 snprintf(label
, sizeof(label
), "parallel%d", index
);
4611 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4612 if (!parallel_hds
[index
]) {
4613 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4614 devname
, strerror(errno
));
4621 static int virtcon_parse(const char *devname
)
4623 static int index
= 0;
4625 QemuOpts
*bus_opts
, *dev_opts
;
4627 if (strcmp(devname
, "none") == 0)
4629 if (index
== MAX_VIRTIO_CONSOLES
) {
4630 fprintf(stderr
, "qemu: too many virtio consoles\n");
4634 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4635 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4637 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4638 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4640 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4641 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4642 if (!virtcon_hds
[index
]) {
4643 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4644 devname
, strerror(errno
));
4647 qemu_opt_set(dev_opts
, "chardev", label
);
4653 static int debugcon_parse(const char *devname
)
4657 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4660 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4662 fprintf(stderr
, "qemu: already have a debugcon device\n");
4665 qemu_opt_set(opts
, "driver", "isa-debugcon");
4666 qemu_opt_set(opts
, "chardev", "debugcon");
4670 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4671 const char **poptarg
, int *poptind
)
4673 const QEMUOption
*popt
;
4674 int optind
= *poptind
;
4675 char *r
= argv
[optind
];
4679 /* Treat --foo the same as -foo. */
4682 popt
= qemu_options
;
4685 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4689 if (!strcmp(popt
->name
, r
+ 1))
4693 if (popt
->flags
& HAS_ARG
) {
4694 if (optind
>= argc
) {
4695 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4699 optarg
= argv
[optind
++];
4710 int main(int argc
, char **argv
, char **envp
)
4712 const char *gdbstub_dev
= NULL
;
4713 uint32_t boot_devices_bitmap
= 0;
4715 int snapshot
, linux_boot
, net_boot
;
4716 const char *initrd_filename
;
4717 const char *kernel_filename
, *kernel_cmdline
;
4718 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4720 DisplayChangeListener
*dcl
;
4721 int cyls
, heads
, secs
, translation
;
4722 QemuOpts
*hda_opts
= NULL
, *opts
;
4725 const char *loadvm
= NULL
;
4726 QEMUMachine
*machine
;
4727 const char *cpu_model
;
4732 const char *pid_file
= NULL
;
4733 const char *incoming
= NULL
;
4736 struct passwd
*pwd
= NULL
;
4737 const char *chroot_dir
= NULL
;
4738 const char *run_as
= NULL
;
4741 int show_vnc_port
= 0;
4746 qemu_errors_to_file(stderr
);
4747 qemu_cache_utils_init(envp
);
4749 QLIST_INIT (&vm_change_state_head
);
4752 struct sigaction act
;
4753 sigfillset(&act
.sa_mask
);
4755 act
.sa_handler
= SIG_IGN
;
4756 sigaction(SIGPIPE
, &act
, NULL
);
4759 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4760 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4761 QEMU to run on a single CPU */
4766 h
= GetCurrentProcess();
4767 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4768 for(i
= 0; i
< 32; i
++) {
4769 if (mask
& (1 << i
))
4774 SetProcessAffinityMask(h
, mask
);
4780 module_call_init(MODULE_INIT_MACHINE
);
4781 machine
= find_default_machine();
4783 initrd_filename
= NULL
;
4786 kernel_filename
= NULL
;
4787 kernel_cmdline
= "";
4788 cyls
= heads
= secs
= 0;
4789 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4791 for (i
= 0; i
< MAX_NODES
; i
++) {
4793 node_cpumask
[i
] = 0;
4802 /* first pass of option parsing */
4804 while (optind
< argc
) {
4805 if (argv
[optind
][0] != '-') {
4810 const QEMUOption
*popt
;
4812 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4813 switch (popt
->index
) {
4814 case QEMU_OPTION_nodefconfig
:
4823 fp
= fopen(CONFIG_QEMU_CONFDIR
"/qemu.conf", "r");
4825 if (qemu_config_parse(fp
) != 0) {
4831 fp
= fopen(CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", "r");
4833 if (qemu_config_parse(fp
) != 0) {
4840 /* second pass of option parsing */
4845 if (argv
[optind
][0] != '-') {
4846 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4848 const QEMUOption
*popt
;
4850 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4851 switch(popt
->index
) {
4853 machine
= find_machine(optarg
);
4856 printf("Supported machines are:\n");
4857 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4859 printf("%-10s %s (alias of %s)\n",
4860 m
->alias
, m
->desc
, m
->name
);
4861 printf("%-10s %s%s\n",
4863 m
->is_default
? " (default)" : "");
4865 exit(*optarg
!= '?');
4868 case QEMU_OPTION_cpu
:
4869 /* hw initialization will check this */
4870 if (*optarg
== '?') {
4871 /* XXX: implement xxx_cpu_list for targets that still miss it */
4872 #if defined(cpu_list)
4873 cpu_list(stdout
, &fprintf
);
4880 case QEMU_OPTION_initrd
:
4881 initrd_filename
= optarg
;
4883 case QEMU_OPTION_hda
:
4885 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4887 hda_opts
= drive_add(optarg
, HD_ALIAS
4888 ",cyls=%d,heads=%d,secs=%d%s",
4889 0, cyls
, heads
, secs
,
4890 translation
== BIOS_ATA_TRANSLATION_LBA
?
4892 translation
== BIOS_ATA_TRANSLATION_NONE
?
4893 ",trans=none" : "");
4895 case QEMU_OPTION_hdb
:
4896 case QEMU_OPTION_hdc
:
4897 case QEMU_OPTION_hdd
:
4898 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4900 case QEMU_OPTION_drive
:
4901 drive_add(NULL
, "%s", optarg
);
4903 case QEMU_OPTION_set
:
4904 if (qemu_set_option(optarg
) != 0)
4907 case QEMU_OPTION_global
:
4908 if (qemu_global_option(optarg
) != 0)
4911 case QEMU_OPTION_mtdblock
:
4912 drive_add(optarg
, MTD_ALIAS
);
4914 case QEMU_OPTION_sd
:
4915 drive_add(optarg
, SD_ALIAS
);
4917 case QEMU_OPTION_pflash
:
4918 drive_add(optarg
, PFLASH_ALIAS
);
4920 case QEMU_OPTION_snapshot
:
4923 case QEMU_OPTION_hdachs
:
4927 cyls
= strtol(p
, (char **)&p
, 0);
4928 if (cyls
< 1 || cyls
> 16383)
4933 heads
= strtol(p
, (char **)&p
, 0);
4934 if (heads
< 1 || heads
> 16)
4939 secs
= strtol(p
, (char **)&p
, 0);
4940 if (secs
< 1 || secs
> 63)
4944 if (!strcmp(p
, "none"))
4945 translation
= BIOS_ATA_TRANSLATION_NONE
;
4946 else if (!strcmp(p
, "lba"))
4947 translation
= BIOS_ATA_TRANSLATION_LBA
;
4948 else if (!strcmp(p
, "auto"))
4949 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4952 } else if (*p
!= '\0') {
4954 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4957 if (hda_opts
!= NULL
) {
4959 snprintf(num
, sizeof(num
), "%d", cyls
);
4960 qemu_opt_set(hda_opts
, "cyls", num
);
4961 snprintf(num
, sizeof(num
), "%d", heads
);
4962 qemu_opt_set(hda_opts
, "heads", num
);
4963 snprintf(num
, sizeof(num
), "%d", secs
);
4964 qemu_opt_set(hda_opts
, "secs", num
);
4965 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4966 qemu_opt_set(hda_opts
, "trans", "lba");
4967 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4968 qemu_opt_set(hda_opts
, "trans", "none");
4972 case QEMU_OPTION_numa
:
4973 if (nb_numa_nodes
>= MAX_NODES
) {
4974 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4979 case QEMU_OPTION_nographic
:
4980 display_type
= DT_NOGRAPHIC
;
4982 #ifdef CONFIG_CURSES
4983 case QEMU_OPTION_curses
:
4984 display_type
= DT_CURSES
;
4987 case QEMU_OPTION_portrait
:
4990 case QEMU_OPTION_kernel
:
4991 kernel_filename
= optarg
;
4993 case QEMU_OPTION_append
:
4994 kernel_cmdline
= optarg
;
4996 case QEMU_OPTION_cdrom
:
4997 drive_add(optarg
, CDROM_ALIAS
);
4999 case QEMU_OPTION_boot
:
5001 static const char * const params
[] = {
5002 "order", "once", "menu", NULL
5004 char buf
[sizeof(boot_devices
)];
5005 char *standard_boot_devices
;
5008 if (!strchr(optarg
, '=')) {
5010 pstrcpy(buf
, sizeof(buf
), optarg
);
5011 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5013 "qemu: unknown boot parameter '%s' in '%s'\n",
5019 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5020 boot_devices_bitmap
= parse_bootdevices(buf
);
5021 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5024 if (get_param_value(buf
, sizeof(buf
),
5026 boot_devices_bitmap
|= parse_bootdevices(buf
);
5027 standard_boot_devices
= qemu_strdup(boot_devices
);
5028 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5029 qemu_register_reset(restore_boot_devices
,
5030 standard_boot_devices
);
5032 if (get_param_value(buf
, sizeof(buf
),
5034 if (!strcmp(buf
, "on")) {
5036 } else if (!strcmp(buf
, "off")) {
5040 "qemu: invalid option value '%s'\n",
5048 case QEMU_OPTION_fda
:
5049 case QEMU_OPTION_fdb
:
5050 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5053 case QEMU_OPTION_no_fd_bootchk
:
5057 case QEMU_OPTION_netdev
:
5058 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5062 case QEMU_OPTION_net
:
5063 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5068 case QEMU_OPTION_tftp
:
5069 legacy_tftp_prefix
= optarg
;
5071 case QEMU_OPTION_bootp
:
5072 legacy_bootp_filename
= optarg
;
5075 case QEMU_OPTION_smb
:
5076 if (net_slirp_smb(optarg
) < 0)
5080 case QEMU_OPTION_redir
:
5081 if (net_slirp_redir(optarg
) < 0)
5085 case QEMU_OPTION_bt
:
5086 add_device_config(DEV_BT
, optarg
);
5089 case QEMU_OPTION_audio_help
:
5093 case QEMU_OPTION_soundhw
:
5094 select_soundhw (optarg
);
5100 case QEMU_OPTION_version
:
5104 case QEMU_OPTION_m
: {
5108 value
= strtoul(optarg
, &ptr
, 10);
5110 case 0: case 'M': case 'm':
5117 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5121 /* On 32-bit hosts, QEMU is limited by virtual address space */
5122 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5123 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5126 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5127 fprintf(stderr
, "qemu: ram size too large\n");
5136 const CPULogItem
*item
;
5138 mask
= cpu_str_to_log_mask(optarg
);
5140 printf("Log items (comma separated):\n");
5141 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5142 printf("%-10s %s\n", item
->name
, item
->help
);
5150 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5152 case QEMU_OPTION_gdb
:
5153 gdbstub_dev
= optarg
;
5158 case QEMU_OPTION_bios
:
5161 case QEMU_OPTION_singlestep
:
5168 keyboard_layout
= optarg
;
5170 case QEMU_OPTION_localtime
:
5173 case QEMU_OPTION_vga
:
5174 select_vgahw (optarg
);
5176 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5182 w
= strtol(p
, (char **)&p
, 10);
5185 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5191 h
= strtol(p
, (char **)&p
, 10);
5196 depth
= strtol(p
, (char **)&p
, 10);
5197 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5198 depth
!= 24 && depth
!= 32)
5200 } else if (*p
== '\0') {
5201 depth
= graphic_depth
;
5208 graphic_depth
= depth
;
5212 case QEMU_OPTION_echr
:
5215 term_escape_char
= strtol(optarg
, &r
, 0);
5217 printf("Bad argument to echr\n");
5220 case QEMU_OPTION_monitor
:
5221 monitor_parse(optarg
, "readline");
5222 default_monitor
= 0;
5224 case QEMU_OPTION_qmp
:
5225 monitor_parse(optarg
, "control");
5226 default_monitor
= 0;
5228 case QEMU_OPTION_mon
:
5229 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5231 fprintf(stderr
, "parse error: %s\n", optarg
);
5234 default_monitor
= 0;
5236 case QEMU_OPTION_chardev
:
5237 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5239 fprintf(stderr
, "parse error: %s\n", optarg
);
5243 case QEMU_OPTION_serial
:
5244 add_device_config(DEV_SERIAL
, optarg
);
5247 case QEMU_OPTION_watchdog
:
5250 "qemu: only one watchdog option may be given\n");
5255 case QEMU_OPTION_watchdog_action
:
5256 if (select_watchdog_action(optarg
) == -1) {
5257 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5261 case QEMU_OPTION_virtiocon
:
5262 add_device_config(DEV_VIRTCON
, optarg
);
5263 default_virtcon
= 0;
5265 case QEMU_OPTION_parallel
:
5266 add_device_config(DEV_PARALLEL
, optarg
);
5267 default_parallel
= 0;
5269 case QEMU_OPTION_debugcon
:
5270 add_device_config(DEV_DEBUGCON
, optarg
);
5272 case QEMU_OPTION_loadvm
:
5275 case QEMU_OPTION_full_screen
:
5279 case QEMU_OPTION_no_frame
:
5282 case QEMU_OPTION_alt_grab
:
5285 case QEMU_OPTION_ctrl_grab
:
5288 case QEMU_OPTION_no_quit
:
5291 case QEMU_OPTION_sdl
:
5292 display_type
= DT_SDL
;
5295 case QEMU_OPTION_pidfile
:
5299 case QEMU_OPTION_win2k_hack
:
5300 win2k_install_hack
= 1;
5302 case QEMU_OPTION_rtc_td_hack
:
5305 case QEMU_OPTION_acpitable
:
5306 if(acpi_table_add(optarg
) < 0) {
5307 fprintf(stderr
, "Wrong acpi table provided\n");
5311 case QEMU_OPTION_smbios
:
5312 if(smbios_entry_add(optarg
) < 0) {
5313 fprintf(stderr
, "Wrong smbios provided\n");
5319 case QEMU_OPTION_enable_kvm
:
5323 case QEMU_OPTION_usb
:
5326 case QEMU_OPTION_usbdevice
:
5328 add_device_config(DEV_USB
, optarg
);
5330 case QEMU_OPTION_device
:
5331 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5335 case QEMU_OPTION_smp
:
5338 fprintf(stderr
, "Invalid number of CPUs\n");
5341 if (max_cpus
< smp_cpus
) {
5342 fprintf(stderr
, "maxcpus must be equal to or greater than "
5346 if (max_cpus
> 255) {
5347 fprintf(stderr
, "Unsupported number of maxcpus\n");
5351 case QEMU_OPTION_vnc
:
5352 display_type
= DT_VNC
;
5353 vnc_display
= optarg
;
5356 case QEMU_OPTION_no_acpi
:
5359 case QEMU_OPTION_no_hpet
:
5362 case QEMU_OPTION_balloon
:
5363 if (balloon_parse(optarg
) < 0) {
5364 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5369 case QEMU_OPTION_no_reboot
:
5372 case QEMU_OPTION_no_shutdown
:
5375 case QEMU_OPTION_show_cursor
:
5378 case QEMU_OPTION_uuid
:
5379 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5380 fprintf(stderr
, "Fail to parse UUID string."
5381 " Wrong format.\n");
5386 case QEMU_OPTION_daemonize
:
5390 case QEMU_OPTION_option_rom
:
5391 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5392 fprintf(stderr
, "Too many option ROMs\n");
5395 option_rom
[nb_option_roms
] = optarg
;
5398 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5399 case QEMU_OPTION_semihosting
:
5400 semihosting_enabled
= 1;
5403 case QEMU_OPTION_name
:
5404 qemu_name
= qemu_strdup(optarg
);
5406 char *p
= strchr(qemu_name
, ',');
5409 if (strncmp(p
, "process=", 8)) {
5410 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5418 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5419 case QEMU_OPTION_prom_env
:
5420 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5421 fprintf(stderr
, "Too many prom variables\n");
5424 prom_envs
[nb_prom_envs
] = optarg
;
5429 case QEMU_OPTION_old_param
:
5433 case QEMU_OPTION_clock
:
5434 configure_alarms(optarg
);
5436 case QEMU_OPTION_startdate
:
5437 configure_rtc_date_offset(optarg
, 1);
5439 case QEMU_OPTION_rtc
:
5440 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5442 fprintf(stderr
, "parse error: %s\n", optarg
);
5445 configure_rtc(opts
);
5447 case QEMU_OPTION_tb_size
:
5448 tb_size
= strtol(optarg
, NULL
, 0);
5452 case QEMU_OPTION_icount
:
5454 if (strcmp(optarg
, "auto") == 0) {
5455 icount_time_shift
= -1;
5457 icount_time_shift
= strtol(optarg
, NULL
, 0);
5460 case QEMU_OPTION_incoming
:
5463 case QEMU_OPTION_nodefaults
:
5465 default_parallel
= 0;
5466 default_virtcon
= 0;
5467 default_monitor
= 0;
5475 case QEMU_OPTION_chroot
:
5476 chroot_dir
= optarg
;
5478 case QEMU_OPTION_runas
:
5483 case QEMU_OPTION_xen_domid
:
5484 xen_domid
= atoi(optarg
);
5486 case QEMU_OPTION_xen_create
:
5487 xen_mode
= XEN_CREATE
;
5489 case QEMU_OPTION_xen_attach
:
5490 xen_mode
= XEN_ATTACH
;
5493 case QEMU_OPTION_readconfig
:
5496 fp
= fopen(optarg
, "r");
5498 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5501 if (qemu_config_parse(fp
) != 0) {
5507 case QEMU_OPTION_writeconfig
:
5510 if (strcmp(optarg
, "-") == 0) {
5513 fp
= fopen(optarg
, "w");
5515 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5519 qemu_config_write(fp
);
5527 /* If no data_dir is specified then try to find it relative to the
5530 data_dir
= find_datadir(argv
[0]);
5532 /* If all else fails use the install patch specified when building. */
5534 data_dir
= CONFIG_QEMU_SHAREDIR
;
5538 * Default to max_cpus = smp_cpus, in case the user doesn't
5539 * specify a max_cpus value.
5542 max_cpus
= smp_cpus
;
5544 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5545 if (smp_cpus
> machine
->max_cpus
) {
5546 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5547 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5552 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5553 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5555 if (machine
->no_serial
) {
5558 if (machine
->no_parallel
) {
5559 default_parallel
= 0;
5561 if (!machine
->use_virtcon
) {
5562 default_virtcon
= 0;
5564 if (machine
->no_vga
) {
5567 if (machine
->no_floppy
) {
5570 if (machine
->no_cdrom
) {
5573 if (machine
->no_sdcard
) {
5577 if (display_type
== DT_NOGRAPHIC
) {
5578 if (default_parallel
)
5579 add_device_config(DEV_PARALLEL
, "null");
5580 if (default_serial
&& default_monitor
) {
5581 add_device_config(DEV_SERIAL
, "mon:stdio");
5582 } else if (default_virtcon
&& default_monitor
) {
5583 add_device_config(DEV_VIRTCON
, "mon:stdio");
5586 add_device_config(DEV_SERIAL
, "stdio");
5587 if (default_virtcon
)
5588 add_device_config(DEV_VIRTCON
, "stdio");
5589 if (default_monitor
)
5590 monitor_parse("stdio", "readline");
5594 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5595 if (default_parallel
)
5596 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5597 if (default_monitor
)
5598 monitor_parse("vc:80Cx24C", "readline");
5599 if (default_virtcon
)
5600 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5603 vga_interface_type
= VGA_CIRRUS
;
5605 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5612 if (pipe(fds
) == -1)
5623 len
= read(fds
[0], &status
, 1);
5624 if (len
== -1 && (errno
== EINTR
))
5629 else if (status
== 1) {
5630 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5638 qemu_set_cloexec(fds
[1]);
5650 signal(SIGTSTP
, SIG_IGN
);
5651 signal(SIGTTOU
, SIG_IGN
);
5652 signal(SIGTTIN
, SIG_IGN
);
5656 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5660 if (write(fds
[1], &status
, 1) != 1) {
5661 perror("daemonize. Writing to pipe\n");
5665 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5669 if (kvm_enabled()) {
5672 ret
= kvm_init(smp_cpus
);
5674 fprintf(stderr
, "failed to initialize KVM\n");
5679 if (qemu_init_main_loop()) {
5680 fprintf(stderr
, "qemu_init_main_loop failed\n");
5683 linux_boot
= (kernel_filename
!= NULL
);
5685 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5686 fprintf(stderr
, "-append only allowed with -kernel option\n");
5690 if (!linux_boot
&& initrd_filename
!= NULL
) {
5691 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5696 /* Win32 doesn't support line-buffering and requires size >= 2 */
5697 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5700 if (init_timer_alarm() < 0) {
5701 fprintf(stderr
, "could not initialize alarm timer\n");
5704 if (use_icount
&& icount_time_shift
< 0) {
5706 /* 125MIPS seems a reasonable initial guess at the guest speed.
5707 It will be corrected fairly quickly anyway. */
5708 icount_time_shift
= 3;
5709 init_icount_adjust();
5716 if (net_init_clients() < 0) {
5720 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5721 net_set_boot_mask(net_boot
);
5723 /* init the bluetooth world */
5724 if (foreach_device_config(DEV_BT
, bt_parse
))
5727 /* init the memory */
5729 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5731 /* init the dynamic translator */
5732 cpu_exec_init_all(tb_size
* 1024 * 1024);
5734 bdrv_init_with_whitelist();
5738 if (default_cdrom
) {
5739 /* we always create the cdrom drive, even if no disk is there */
5740 drive_add(NULL
, CDROM_ALIAS
);
5743 if (default_floppy
) {
5744 /* we always create at least one floppy */
5745 drive_add(NULL
, FD_ALIAS
, 0);
5748 if (default_sdcard
) {
5749 /* we always create one sd slot, even if no card is in it */
5750 drive_add(NULL
, SD_ALIAS
);
5753 /* open the virtual block devices */
5755 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5756 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5759 vmstate_register(0, &vmstate_timers
,&timers_state
);
5760 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5763 if (nb_numa_nodes
> 0) {
5766 if (nb_numa_nodes
> smp_cpus
) {
5767 nb_numa_nodes
= smp_cpus
;
5770 /* If no memory size if given for any node, assume the default case
5771 * and distribute the available memory equally across all nodes
5773 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5774 if (node_mem
[i
] != 0)
5777 if (i
== nb_numa_nodes
) {
5778 uint64_t usedmem
= 0;
5780 /* On Linux, the each node's border has to be 8MB aligned,
5781 * the final node gets the rest.
5783 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5784 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5785 usedmem
+= node_mem
[i
];
5787 node_mem
[i
] = ram_size
- usedmem
;
5790 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5791 if (node_cpumask
[i
] != 0)
5794 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5795 * must cope with this anyway, because there are BIOSes out there in
5796 * real machines which also use this scheme.
5798 if (i
== nb_numa_nodes
) {
5799 for (i
= 0; i
< smp_cpus
; i
++) {
5800 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5805 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5807 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5809 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5811 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5814 module_call_init(MODULE_INIT_DEVICE
);
5816 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5820 i
= select_watchdog(watchdog
);
5822 exit (i
== 1 ? 1 : 0);
5825 if (machine
->compat_props
) {
5826 qdev_prop_register_global_list(machine
->compat_props
);
5830 machine
->init(ram_size
, boot_devices
,
5831 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5835 /* must be after terminal init, SDL library changes signal handlers */
5839 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5840 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5841 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5847 current_machine
= machine
;
5849 /* init USB devices */
5851 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5855 /* init generic devices */
5856 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5859 net_check_clients();
5861 /* just use the first displaystate for the moment */
5862 ds
= get_displaystate();
5864 if (display_type
== DT_DEFAULT
) {
5865 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5866 display_type
= DT_SDL
;
5868 display_type
= DT_VNC
;
5869 vnc_display
= "localhost:0,to=99";
5875 switch (display_type
) {
5878 #if defined(CONFIG_CURSES)
5880 curses_display_init(ds
, full_screen
);
5883 #if defined(CONFIG_SDL)
5885 sdl_display_init(ds
, full_screen
, no_frame
);
5887 #elif defined(CONFIG_COCOA)
5889 cocoa_display_init(ds
, full_screen
);
5893 vnc_display_init(ds
);
5894 if (vnc_display_open(ds
, vnc_display
) < 0)
5897 if (show_vnc_port
) {
5898 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5906 dcl
= ds
->listeners
;
5907 while (dcl
!= NULL
) {
5908 if (dcl
->dpy_refresh
!= NULL
) {
5909 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5910 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5915 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5916 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5917 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5920 text_consoles_set_display(ds
);
5922 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5925 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5926 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5931 qdev_machine_creation_done();
5933 if (rom_load_all() != 0) {
5934 fprintf(stderr
, "rom loading failed\n");
5938 qemu_system_reset();
5940 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5946 qemu_start_incoming_migration(incoming
);
5947 } else if (autostart
) {
5957 len
= write(fds
[1], &status
, 1);
5958 if (len
== -1 && (errno
== EINTR
))
5965 perror("not able to chdir to /");
5968 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5974 pwd
= getpwnam(run_as
);
5976 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5982 if (chroot(chroot_dir
) < 0) {
5983 fprintf(stderr
, "chroot failed\n");
5987 perror("not able to chdir to /");
5993 if (setgid(pwd
->pw_gid
) < 0) {
5994 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5997 if (setuid(pwd
->pw_uid
) < 0) {
5998 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6001 if (setuid(0) != -1) {
6002 fprintf(stderr
, "Dropping privileges failed\n");