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
;
188 const char *mem_path
= NULL
;
190 int mem_prealloc
= 0; /* force preallocation of physical target memory */
193 NICInfo nd_table
[MAX_NICS
];
196 static int rtc_utc
= 1;
197 static int rtc_date_offset
= -1; /* -1 means no change */
198 QEMUClock
*rtc_clock
;
199 int vga_interface_type
= VGA_NONE
;
201 int graphic_width
= 1024;
202 int graphic_height
= 768;
203 int graphic_depth
= 8;
205 int graphic_width
= 800;
206 int graphic_height
= 600;
207 int graphic_depth
= 15;
209 static int full_screen
= 0;
211 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
216 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
218 int win2k_install_hack
= 0;
227 const char *vnc_display
;
228 int acpi_enabled
= 1;
234 int graphic_rotate
= 0;
235 uint8_t irq0override
= 1;
239 const char *watchdog
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
249 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
250 unsigned int nb_prom_envs
= 0;
251 const char *prom_envs
[MAX_PROM_ENVS
];
256 uint64_t node_mem
[MAX_NODES
];
257 uint64_t node_cpumask
[MAX_NODES
];
259 static CPUState
*cur_cpu
;
260 static CPUState
*next_cpu
;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
269 static QEMUTimer
*nographic_timer
;
271 uint8_t qemu_uuid
[16];
273 static QEMUBootSetHandler
*boot_set_handler
;
274 static void *boot_set_opaque
;
277 #define SIG_IPI (SIGRTMIN+4)
279 #define SIG_IPI SIGUSR1
282 static int default_serial
= 1;
283 static int default_parallel
= 1;
284 static int default_virtcon
= 1;
285 static int default_monitor
= 1;
286 static int default_vga
= 1;
287 static int default_floppy
= 1;
288 static int default_cdrom
= 1;
289 static int default_sdcard
= 1;
295 { .driver
= "isa-serial", .flag
= &default_serial
},
296 { .driver
= "isa-parallel", .flag
= &default_parallel
},
297 { .driver
= "isa-fdc", .flag
= &default_floppy
},
298 { .driver
= "ide-drive", .flag
= &default_cdrom
},
299 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
300 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
301 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
302 { .driver
= "VGA", .flag
= &default_vga
},
303 { .driver
= "cirrus-vga", .flag
= &default_vga
},
304 { .driver
= "vmware-svga", .flag
= &default_vga
},
307 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
309 const char *driver
= qemu_opt_get(opts
, "driver");
314 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
315 if (strcmp(default_list
[i
].driver
, driver
) != 0)
317 *(default_list
[i
].flag
) = 0;
322 /***********************************************************/
323 /* x86 ISA bus support */
325 target_phys_addr_t isa_mem_base
= 0;
328 /***********************************************************/
329 void hw_error(const char *fmt
, ...)
335 fprintf(stderr
, "qemu: hardware error: ");
336 vfprintf(stderr
, fmt
, ap
);
337 fprintf(stderr
, "\n");
338 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
339 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
341 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
343 cpu_dump_state(env
, stderr
, fprintf
, 0);
350 static void set_proc_name(const char *s
)
352 #if defined(__linux__) && defined(PR_SET_NAME)
356 name
[sizeof(name
) - 1] = 0;
357 strncpy(name
, s
, sizeof(name
));
358 /* Could rewrite argv[0] too, but that's a bit more complicated.
359 This simple way is enough for `top'. */
360 prctl(PR_SET_NAME
, name
);
367 static QEMUBalloonEvent
*qemu_balloon_event
;
368 void *qemu_balloon_event_opaque
;
370 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
372 qemu_balloon_event
= func
;
373 qemu_balloon_event_opaque
= opaque
;
376 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
378 if (qemu_balloon_event
) {
379 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
386 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
388 if (qemu_balloon_event
) {
389 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
397 /***********************************************************/
398 /* real time host monotonic timer */
400 /* compute with 96 bit intermediate result: (a*b)/c */
401 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
406 #ifdef HOST_WORDS_BIGENDIAN
416 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
417 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
420 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
424 static int64_t get_clock_realtime(void)
428 gettimeofday(&tv
, NULL
);
429 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
434 static int64_t clock_freq
;
436 static void init_get_clock(void)
440 ret
= QueryPerformanceFrequency(&freq
);
442 fprintf(stderr
, "Could not calibrate ticks\n");
445 clock_freq
= freq
.QuadPart
;
448 static int64_t get_clock(void)
451 QueryPerformanceCounter(&ti
);
452 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
457 static int use_rt_clock
;
459 static void init_get_clock(void)
462 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
463 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
466 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
473 static int64_t get_clock(void)
475 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
476 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
479 clock_gettime(CLOCK_MONOTONIC
, &ts
);
480 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
484 /* XXX: using gettimeofday leads to problems if the date
485 changes, so it should be avoided. */
486 return get_clock_realtime();
491 /* Return the virtual CPU time, based on the instruction counter. */
492 static int64_t cpu_get_icount(void)
495 CPUState
*env
= cpu_single_env
;;
496 icount
= qemu_icount
;
499 fprintf(stderr
, "Bad clock read\n");
500 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
502 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
505 /***********************************************************/
506 /* guest cycle counter */
508 typedef struct TimersState
{
509 int64_t cpu_ticks_prev
;
510 int64_t cpu_ticks_offset
;
511 int64_t cpu_clock_offset
;
512 int32_t cpu_ticks_enabled
;
516 TimersState timers_state
;
518 /* return the host CPU cycle counter and handle stop/restart */
519 int64_t cpu_get_ticks(void)
522 return cpu_get_icount();
524 if (!timers_state
.cpu_ticks_enabled
) {
525 return timers_state
.cpu_ticks_offset
;
528 ticks
= cpu_get_real_ticks();
529 if (timers_state
.cpu_ticks_prev
> ticks
) {
530 /* Note: non increasing ticks may happen if the host uses
532 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
534 timers_state
.cpu_ticks_prev
= ticks
;
535 return ticks
+ timers_state
.cpu_ticks_offset
;
539 /* return the host CPU monotonic timer and handle stop/restart */
540 static int64_t cpu_get_clock(void)
543 if (!timers_state
.cpu_ticks_enabled
) {
544 return timers_state
.cpu_clock_offset
;
547 return ti
+ timers_state
.cpu_clock_offset
;
551 /* enable cpu_get_ticks() */
552 void cpu_enable_ticks(void)
554 if (!timers_state
.cpu_ticks_enabled
) {
555 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
556 timers_state
.cpu_clock_offset
-= get_clock();
557 timers_state
.cpu_ticks_enabled
= 1;
561 /* disable cpu_get_ticks() : the clock is stopped. You must not call
562 cpu_get_ticks() after that. */
563 void cpu_disable_ticks(void)
565 if (timers_state
.cpu_ticks_enabled
) {
566 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
567 timers_state
.cpu_clock_offset
= cpu_get_clock();
568 timers_state
.cpu_ticks_enabled
= 0;
572 /***********************************************************/
575 #define QEMU_CLOCK_REALTIME 0
576 #define QEMU_CLOCK_VIRTUAL 1
577 #define QEMU_CLOCK_HOST 2
582 /* XXX: add frequency */
590 struct QEMUTimer
*next
;
593 struct qemu_alarm_timer
{
595 int (*start
)(struct qemu_alarm_timer
*t
);
596 void (*stop
)(struct qemu_alarm_timer
*t
);
597 void (*rearm
)(struct qemu_alarm_timer
*t
);
604 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
609 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
611 if (!alarm_has_dynticks(t
))
617 /* TODO: MIN_TIMER_REARM_US should be optimized */
618 #define MIN_TIMER_REARM_US 250
620 static struct qemu_alarm_timer
*alarm_timer
;
624 struct qemu_alarm_win32
{
627 } alarm_win32_data
= {0, 0};
629 static int win32_start_timer(struct qemu_alarm_timer
*t
);
630 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
631 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
635 static int unix_start_timer(struct qemu_alarm_timer
*t
);
636 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
640 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
641 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
642 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
644 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
645 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
647 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
648 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
650 #endif /* __linux__ */
654 /* Correlation between real and virtual time is always going to be
655 fairly approximate, so ignore small variation.
656 When the guest is idle real and virtual time will be aligned in
658 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
660 static void icount_adjust(void)
665 static int64_t last_delta
;
666 /* If the VM is not running, then do nothing. */
670 cur_time
= cpu_get_clock();
671 cur_icount
= qemu_get_clock(vm_clock
);
672 delta
= cur_icount
- cur_time
;
673 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
675 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
676 && icount_time_shift
> 0) {
677 /* The guest is getting too far ahead. Slow time down. */
681 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
682 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
683 /* The guest is getting too far behind. Speed time up. */
687 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
690 static void icount_adjust_rt(void * opaque
)
692 qemu_mod_timer(icount_rt_timer
,
693 qemu_get_clock(rt_clock
) + 1000);
697 static void icount_adjust_vm(void * opaque
)
699 qemu_mod_timer(icount_vm_timer
,
700 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
704 static void configure_icount(const char *option
)
709 if (strcmp(option
, "auto") != 0) {
710 icount_time_shift
= strtol(option
, NULL
, 0);
717 /* 125MIPS seems a reasonable initial guess at the guest speed.
718 It will be corrected fairly quickly anyway. */
719 icount_time_shift
= 3;
721 /* Have both realtime and virtual time triggers for speed adjustment.
722 The realtime trigger catches emulated time passing too slowly,
723 the virtual time trigger catches emulated time passing too fast.
724 Realtime triggers occur even when idle, so use them less frequently
726 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
727 qemu_mod_timer(icount_rt_timer
,
728 qemu_get_clock(rt_clock
) + 1000);
729 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
730 qemu_mod_timer(icount_vm_timer
,
731 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
734 static struct qemu_alarm_timer alarm_timers
[] = {
737 {"dynticks", dynticks_start_timer
,
738 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
739 /* HPET - if available - is preferred */
740 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
741 /* ...otherwise try RTC */
742 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
744 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
746 {"dynticks", win32_start_timer
,
747 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
748 {"win32", win32_start_timer
,
749 win32_stop_timer
, NULL
, &alarm_win32_data
},
754 static void show_available_alarms(void)
758 printf("Available alarm timers, in order of precedence:\n");
759 for (i
= 0; alarm_timers
[i
].name
; i
++)
760 printf("%s\n", alarm_timers
[i
].name
);
763 static void configure_alarms(char const *opt
)
767 int count
= ARRAY_SIZE(alarm_timers
) - 1;
770 struct qemu_alarm_timer tmp
;
772 if (!strcmp(opt
, "?")) {
773 show_available_alarms();
777 arg
= qemu_strdup(opt
);
779 /* Reorder the array */
780 name
= strtok(arg
, ",");
782 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
783 if (!strcmp(alarm_timers
[i
].name
, name
))
788 fprintf(stderr
, "Unknown clock %s\n", name
);
797 tmp
= alarm_timers
[i
];
798 alarm_timers
[i
] = alarm_timers
[cur
];
799 alarm_timers
[cur
] = tmp
;
803 name
= strtok(NULL
, ",");
809 /* Disable remaining timers */
810 for (i
= cur
; i
< count
; i
++)
811 alarm_timers
[i
].name
= NULL
;
813 show_available_alarms();
818 #define QEMU_NUM_CLOCKS 3
822 QEMUClock
*host_clock
;
824 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
826 static QEMUClock
*qemu_new_clock(int type
)
829 clock
= qemu_mallocz(sizeof(QEMUClock
));
835 static void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
837 clock
->enabled
= enabled
;
840 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
844 ts
= qemu_mallocz(sizeof(QEMUTimer
));
851 void qemu_free_timer(QEMUTimer
*ts
)
856 /* stop a timer, but do not dealloc it */
857 void qemu_del_timer(QEMUTimer
*ts
)
861 /* NOTE: this code must be signal safe because
862 qemu_timer_expired() can be called from a signal. */
863 pt
= &active_timers
[ts
->clock
->type
];
876 /* modify the current timer so that it will be fired when current_time
877 >= expire_time. The corresponding callback will be called. */
878 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
884 /* add the timer in the sorted list */
885 /* NOTE: this code must be signal safe because
886 qemu_timer_expired() can be called from a signal. */
887 pt
= &active_timers
[ts
->clock
->type
];
892 if (t
->expire_time
> expire_time
)
896 ts
->expire_time
= expire_time
;
900 /* Rearm if necessary */
901 if (pt
== &active_timers
[ts
->clock
->type
]) {
902 if (!alarm_timer
->pending
) {
903 qemu_rearm_alarm_timer(alarm_timer
);
905 /* Interrupt execution to force deadline recalculation. */
911 int qemu_timer_pending(QEMUTimer
*ts
)
914 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
921 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
925 return (timer_head
->expire_time
<= current_time
);
928 static void qemu_run_timers(QEMUClock
*clock
)
930 QEMUTimer
**ptimer_head
, *ts
;
931 int64_t current_time
;
936 current_time
= qemu_get_clock (clock
);
937 ptimer_head
= &active_timers
[clock
->type
];
940 if (!ts
|| ts
->expire_time
> current_time
)
942 /* remove timer from the list before calling the callback */
943 *ptimer_head
= ts
->next
;
946 /* run the callback (the timer list can be modified) */
951 int64_t qemu_get_clock(QEMUClock
*clock
)
953 switch(clock
->type
) {
954 case QEMU_CLOCK_REALTIME
:
955 return get_clock() / 1000000;
957 case QEMU_CLOCK_VIRTUAL
:
959 return cpu_get_icount();
961 return cpu_get_clock();
963 case QEMU_CLOCK_HOST
:
964 return get_clock_realtime();
968 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
970 switch(clock
->type
) {
971 case QEMU_CLOCK_REALTIME
:
974 case QEMU_CLOCK_VIRTUAL
:
976 return cpu_get_icount();
978 return cpu_get_clock();
980 case QEMU_CLOCK_HOST
:
981 return get_clock_realtime();
985 static void init_clocks(void)
988 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
989 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
990 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
992 rtc_clock
= host_clock
;
996 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
998 uint64_t expire_time
;
1000 if (qemu_timer_pending(ts
)) {
1001 expire_time
= ts
->expire_time
;
1005 qemu_put_be64(f
, expire_time
);
1008 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1010 uint64_t expire_time
;
1012 expire_time
= qemu_get_be64(f
);
1013 if (expire_time
!= -1) {
1014 qemu_mod_timer(ts
, expire_time
);
1020 static const VMStateDescription vmstate_timers
= {
1023 .minimum_version_id
= 1,
1024 .minimum_version_id_old
= 1,
1025 .fields
= (VMStateField
[]) {
1026 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1027 VMSTATE_INT64(dummy
, TimersState
),
1028 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1029 VMSTATE_END_OF_LIST()
1033 static void qemu_run_all_timers(void)
1035 /* rearm timer, if not periodic */
1036 if (alarm_timer
->expired
) {
1037 alarm_timer
->expired
= 0;
1038 qemu_rearm_alarm_timer(alarm_timer
);
1041 alarm_timer
->pending
= 0;
1043 /* vm time timers */
1045 qemu_run_timers(vm_clock
);
1048 qemu_run_timers(rt_clock
);
1049 qemu_run_timers(host_clock
);
1053 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1054 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1057 static void host_alarm_handler(int host_signum
)
1060 struct qemu_alarm_timer
*t
= alarm_timer
;
1065 #define DISP_FREQ 1000
1067 static int64_t delta_min
= INT64_MAX
;
1068 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1070 ti
= qemu_get_clock(vm_clock
);
1071 if (last_clock
!= 0) {
1072 delta
= ti
- last_clock
;
1073 if (delta
< delta_min
)
1075 if (delta
> delta_max
)
1078 if (++count
== DISP_FREQ
) {
1079 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1080 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1081 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1082 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1083 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1085 delta_min
= INT64_MAX
;
1093 if (alarm_has_dynticks(t
) ||
1095 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1096 qemu_get_clock(vm_clock
))) ||
1097 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1098 qemu_get_clock(rt_clock
)) ||
1099 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1100 qemu_get_clock(host_clock
))) {
1102 t
->expired
= alarm_has_dynticks(t
);
1104 qemu_notify_event();
1108 static int64_t qemu_next_deadline(void)
1110 /* To avoid problems with overflow limit this to 2^32. */
1111 int64_t delta
= INT32_MAX
;
1113 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1114 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1115 qemu_get_clock(vm_clock
);
1117 if (active_timers
[QEMU_CLOCK_HOST
]) {
1118 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1119 qemu_get_clock(host_clock
);
1130 #if defined(__linux__)
1131 static uint64_t qemu_next_deadline_dyntick(void)
1139 delta
= (qemu_next_deadline() + 999) / 1000;
1141 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1142 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1143 qemu_get_clock(rt_clock
))*1000;
1144 if (rtdelta
< delta
)
1148 if (delta
< MIN_TIMER_REARM_US
)
1149 delta
= MIN_TIMER_REARM_US
;
1157 /* Sets a specific flag */
1158 static int fcntl_setfl(int fd
, int flag
)
1162 flags
= fcntl(fd
, F_GETFL
);
1166 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1172 #if defined(__linux__)
1174 #define RTC_FREQ 1024
1176 static void enable_sigio_timer(int fd
)
1178 struct sigaction act
;
1181 sigfillset(&act
.sa_mask
);
1183 act
.sa_handler
= host_alarm_handler
;
1185 sigaction(SIGIO
, &act
, NULL
);
1186 fcntl_setfl(fd
, O_ASYNC
);
1187 fcntl(fd
, F_SETOWN
, getpid());
1190 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1192 struct hpet_info info
;
1195 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1200 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1202 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1203 "error, but for better emulation accuracy type:\n"
1204 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1208 /* Check capabilities */
1209 r
= ioctl(fd
, HPET_INFO
, &info
);
1213 /* Enable periodic mode */
1214 r
= ioctl(fd
, HPET_EPI
, 0);
1215 if (info
.hi_flags
&& (r
< 0))
1218 /* Enable interrupt */
1219 r
= ioctl(fd
, HPET_IE_ON
, 0);
1223 enable_sigio_timer(fd
);
1224 t
->priv
= (void *)(long)fd
;
1232 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1234 int fd
= (long)t
->priv
;
1239 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1242 unsigned long current_rtc_freq
= 0;
1244 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1247 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1248 if (current_rtc_freq
!= RTC_FREQ
&&
1249 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1250 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1251 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1252 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1255 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1261 enable_sigio_timer(rtc_fd
);
1263 t
->priv
= (void *)(long)rtc_fd
;
1268 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1270 int rtc_fd
= (long)t
->priv
;
1275 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1279 struct sigaction act
;
1281 sigfillset(&act
.sa_mask
);
1283 act
.sa_handler
= host_alarm_handler
;
1285 sigaction(SIGALRM
, &act
, NULL
);
1288 * Initialize ev struct to 0 to avoid valgrind complaining
1289 * about uninitialized data in timer_create call
1291 memset(&ev
, 0, sizeof(ev
));
1292 ev
.sigev_value
.sival_int
= 0;
1293 ev
.sigev_notify
= SIGEV_SIGNAL
;
1294 ev
.sigev_signo
= SIGALRM
;
1296 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1297 perror("timer_create");
1299 /* disable dynticks */
1300 fprintf(stderr
, "Dynamic Ticks disabled\n");
1305 t
->priv
= (void *)(long)host_timer
;
1310 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1312 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1314 timer_delete(host_timer
);
1317 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1319 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1320 struct itimerspec timeout
;
1321 int64_t nearest_delta_us
= INT64_MAX
;
1324 assert(alarm_has_dynticks(t
));
1325 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1326 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1327 !active_timers
[QEMU_CLOCK_HOST
])
1330 nearest_delta_us
= qemu_next_deadline_dyntick();
1332 /* check whether a timer is already running */
1333 if (timer_gettime(host_timer
, &timeout
)) {
1335 fprintf(stderr
, "Internal timer error: aborting\n");
1338 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1339 if (current_us
&& current_us
<= nearest_delta_us
)
1342 timeout
.it_interval
.tv_sec
= 0;
1343 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1344 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1345 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1346 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1348 fprintf(stderr
, "Internal timer error: aborting\n");
1353 #endif /* defined(__linux__) */
1355 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1357 struct sigaction act
;
1358 struct itimerval itv
;
1362 sigfillset(&act
.sa_mask
);
1364 act
.sa_handler
= host_alarm_handler
;
1366 sigaction(SIGALRM
, &act
, NULL
);
1368 itv
.it_interval
.tv_sec
= 0;
1369 /* for i386 kernel 2.6 to get 1 ms */
1370 itv
.it_interval
.tv_usec
= 999;
1371 itv
.it_value
.tv_sec
= 0;
1372 itv
.it_value
.tv_usec
= 10 * 1000;
1374 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1381 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1383 struct itimerval itv
;
1385 memset(&itv
, 0, sizeof(itv
));
1386 setitimer(ITIMER_REAL
, &itv
, NULL
);
1389 #endif /* !defined(_WIN32) */
1394 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1397 struct qemu_alarm_win32
*data
= t
->priv
;
1400 memset(&tc
, 0, sizeof(tc
));
1401 timeGetDevCaps(&tc
, sizeof(tc
));
1403 data
->period
= tc
.wPeriodMin
;
1404 timeBeginPeriod(data
->period
);
1406 flags
= TIME_CALLBACK_FUNCTION
;
1407 if (alarm_has_dynticks(t
))
1408 flags
|= TIME_ONESHOT
;
1410 flags
|= TIME_PERIODIC
;
1412 data
->timerId
= timeSetEvent(1, // interval (ms)
1413 data
->period
, // resolution
1414 host_alarm_handler
, // function
1415 (DWORD
)t
, // parameter
1418 if (!data
->timerId
) {
1419 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1421 timeEndPeriod(data
->period
);
1428 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1430 struct qemu_alarm_win32
*data
= t
->priv
;
1432 timeKillEvent(data
->timerId
);
1433 timeEndPeriod(data
->period
);
1436 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1438 struct qemu_alarm_win32
*data
= t
->priv
;
1440 assert(alarm_has_dynticks(t
));
1441 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1442 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1443 !active_timers
[QEMU_CLOCK_HOST
])
1446 timeKillEvent(data
->timerId
);
1448 data
->timerId
= timeSetEvent(1,
1452 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1454 if (!data
->timerId
) {
1455 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1458 timeEndPeriod(data
->period
);
1465 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1468 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1471 static int init_timer_alarm(void)
1473 struct qemu_alarm_timer
*t
= NULL
;
1476 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1477 t
= &alarm_timers
[i
];
1489 /* first event is at time 0 */
1492 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1500 static void quit_timers(void)
1502 struct qemu_alarm_timer
*t
= alarm_timer
;
1507 /***********************************************************/
1508 /* host time/date access */
1509 void qemu_get_timedate(struct tm
*tm
, int offset
)
1516 if (rtc_date_offset
== -1) {
1520 ret
= localtime(&ti
);
1522 ti
-= rtc_date_offset
;
1526 memcpy(tm
, ret
, sizeof(struct tm
));
1529 int qemu_timedate_diff(struct tm
*tm
)
1533 if (rtc_date_offset
== -1)
1535 seconds
= mktimegm(tm
);
1537 seconds
= mktime(tm
);
1539 seconds
= mktimegm(tm
) + rtc_date_offset
;
1541 return seconds
- time(NULL
);
1544 void rtc_change_mon_event(struct tm
*tm
)
1548 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
1549 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
1550 qobject_decref(data
);
1553 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1555 time_t rtc_start_date
;
1558 if (!strcmp(startdate
, "now") && legacy
) {
1559 rtc_date_offset
= -1;
1561 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1569 } else if (sscanf(startdate
, "%d-%d-%d",
1572 &tm
.tm_mday
) == 3) {
1581 rtc_start_date
= mktimegm(&tm
);
1582 if (rtc_start_date
== -1) {
1584 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1585 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1588 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1592 static void configure_rtc(QemuOpts
*opts
)
1596 value
= qemu_opt_get(opts
, "base");
1598 if (!strcmp(value
, "utc")) {
1600 } else if (!strcmp(value
, "localtime")) {
1603 configure_rtc_date_offset(value
, 0);
1606 value
= qemu_opt_get(opts
, "clock");
1608 if (!strcmp(value
, "host")) {
1609 rtc_clock
= host_clock
;
1610 } else if (!strcmp(value
, "vm")) {
1611 rtc_clock
= vm_clock
;
1613 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1617 #ifdef CONFIG_TARGET_I386
1618 value
= qemu_opt_get(opts
, "driftfix");
1620 if (!strcmp(buf
, "slew")) {
1622 } else if (!strcmp(buf
, "none")) {
1625 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1633 static void socket_cleanup(void)
1638 static int socket_init(void)
1643 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1645 err
= WSAGetLastError();
1646 fprintf(stderr
, "WSAStartup: %d\n", err
);
1649 atexit(socket_cleanup
);
1654 /***********************************************************/
1655 /* Bluetooth support */
1658 static struct HCIInfo
*hci_table
[MAX_NICS
];
1660 static struct bt_vlan_s
{
1661 struct bt_scatternet_s net
;
1663 struct bt_vlan_s
*next
;
1666 /* find or alloc a new bluetooth "VLAN" */
1667 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1669 struct bt_vlan_s
**pvlan
, *vlan
;
1670 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1674 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1676 pvlan
= &first_bt_vlan
;
1677 while (*pvlan
!= NULL
)
1678 pvlan
= &(*pvlan
)->next
;
1683 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1687 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1692 static struct HCIInfo null_hci
= {
1693 .cmd_send
= null_hci_send
,
1694 .sco_send
= null_hci_send
,
1695 .acl_send
= null_hci_send
,
1696 .bdaddr_set
= null_hci_addr_set
,
1699 struct HCIInfo
*qemu_next_hci(void)
1701 if (cur_hci
== nb_hcis
)
1704 return hci_table
[cur_hci
++];
1707 static struct HCIInfo
*hci_init(const char *str
)
1710 struct bt_scatternet_s
*vlan
= 0;
1712 if (!strcmp(str
, "null"))
1715 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1717 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1718 else if (!strncmp(str
, "hci", 3)) {
1721 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1722 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1727 vlan
= qemu_find_bt_vlan(0);
1729 return bt_new_hci(vlan
);
1732 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1737 static int bt_hci_parse(const char *str
)
1739 struct HCIInfo
*hci
;
1742 if (nb_hcis
>= MAX_NICS
) {
1743 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1747 hci
= hci_init(str
);
1756 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1757 hci
->bdaddr_set(hci
, bdaddr
.b
);
1759 hci_table
[nb_hcis
++] = hci
;
1764 static void bt_vhci_add(int vlan_id
)
1766 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1769 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1770 "an empty scatternet %i\n", vlan_id
);
1772 bt_vhci_init(bt_new_hci(vlan
));
1775 static struct bt_device_s
*bt_device_add(const char *opt
)
1777 struct bt_scatternet_s
*vlan
;
1779 char *endp
= strstr(opt
, ",vlan=");
1780 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1783 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1786 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1788 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1793 vlan
= qemu_find_bt_vlan(vlan_id
);
1796 fprintf(stderr
, "qemu: warning: adding a slave device to "
1797 "an empty scatternet %i\n", vlan_id
);
1799 if (!strcmp(devname
, "keyboard"))
1800 return bt_keyboard_init(vlan
);
1802 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1806 static int bt_parse(const char *opt
)
1808 const char *endp
, *p
;
1811 if (strstart(opt
, "hci", &endp
)) {
1812 if (!*endp
|| *endp
== ',') {
1814 if (!strstart(endp
, ",vlan=", 0))
1817 return bt_hci_parse(opt
);
1819 } else if (strstart(opt
, "vhci", &endp
)) {
1820 if (!*endp
|| *endp
== ',') {
1822 if (strstart(endp
, ",vlan=", &p
)) {
1823 vlan
= strtol(p
, (char **) &endp
, 0);
1825 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1829 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1838 } else if (strstart(opt
, "device:", &endp
))
1839 return !bt_device_add(endp
);
1841 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1845 /***********************************************************/
1846 /* QEMU Block devices */
1848 #define HD_ALIAS "index=%d,media=disk"
1849 #define CDROM_ALIAS "index=2,media=cdrom"
1850 #define FD_ALIAS "index=%d,if=floppy"
1851 #define PFLASH_ALIAS "if=pflash"
1852 #define MTD_ALIAS "if=mtd"
1853 #define SD_ALIAS "index=0,if=sd"
1855 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1862 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1865 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
1867 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1868 __FUNCTION__
, optstr
);
1872 qemu_opt_set(opts
, "file", file
);
1876 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1880 /* seek interface, bus and unit */
1882 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1883 if (dinfo
->type
== type
&&
1884 dinfo
->bus
== bus
&&
1885 dinfo
->unit
== unit
)
1892 DriveInfo
*drive_get_by_id(const char *id
)
1896 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1897 if (strcmp(id
, dinfo
->id
))
1904 int drive_get_max_bus(BlockInterfaceType type
)
1910 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1911 if(dinfo
->type
== type
&&
1912 dinfo
->bus
> max_bus
)
1913 max_bus
= dinfo
->bus
;
1918 const char *drive_get_serial(BlockDriverState
*bdrv
)
1922 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1923 if (dinfo
->bdrv
== bdrv
)
1924 return dinfo
->serial
;
1930 BlockInterfaceErrorAction
drive_get_on_error(
1931 BlockDriverState
*bdrv
, int is_read
)
1935 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1936 if (dinfo
->bdrv
== bdrv
)
1937 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1940 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1943 static void bdrv_format_print(void *opaque
, const char *name
)
1945 fprintf(stderr
, " %s", name
);
1948 void drive_uninit(DriveInfo
*dinfo
)
1950 qemu_opts_del(dinfo
->opts
);
1951 bdrv_delete(dinfo
->bdrv
);
1952 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1956 static int parse_block_error_action(const char *buf
, int is_read
)
1958 if (!strcmp(buf
, "ignore")) {
1959 return BLOCK_ERR_IGNORE
;
1960 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1961 return BLOCK_ERR_STOP_ENOSPC
;
1962 } else if (!strcmp(buf
, "stop")) {
1963 return BLOCK_ERR_STOP_ANY
;
1964 } else if (!strcmp(buf
, "report")) {
1965 return BLOCK_ERR_REPORT
;
1967 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1968 buf
, is_read
? "read" : "write");
1973 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1977 const char *file
= NULL
;
1980 const char *mediastr
= "";
1981 BlockInterfaceType type
;
1982 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1983 int bus_id
, unit_id
;
1984 int cyls
, heads
, secs
, translation
;
1985 BlockDriver
*drv
= NULL
;
1986 QEMUMachine
*machine
= opaque
;
1993 int on_read_error
, on_write_error
;
1994 const char *devaddr
;
2000 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2003 if (machine
&& machine
->use_scsi
) {
2005 max_devs
= MAX_SCSI_DEVS
;
2006 pstrcpy(devname
, sizeof(devname
), "scsi");
2009 max_devs
= MAX_IDE_DEVS
;
2010 pstrcpy(devname
, sizeof(devname
), "ide");
2014 /* extract parameters */
2015 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2016 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2017 index
= qemu_opt_get_number(opts
, "index", -1);
2019 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2020 heads
= qemu_opt_get_number(opts
, "heads", 0);
2021 secs
= qemu_opt_get_number(opts
, "secs", 0);
2023 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2024 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2026 file
= qemu_opt_get(opts
, "file");
2027 serial
= qemu_opt_get(opts
, "serial");
2029 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2030 pstrcpy(devname
, sizeof(devname
), buf
);
2031 if (!strcmp(buf
, "ide")) {
2033 max_devs
= MAX_IDE_DEVS
;
2034 } else if (!strcmp(buf
, "scsi")) {
2036 max_devs
= MAX_SCSI_DEVS
;
2037 } else if (!strcmp(buf
, "floppy")) {
2040 } else if (!strcmp(buf
, "pflash")) {
2043 } else if (!strcmp(buf
, "mtd")) {
2046 } else if (!strcmp(buf
, "sd")) {
2049 } else if (!strcmp(buf
, "virtio")) {
2052 } else if (!strcmp(buf
, "xen")) {
2055 } else if (!strcmp(buf
, "none")) {
2059 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2064 if (cyls
|| heads
|| secs
) {
2065 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2066 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2069 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2070 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2073 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2074 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2079 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2082 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2086 if (!strcmp(buf
, "none"))
2087 translation
= BIOS_ATA_TRANSLATION_NONE
;
2088 else if (!strcmp(buf
, "lba"))
2089 translation
= BIOS_ATA_TRANSLATION_LBA
;
2090 else if (!strcmp(buf
, "auto"))
2091 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2093 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2098 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2099 if (!strcmp(buf
, "disk")) {
2101 } else if (!strcmp(buf
, "cdrom")) {
2102 if (cyls
|| secs
|| heads
) {
2104 "qemu: '%s' invalid physical CHS format\n", buf
);
2107 media
= MEDIA_CDROM
;
2109 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2114 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2115 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2117 else if (!strcmp(buf
, "writethrough"))
2119 else if (!strcmp(buf
, "writeback"))
2122 fprintf(stderr
, "qemu: invalid cache option\n");
2127 #ifdef CONFIG_LINUX_AIO
2128 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2129 if (!strcmp(buf
, "threads"))
2131 else if (!strcmp(buf
, "native"))
2134 fprintf(stderr
, "qemu: invalid aio option\n");
2140 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2141 if (strcmp(buf
, "?") == 0) {
2142 fprintf(stderr
, "qemu: Supported formats:");
2143 bdrv_iterate_format(bdrv_format_print
, NULL
);
2144 fprintf(stderr
, "\n");
2147 drv
= bdrv_find_whitelisted_format(buf
);
2149 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2154 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2155 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2156 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2157 fprintf(stderr
, "werror is no supported by this format\n");
2161 on_write_error
= parse_block_error_action(buf
, 0);
2162 if (on_write_error
< 0) {
2167 on_read_error
= BLOCK_ERR_REPORT
;
2168 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2169 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2170 fprintf(stderr
, "rerror is no supported by this format\n");
2174 on_read_error
= parse_block_error_action(buf
, 1);
2175 if (on_read_error
< 0) {
2180 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2181 if (type
!= IF_VIRTIO
) {
2182 fprintf(stderr
, "addr is not supported\n");
2187 /* compute bus and unit according index */
2190 if (bus_id
!= 0 || unit_id
!= -1) {
2192 "qemu: index cannot be used with bus and unit\n");
2200 unit_id
= index
% max_devs
;
2201 bus_id
= index
/ max_devs
;
2205 /* if user doesn't specify a unit_id,
2206 * try to find the first free
2209 if (unit_id
== -1) {
2211 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2213 if (max_devs
&& unit_id
>= max_devs
) {
2214 unit_id
-= max_devs
;
2222 if (max_devs
&& unit_id
>= max_devs
) {
2223 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2224 unit_id
, max_devs
- 1);
2229 * ignore multiple definitions
2232 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2239 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2240 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2241 dinfo
->id
= qemu_strdup(buf
);
2243 /* no id supplied -> create one */
2244 dinfo
->id
= qemu_mallocz(32);
2245 if (type
== IF_IDE
|| type
== IF_SCSI
)
2246 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2248 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2249 devname
, bus_id
, mediastr
, unit_id
);
2251 snprintf(dinfo
->id
, 32, "%s%s%i",
2252 devname
, mediastr
, unit_id
);
2254 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2255 dinfo
->devaddr
= devaddr
;
2257 dinfo
->bus
= bus_id
;
2258 dinfo
->unit
= unit_id
;
2259 dinfo
->on_read_error
= on_read_error
;
2260 dinfo
->on_write_error
= on_write_error
;
2263 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2264 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2274 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2275 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2279 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2284 /* FIXME: This isn't really a floppy, but it's a reasonable
2287 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2293 /* add virtio block device */
2294 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2295 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2296 qemu_opt_set(opts
, "drive", dinfo
->id
);
2298 qemu_opt_set(opts
, "addr", devaddr
);
2309 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2310 cache
= 2; /* always use write-back with snapshot */
2312 if (cache
== 0) /* no caching */
2313 bdrv_flags
|= BDRV_O_NOCACHE
;
2314 else if (cache
== 2) /* write-back */
2315 bdrv_flags
|= BDRV_O_CACHE_WB
;
2318 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2320 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2324 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2325 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2330 * cdrom is read-only. Set it now, after above interface checking
2331 * since readonly attribute not explicitly required, so no error.
2333 if (media
== MEDIA_CDROM
) {
2336 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2338 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2339 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2340 file
, strerror(errno
));
2344 if (bdrv_key_required(dinfo
->bdrv
))
2350 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2352 QEMUMachine
*machine
= opaque
;
2353 int fatal_error
= 0;
2355 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2362 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2364 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2365 qemu_opt_set(opts
, "snapshot", "on");
2370 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2372 boot_set_handler
= func
;
2373 boot_set_opaque
= opaque
;
2376 int qemu_boot_set(const char *boot_devices
)
2378 if (!boot_set_handler
) {
2381 return boot_set_handler(boot_set_opaque
, boot_devices
);
2384 static int parse_bootdevices(char *devices
)
2386 /* We just do some generic consistency checks */
2390 for (p
= devices
; *p
!= '\0'; p
++) {
2391 /* Allowed boot devices are:
2392 * a-b: floppy disk drives
2393 * c-f: IDE disk drives
2394 * g-m: machine implementation dependant drives
2395 * n-p: network devices
2396 * It's up to each machine implementation to check if the given boot
2397 * devices match the actual hardware implementation and firmware
2400 if (*p
< 'a' || *p
> 'p') {
2401 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2404 if (bitmap
& (1 << (*p
- 'a'))) {
2405 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2408 bitmap
|= 1 << (*p
- 'a');
2413 static void restore_boot_devices(void *opaque
)
2415 char *standard_boot_devices
= opaque
;
2417 qemu_boot_set(standard_boot_devices
);
2419 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2420 qemu_free(standard_boot_devices
);
2423 static void numa_add(const char *optarg
)
2427 unsigned long long value
, endvalue
;
2430 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2431 if (!strcmp(option
, "node")) {
2432 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2433 nodenr
= nb_numa_nodes
;
2435 nodenr
= strtoull(option
, NULL
, 10);
2438 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2439 node_mem
[nodenr
] = 0;
2441 value
= strtoull(option
, &endptr
, 0);
2443 case 0: case 'M': case 'm':
2450 node_mem
[nodenr
] = value
;
2452 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2453 node_cpumask
[nodenr
] = 0;
2455 value
= strtoull(option
, &endptr
, 10);
2458 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2460 if (*endptr
== '-') {
2461 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2462 if (endvalue
>= 63) {
2465 "only 63 CPUs in NUMA mode supported.\n");
2467 value
= (2ULL << endvalue
) - (1ULL << value
);
2469 value
= 1ULL << value
;
2472 node_cpumask
[nodenr
] = value
;
2479 static void smp_parse(const char *optarg
)
2481 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2485 smp
= strtoul(optarg
, &endptr
, 10);
2486 if (endptr
!= optarg
) {
2487 if (*endptr
== ',') {
2491 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2492 sockets
= strtoull(option
, NULL
, 10);
2493 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2494 cores
= strtoull(option
, NULL
, 10);
2495 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2496 threads
= strtoull(option
, NULL
, 10);
2497 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2498 max_cpus
= strtoull(option
, NULL
, 10);
2500 /* compute missing values, prefer sockets over cores over threads */
2501 if (smp
== 0 || sockets
== 0) {
2502 sockets
= sockets
> 0 ? sockets
: 1;
2503 cores
= cores
> 0 ? cores
: 1;
2504 threads
= threads
> 0 ? threads
: 1;
2506 smp
= cores
* threads
* sockets
;
2510 threads
= threads
> 0 ? threads
: 1;
2511 cores
= smp
/ (sockets
* threads
);
2514 threads
= smp
/ (cores
* sockets
);
2519 smp_cores
= cores
> 0 ? cores
: 1;
2520 smp_threads
= threads
> 0 ? threads
: 1;
2522 max_cpus
= smp_cpus
;
2525 /***********************************************************/
2528 static int usb_device_add(const char *devname
, int is_hotplug
)
2531 USBDevice
*dev
= NULL
;
2536 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2537 dev
= usbdevice_create(devname
);
2541 /* the other ones */
2542 if (strstart(devname
, "host:", &p
)) {
2543 dev
= usb_host_device_open(p
);
2544 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2545 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2546 bt_new_hci(qemu_find_bt_vlan(0)));
2557 static int usb_device_del(const char *devname
)
2562 if (strstart(devname
, "host:", &p
))
2563 return usb_host_device_close(p
);
2568 p
= strchr(devname
, '.');
2571 bus_num
= strtoul(devname
, NULL
, 0);
2572 addr
= strtoul(p
+ 1, NULL
, 0);
2574 return usb_device_delete_addr(bus_num
, addr
);
2577 static int usb_parse(const char *cmdline
)
2580 r
= usb_device_add(cmdline
, 0);
2582 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2587 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2589 const char *devname
= qdict_get_str(qdict
, "devname");
2590 if (usb_device_add(devname
, 1) < 0) {
2591 error_report("could not add USB device '%s'", devname
);
2595 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2597 const char *devname
= qdict_get_str(qdict
, "devname");
2598 if (usb_device_del(devname
) < 0) {
2599 error_report("could not delete USB device '%s'", devname
);
2603 /***********************************************************/
2604 /* PCMCIA/Cardbus */
2606 static struct pcmcia_socket_entry_s
{
2607 PCMCIASocket
*socket
;
2608 struct pcmcia_socket_entry_s
*next
;
2609 } *pcmcia_sockets
= 0;
2611 void pcmcia_socket_register(PCMCIASocket
*socket
)
2613 struct pcmcia_socket_entry_s
*entry
;
2615 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2616 entry
->socket
= socket
;
2617 entry
->next
= pcmcia_sockets
;
2618 pcmcia_sockets
= entry
;
2621 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2623 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2625 ptr
= &pcmcia_sockets
;
2626 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2627 if (entry
->socket
== socket
) {
2633 void pcmcia_info(Monitor
*mon
)
2635 struct pcmcia_socket_entry_s
*iter
;
2637 if (!pcmcia_sockets
)
2638 monitor_printf(mon
, "No PCMCIA sockets\n");
2640 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2641 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2642 iter
->socket
->attached
? iter
->socket
->card_string
:
2646 /***********************************************************/
2649 typedef struct IOHandlerRecord
{
2651 IOCanRWHandler
*fd_read_poll
;
2653 IOHandler
*fd_write
;
2656 /* temporary data */
2658 struct IOHandlerRecord
*next
;
2661 static IOHandlerRecord
*first_io_handler
;
2663 /* XXX: fd_read_poll should be suppressed, but an API change is
2664 necessary in the character devices to suppress fd_can_read(). */
2665 int qemu_set_fd_handler2(int fd
,
2666 IOCanRWHandler
*fd_read_poll
,
2668 IOHandler
*fd_write
,
2671 IOHandlerRecord
**pioh
, *ioh
;
2673 if (!fd_read
&& !fd_write
) {
2674 pioh
= &first_io_handler
;
2679 if (ioh
->fd
== fd
) {
2686 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2690 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2691 ioh
->next
= first_io_handler
;
2692 first_io_handler
= ioh
;
2695 ioh
->fd_read_poll
= fd_read_poll
;
2696 ioh
->fd_read
= fd_read
;
2697 ioh
->fd_write
= fd_write
;
2698 ioh
->opaque
= opaque
;
2704 int qemu_set_fd_handler(int fd
,
2706 IOHandler
*fd_write
,
2709 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2713 /***********************************************************/
2714 /* Polling handling */
2716 typedef struct PollingEntry
{
2719 struct PollingEntry
*next
;
2722 static PollingEntry
*first_polling_entry
;
2724 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2726 PollingEntry
**ppe
, *pe
;
2727 pe
= qemu_mallocz(sizeof(PollingEntry
));
2729 pe
->opaque
= opaque
;
2730 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2735 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2737 PollingEntry
**ppe
, *pe
;
2738 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2740 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2748 /***********************************************************/
2749 /* Wait objects support */
2750 typedef struct WaitObjects
{
2752 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2753 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2754 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2757 static WaitObjects wait_objects
= {0};
2759 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2761 WaitObjects
*w
= &wait_objects
;
2763 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2765 w
->events
[w
->num
] = handle
;
2766 w
->func
[w
->num
] = func
;
2767 w
->opaque
[w
->num
] = opaque
;
2772 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2775 WaitObjects
*w
= &wait_objects
;
2778 for (i
= 0; i
< w
->num
; i
++) {
2779 if (w
->events
[i
] == handle
)
2782 w
->events
[i
] = w
->events
[i
+ 1];
2783 w
->func
[i
] = w
->func
[i
+ 1];
2784 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2792 /***********************************************************/
2793 /* ram save/restore */
2795 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2796 #define RAM_SAVE_FLAG_COMPRESS 0x02
2797 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2798 #define RAM_SAVE_FLAG_PAGE 0x08
2799 #define RAM_SAVE_FLAG_EOS 0x10
2801 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2803 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2804 uint32_t *array
= (uint32_t *)page
;
2807 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2808 if (array
[i
] != val
)
2815 static int ram_save_block(QEMUFile
*f
)
2817 static ram_addr_t current_addr
= 0;
2818 ram_addr_t saved_addr
= current_addr
;
2819 ram_addr_t addr
= 0;
2822 while (addr
< last_ram_offset
) {
2823 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2826 cpu_physical_memory_reset_dirty(current_addr
,
2827 current_addr
+ TARGET_PAGE_SIZE
,
2828 MIGRATION_DIRTY_FLAG
);
2830 p
= qemu_get_ram_ptr(current_addr
);
2832 if (is_dup_page(p
, *p
)) {
2833 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2834 qemu_put_byte(f
, *p
);
2836 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2837 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2843 addr
+= TARGET_PAGE_SIZE
;
2844 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2850 static uint64_t bytes_transferred
;
2852 static ram_addr_t
ram_save_remaining(void)
2855 ram_addr_t count
= 0;
2857 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2858 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2865 uint64_t ram_bytes_remaining(void)
2867 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2870 uint64_t ram_bytes_transferred(void)
2872 return bytes_transferred
;
2875 uint64_t ram_bytes_total(void)
2877 return last_ram_offset
;
2880 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2883 uint64_t bytes_transferred_last
;
2885 uint64_t expected_time
= 0;
2888 cpu_physical_memory_set_dirty_tracking(0);
2892 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2893 qemu_file_set_error(f
);
2898 bytes_transferred
= 0;
2900 /* Make sure all dirty bits are set */
2901 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2902 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2903 cpu_physical_memory_set_dirty(addr
);
2906 /* Enable dirty memory tracking */
2907 cpu_physical_memory_set_dirty_tracking(1);
2909 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2912 bytes_transferred_last
= bytes_transferred
;
2913 bwidth
= qemu_get_clock_ns(rt_clock
);
2915 while (!qemu_file_rate_limit(f
)) {
2918 ret
= ram_save_block(f
);
2919 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2920 if (ret
== 0) /* no more blocks */
2924 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
2925 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2927 /* if we haven't transferred anything this round, force expected_time to a
2928 * a very high value, but without crashing */
2932 /* try transferring iterative blocks of memory */
2934 /* flush all remaining blocks regardless of rate limiting */
2935 while (ram_save_block(f
) != 0) {
2936 bytes_transferred
+= TARGET_PAGE_SIZE
;
2938 cpu_physical_memory_set_dirty_tracking(0);
2941 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2943 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2945 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2948 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2953 if (version_id
!= 3)
2957 addr
= qemu_get_be64(f
);
2959 flags
= addr
& ~TARGET_PAGE_MASK
;
2960 addr
&= TARGET_PAGE_MASK
;
2962 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2963 if (addr
!= last_ram_offset
)
2967 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2968 uint8_t ch
= qemu_get_byte(f
);
2969 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2972 (!kvm_enabled() || kvm_has_sync_mmu())) {
2973 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2976 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2977 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2979 if (qemu_file_has_error(f
)) {
2982 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2987 void qemu_service_io(void)
2989 qemu_notify_event();
2992 /***********************************************************/
2993 /* machine registration */
2995 static QEMUMachine
*first_machine
= NULL
;
2996 QEMUMachine
*current_machine
= NULL
;
2998 int qemu_register_machine(QEMUMachine
*m
)
3001 pm
= &first_machine
;
3009 static QEMUMachine
*find_machine(const char *name
)
3013 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3014 if (!strcmp(m
->name
, name
))
3016 if (m
->alias
&& !strcmp(m
->alias
, name
))
3022 static QEMUMachine
*find_default_machine(void)
3026 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3027 if (m
->is_default
) {
3034 /***********************************************************/
3035 /* main execution loop */
3037 static void gui_update(void *opaque
)
3039 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3040 DisplayState
*ds
= opaque
;
3041 DisplayChangeListener
*dcl
= ds
->listeners
;
3043 qemu_flush_coalesced_mmio_buffer();
3046 while (dcl
!= NULL
) {
3047 if (dcl
->gui_timer_interval
&&
3048 dcl
->gui_timer_interval
< interval
)
3049 interval
= dcl
->gui_timer_interval
;
3052 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3055 static void nographic_update(void *opaque
)
3057 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3059 qemu_flush_coalesced_mmio_buffer();
3060 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3063 void cpu_synchronize_all_states(void)
3067 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3068 cpu_synchronize_state(cpu
);
3072 void cpu_synchronize_all_post_reset(void)
3076 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3077 cpu_synchronize_post_reset(cpu
);
3081 void cpu_synchronize_all_post_init(void)
3085 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3086 cpu_synchronize_post_init(cpu
);
3090 struct vm_change_state_entry
{
3091 VMChangeStateHandler
*cb
;
3093 QLIST_ENTRY (vm_change_state_entry
) entries
;
3096 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3098 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3101 VMChangeStateEntry
*e
;
3103 e
= qemu_mallocz(sizeof (*e
));
3107 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3111 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3113 QLIST_REMOVE (e
, entries
);
3117 static void vm_state_notify(int running
, int reason
)
3119 VMChangeStateEntry
*e
;
3121 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3122 e
->cb(e
->opaque
, running
, reason
);
3126 static void resume_all_vcpus(void);
3127 static void pause_all_vcpus(void);
3134 vm_state_notify(1, 0);
3139 /* reset/shutdown handler */
3141 typedef struct QEMUResetEntry
{
3142 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3143 QEMUResetHandler
*func
;
3147 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3148 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3149 static int reset_requested
;
3150 static int shutdown_requested
;
3151 static int powerdown_requested
;
3152 static int debug_requested
;
3153 static int vmstop_requested
;
3155 int qemu_shutdown_requested(void)
3157 int r
= shutdown_requested
;
3158 shutdown_requested
= 0;
3162 int qemu_reset_requested(void)
3164 int r
= reset_requested
;
3165 reset_requested
= 0;
3169 int qemu_powerdown_requested(void)
3171 int r
= powerdown_requested
;
3172 powerdown_requested
= 0;
3176 static int qemu_debug_requested(void)
3178 int r
= debug_requested
;
3179 debug_requested
= 0;
3183 static int qemu_vmstop_requested(void)
3185 int r
= vmstop_requested
;
3186 vmstop_requested
= 0;
3190 static void do_vm_stop(int reason
)
3193 cpu_disable_ticks();
3196 vm_state_notify(0, reason
);
3197 monitor_protocol_event(QEVENT_STOP
, NULL
);
3201 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3203 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3206 re
->opaque
= opaque
;
3207 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3210 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3214 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3215 if (re
->func
== func
&& re
->opaque
== opaque
) {
3216 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3223 void qemu_system_reset(void)
3225 QEMUResetEntry
*re
, *nre
;
3227 /* reset all devices */
3228 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3229 re
->func(re
->opaque
);
3231 monitor_protocol_event(QEVENT_RESET
, NULL
);
3232 cpu_synchronize_all_post_reset();
3235 void qemu_system_reset_request(void)
3238 shutdown_requested
= 1;
3240 reset_requested
= 1;
3242 qemu_notify_event();
3245 void qemu_system_shutdown_request(void)
3247 shutdown_requested
= 1;
3248 qemu_notify_event();
3251 void qemu_system_powerdown_request(void)
3253 powerdown_requested
= 1;
3254 qemu_notify_event();
3257 #ifdef CONFIG_IOTHREAD
3258 static void qemu_system_vmstop_request(int reason
)
3260 vmstop_requested
= reason
;
3261 qemu_notify_event();
3266 static int io_thread_fd
= -1;
3268 static void qemu_event_increment(void)
3270 /* Write 8 bytes to be compatible with eventfd. */
3271 static uint64_t val
= 1;
3274 if (io_thread_fd
== -1)
3278 ret
= write(io_thread_fd
, &val
, sizeof(val
));
3279 } while (ret
< 0 && errno
== EINTR
);
3281 /* EAGAIN is fine, a read must be pending. */
3282 if (ret
< 0 && errno
!= EAGAIN
) {
3283 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3289 static void qemu_event_read(void *opaque
)
3291 int fd
= (unsigned long)opaque
;
3295 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
3297 len
= read(fd
, buffer
, sizeof(buffer
));
3298 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
3301 static int qemu_event_init(void)
3306 err
= qemu_eventfd(fds
);
3310 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3314 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3318 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3319 (void *)(unsigned long)fds
[0]);
3321 io_thread_fd
= fds
[1];
3330 HANDLE qemu_event_handle
;
3332 static void dummy_event_handler(void *opaque
)
3336 static int qemu_event_init(void)
3338 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3339 if (!qemu_event_handle
) {
3340 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3343 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3347 static void qemu_event_increment(void)
3349 if (!SetEvent(qemu_event_handle
)) {
3350 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3357 static int cpu_can_run(CPUState
*env
)
3368 #ifndef CONFIG_IOTHREAD
3369 static int qemu_init_main_loop(void)
3371 return qemu_event_init();
3374 void qemu_init_vcpu(void *_env
)
3376 CPUState
*env
= _env
;
3378 env
->nr_cores
= smp_cores
;
3379 env
->nr_threads
= smp_threads
;
3385 int qemu_cpu_self(void *env
)
3390 static void resume_all_vcpus(void)
3394 static void pause_all_vcpus(void)
3398 void qemu_cpu_kick(void *env
)
3403 void qemu_notify_event(void)
3405 CPUState
*env
= cpu_single_env
;
3407 qemu_event_increment ();
3411 if (next_cpu
&& env
!= next_cpu
) {
3416 void qemu_mutex_lock_iothread(void) {}
3417 void qemu_mutex_unlock_iothread(void) {}
3419 void vm_stop(int reason
)
3424 #else /* CONFIG_IOTHREAD */
3426 #include "qemu-thread.h"
3428 QemuMutex qemu_global_mutex
;
3429 static QemuMutex qemu_fair_mutex
;
3431 static QemuThread io_thread
;
3433 static QemuThread
*tcg_cpu_thread
;
3434 static QemuCond
*tcg_halt_cond
;
3436 static int qemu_system_ready
;
3438 static QemuCond qemu_cpu_cond
;
3440 static QemuCond qemu_system_cond
;
3441 static QemuCond qemu_pause_cond
;
3443 static void tcg_block_io_signals(void);
3444 static void kvm_block_io_signals(CPUState
*env
);
3445 static void unblock_io_signals(void);
3446 static int tcg_has_work(void);
3447 static int cpu_has_work(CPUState
*env
);
3449 static int qemu_init_main_loop(void)
3453 ret
= qemu_event_init();
3457 qemu_cond_init(&qemu_pause_cond
);
3458 qemu_mutex_init(&qemu_fair_mutex
);
3459 qemu_mutex_init(&qemu_global_mutex
);
3460 qemu_mutex_lock(&qemu_global_mutex
);
3462 unblock_io_signals();
3463 qemu_thread_self(&io_thread
);
3468 static void qemu_wait_io_event_common(CPUState
*env
)
3473 qemu_cond_signal(&qemu_pause_cond
);
3477 static void qemu_wait_io_event(CPUState
*env
)
3479 while (!tcg_has_work())
3480 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3482 qemu_mutex_unlock(&qemu_global_mutex
);
3485 * Users of qemu_global_mutex can be starved, having no chance
3486 * to acquire it since this path will get to it first.
3487 * So use another lock to provide fairness.
3489 qemu_mutex_lock(&qemu_fair_mutex
);
3490 qemu_mutex_unlock(&qemu_fair_mutex
);
3492 qemu_mutex_lock(&qemu_global_mutex
);
3493 qemu_wait_io_event_common(env
);
3496 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
3503 ts
.tv_sec
= timeout
/ 1000;
3504 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
3506 sigemptyset(&waitset
);
3507 sigaddset(&waitset
, SIG_IPI
);
3509 qemu_mutex_unlock(&qemu_global_mutex
);
3510 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
3512 qemu_mutex_lock(&qemu_global_mutex
);
3514 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
3515 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
3520 static void qemu_kvm_wait_io_event(CPUState
*env
)
3522 while (!cpu_has_work(env
))
3523 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3525 qemu_kvm_eat_signal(env
, 0);
3526 qemu_wait_io_event_common(env
);
3529 static int qemu_cpu_exec(CPUState
*env
);
3531 static void *kvm_cpu_thread_fn(void *arg
)
3533 CPUState
*env
= arg
;
3535 qemu_thread_self(env
->thread
);
3539 kvm_block_io_signals(env
);
3541 /* signal CPU creation */
3542 qemu_mutex_lock(&qemu_global_mutex
);
3544 qemu_cond_signal(&qemu_cpu_cond
);
3546 /* and wait for machine initialization */
3547 while (!qemu_system_ready
)
3548 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3551 if (cpu_can_run(env
))
3553 qemu_kvm_wait_io_event(env
);
3559 static void tcg_cpu_exec(void);
3561 static void *tcg_cpu_thread_fn(void *arg
)
3563 CPUState
*env
= arg
;
3565 tcg_block_io_signals();
3566 qemu_thread_self(env
->thread
);
3568 /* signal CPU creation */
3569 qemu_mutex_lock(&qemu_global_mutex
);
3570 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3572 qemu_cond_signal(&qemu_cpu_cond
);
3574 /* and wait for machine initialization */
3575 while (!qemu_system_ready
)
3576 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3580 qemu_wait_io_event(cur_cpu
);
3586 void qemu_cpu_kick(void *_env
)
3588 CPUState
*env
= _env
;
3589 qemu_cond_broadcast(env
->halt_cond
);
3591 qemu_thread_signal(env
->thread
, SIG_IPI
);
3594 int qemu_cpu_self(void *_env
)
3596 CPUState
*env
= _env
;
3599 qemu_thread_self(&this);
3601 return qemu_thread_equal(&this, env
->thread
);
3604 static void cpu_signal(int sig
)
3607 cpu_exit(cpu_single_env
);
3610 static void tcg_block_io_signals(void)
3613 struct sigaction sigact
;
3616 sigaddset(&set
, SIGUSR2
);
3617 sigaddset(&set
, SIGIO
);
3618 sigaddset(&set
, SIGALRM
);
3619 sigaddset(&set
, SIGCHLD
);
3620 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3623 sigaddset(&set
, SIG_IPI
);
3624 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3626 memset(&sigact
, 0, sizeof(sigact
));
3627 sigact
.sa_handler
= cpu_signal
;
3628 sigaction(SIG_IPI
, &sigact
, NULL
);
3631 static void dummy_signal(int sig
)
3635 static void kvm_block_io_signals(CPUState
*env
)
3639 struct sigaction sigact
;
3642 sigaddset(&set
, SIGUSR2
);
3643 sigaddset(&set
, SIGIO
);
3644 sigaddset(&set
, SIGALRM
);
3645 sigaddset(&set
, SIGCHLD
);
3646 sigaddset(&set
, SIG_IPI
);
3647 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3649 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
3650 sigdelset(&set
, SIG_IPI
);
3652 memset(&sigact
, 0, sizeof(sigact
));
3653 sigact
.sa_handler
= dummy_signal
;
3654 sigaction(SIG_IPI
, &sigact
, NULL
);
3656 r
= kvm_set_signal_mask(env
, &set
);
3658 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
3663 static void unblock_io_signals(void)
3668 sigaddset(&set
, SIGUSR2
);
3669 sigaddset(&set
, SIGIO
);
3670 sigaddset(&set
, SIGALRM
);
3671 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3674 sigaddset(&set
, SIG_IPI
);
3675 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3678 static void qemu_signal_lock(unsigned int msecs
)
3680 qemu_mutex_lock(&qemu_fair_mutex
);
3682 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3683 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
3684 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3687 qemu_mutex_unlock(&qemu_fair_mutex
);
3690 void qemu_mutex_lock_iothread(void)
3692 if (kvm_enabled()) {
3693 qemu_mutex_lock(&qemu_fair_mutex
);
3694 qemu_mutex_lock(&qemu_global_mutex
);
3695 qemu_mutex_unlock(&qemu_fair_mutex
);
3697 qemu_signal_lock(100);
3700 void qemu_mutex_unlock_iothread(void)
3702 qemu_mutex_unlock(&qemu_global_mutex
);
3705 static int all_vcpus_paused(void)
3707 CPUState
*penv
= first_cpu
;
3712 penv
= (CPUState
*)penv
->next_cpu
;
3718 static void pause_all_vcpus(void)
3720 CPUState
*penv
= first_cpu
;
3724 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3725 qemu_cpu_kick(penv
);
3726 penv
= (CPUState
*)penv
->next_cpu
;
3729 while (!all_vcpus_paused()) {
3730 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3733 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3734 penv
= (CPUState
*)penv
->next_cpu
;
3739 static void resume_all_vcpus(void)
3741 CPUState
*penv
= first_cpu
;
3746 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3747 qemu_cpu_kick(penv
);
3748 penv
= (CPUState
*)penv
->next_cpu
;
3752 static void tcg_init_vcpu(void *_env
)
3754 CPUState
*env
= _env
;
3755 /* share a single thread for all cpus with TCG */
3756 if (!tcg_cpu_thread
) {
3757 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3758 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3759 qemu_cond_init(env
->halt_cond
);
3760 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3761 while (env
->created
== 0)
3762 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3763 tcg_cpu_thread
= env
->thread
;
3764 tcg_halt_cond
= env
->halt_cond
;
3766 env
->thread
= tcg_cpu_thread
;
3767 env
->halt_cond
= tcg_halt_cond
;
3771 static void kvm_start_vcpu(CPUState
*env
)
3773 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3774 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3775 qemu_cond_init(env
->halt_cond
);
3776 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3777 while (env
->created
== 0)
3778 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3781 void qemu_init_vcpu(void *_env
)
3783 CPUState
*env
= _env
;
3785 env
->nr_cores
= smp_cores
;
3786 env
->nr_threads
= smp_threads
;
3788 kvm_start_vcpu(env
);
3793 void qemu_notify_event(void)
3795 qemu_event_increment();
3798 void vm_stop(int reason
)
3801 qemu_thread_self(&me
);
3803 if (!qemu_thread_equal(&me
, &io_thread
)) {
3804 qemu_system_vmstop_request(reason
);
3806 * FIXME: should not return to device code in case
3807 * vm_stop() has been requested.
3809 if (cpu_single_env
) {
3810 cpu_exit(cpu_single_env
);
3811 cpu_single_env
->stop
= 1;
3822 static void host_main_loop_wait(int *timeout
)
3828 /* XXX: need to suppress polling by better using win32 events */
3830 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3831 ret
|= pe
->func(pe
->opaque
);
3835 WaitObjects
*w
= &wait_objects
;
3837 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3838 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3839 if (w
->func
[ret
- WAIT_OBJECT_0
])
3840 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3842 /* Check for additional signaled events */
3843 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3845 /* Check if event is signaled */
3846 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3847 if(ret2
== WAIT_OBJECT_0
) {
3849 w
->func
[i
](w
->opaque
[i
]);
3850 } else if (ret2
== WAIT_TIMEOUT
) {
3852 err
= GetLastError();
3853 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3856 } else if (ret
== WAIT_TIMEOUT
) {
3858 err
= GetLastError();
3859 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3866 static void host_main_loop_wait(int *timeout
)
3871 void main_loop_wait(int timeout
)
3873 IOHandlerRecord
*ioh
;
3874 fd_set rfds
, wfds
, xfds
;
3878 qemu_bh_update_timeout(&timeout
);
3880 host_main_loop_wait(&timeout
);
3882 /* poll any events */
3883 /* XXX: separate device handlers from system ones */
3888 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3892 (!ioh
->fd_read_poll
||
3893 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3894 FD_SET(ioh
->fd
, &rfds
);
3898 if (ioh
->fd_write
) {
3899 FD_SET(ioh
->fd
, &wfds
);
3905 tv
.tv_sec
= timeout
/ 1000;
3906 tv
.tv_usec
= (timeout
% 1000) * 1000;
3908 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3910 qemu_mutex_unlock_iothread();
3911 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3912 qemu_mutex_lock_iothread();
3914 IOHandlerRecord
**pioh
;
3916 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3917 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3918 ioh
->fd_read(ioh
->opaque
);
3920 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3921 ioh
->fd_write(ioh
->opaque
);
3925 /* remove deleted IO handlers */
3926 pioh
= &first_io_handler
;
3937 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3939 qemu_run_all_timers();
3941 /* Check bottom-halves last in case any of the earlier events triggered
3947 static int qemu_cpu_exec(CPUState
*env
)
3950 #ifdef CONFIG_PROFILER
3954 #ifdef CONFIG_PROFILER
3955 ti
= profile_getclock();
3960 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3961 env
->icount_decr
.u16
.low
= 0;
3962 env
->icount_extra
= 0;
3963 count
= qemu_next_deadline();
3964 count
= (count
+ (1 << icount_time_shift
) - 1)
3965 >> icount_time_shift
;
3966 qemu_icount
+= count
;
3967 decr
= (count
> 0xffff) ? 0xffff : count
;
3969 env
->icount_decr
.u16
.low
= decr
;
3970 env
->icount_extra
= count
;
3972 ret
= cpu_exec(env
);
3973 #ifdef CONFIG_PROFILER
3974 qemu_time
+= profile_getclock() - ti
;
3977 /* Fold pending instructions back into the
3978 instruction counter, and clear the interrupt flag. */
3979 qemu_icount
-= (env
->icount_decr
.u16
.low
3980 + env
->icount_extra
);
3981 env
->icount_decr
.u32
= 0;
3982 env
->icount_extra
= 0;
3987 static void tcg_cpu_exec(void)
3991 if (next_cpu
== NULL
)
3992 next_cpu
= first_cpu
;
3993 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3994 CPUState
*env
= cur_cpu
= next_cpu
;
3996 qemu_clock_enable(vm_clock
,
3997 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
3999 if (alarm_timer
->pending
)
4001 if (cpu_can_run(env
))
4002 ret
= qemu_cpu_exec(env
);
4006 if (ret
== EXCP_DEBUG
) {
4007 gdb_set_stop_cpu(env
);
4008 debug_requested
= 1;
4014 static int cpu_has_work(CPUState
*env
)
4022 if (qemu_cpu_has_work(env
))
4027 static int tcg_has_work(void)
4031 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4032 if (cpu_has_work(env
))
4037 static int qemu_calculate_timeout(void)
4039 #ifndef CONFIG_IOTHREAD
4044 else if (tcg_has_work())
4046 else if (!use_icount
)
4049 /* XXX: use timeout computed from timers */
4052 /* Advance virtual time to the next event. */
4053 if (use_icount
== 1) {
4054 /* When not using an adaptive execution frequency
4055 we tend to get badly out of sync with real time,
4056 so just delay for a reasonable amount of time. */
4059 delta
= cpu_get_icount() - cpu_get_clock();
4062 /* If virtual time is ahead of real time then just
4064 timeout
= (delta
/ 1000000) + 1;
4066 /* Wait for either IO to occur or the next
4068 add
= qemu_next_deadline();
4069 /* We advance the timer before checking for IO.
4070 Limit the amount we advance so that early IO
4071 activity won't get the guest too far ahead. */
4075 add
= (add
+ (1 << icount_time_shift
) - 1)
4076 >> icount_time_shift
;
4078 timeout
= delta
/ 1000000;
4085 #else /* CONFIG_IOTHREAD */
4090 static int vm_can_run(void)
4092 if (powerdown_requested
)
4094 if (reset_requested
)
4096 if (shutdown_requested
)
4098 if (debug_requested
)
4103 qemu_irq qemu_system_powerdown
;
4105 static void main_loop(void)
4109 #ifdef CONFIG_IOTHREAD
4110 qemu_system_ready
= 1;
4111 qemu_cond_broadcast(&qemu_system_cond
);
4116 #ifdef CONFIG_PROFILER
4119 #ifndef CONFIG_IOTHREAD
4122 #ifdef CONFIG_PROFILER
4123 ti
= profile_getclock();
4125 main_loop_wait(qemu_calculate_timeout());
4126 #ifdef CONFIG_PROFILER
4127 dev_time
+= profile_getclock() - ti
;
4129 } while (vm_can_run());
4131 if (qemu_debug_requested()) {
4132 vm_stop(EXCP_DEBUG
);
4134 if (qemu_shutdown_requested()) {
4135 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4142 if (qemu_reset_requested()) {
4144 qemu_system_reset();
4147 if (qemu_powerdown_requested()) {
4148 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4149 qemu_irq_raise(qemu_system_powerdown
);
4151 if ((r
= qemu_vmstop_requested())) {
4158 static void version(void)
4160 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4163 static void help(int exitcode
)
4165 const char *options_help
=
4166 #define DEF(option, opt_arg, opt_enum, opt_help) \
4168 #define DEFHEADING(text) stringify(text) "\n"
4169 #include "qemu-options.h"
4175 printf("usage: %s [options] [disk_image]\n"
4177 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4180 "During emulation, the following keys are useful:\n"
4181 "ctrl-alt-f toggle full screen\n"
4182 "ctrl-alt-n switch to virtual console 'n'\n"
4183 "ctrl-alt toggle mouse and keyboard grab\n"
4185 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4191 #define HAS_ARG 0x0001
4194 #define DEF(option, opt_arg, opt_enum, opt_help) \
4196 #define DEFHEADING(text)
4197 #include "qemu-options.h"
4203 typedef struct QEMUOption
{
4209 static const QEMUOption qemu_options
[] = {
4210 { "h", 0, QEMU_OPTION_h
},
4211 #define DEF(option, opt_arg, opt_enum, opt_help) \
4212 { option, opt_arg, opt_enum },
4213 #define DEFHEADING(text)
4214 #include "qemu-options.h"
4222 struct soundhw soundhw
[] = {
4223 #ifdef HAS_AUDIO_CHOICE
4224 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4230 { .init_isa
= pcspk_audio_init
}
4237 "Creative Sound Blaster 16",
4240 { .init_isa
= SB16_init
}
4244 #ifdef CONFIG_CS4231A
4250 { .init_isa
= cs4231a_init
}
4258 "Yamaha YMF262 (OPL3)",
4260 "Yamaha YM3812 (OPL2)",
4264 { .init_isa
= Adlib_init
}
4271 "Gravis Ultrasound GF1",
4274 { .init_isa
= GUS_init
}
4281 "Intel 82801AA AC97 Audio",
4284 { .init_pci
= ac97_init
}
4288 #ifdef CONFIG_ES1370
4291 "ENSONIQ AudioPCI ES1370",
4294 { .init_pci
= es1370_init
}
4298 #endif /* HAS_AUDIO_CHOICE */
4300 { NULL
, NULL
, 0, 0, { NULL
} }
4303 static void select_soundhw (const char *optarg
)
4307 if (*optarg
== '?') {
4310 printf ("Valid sound card names (comma separated):\n");
4311 for (c
= soundhw
; c
->name
; ++c
) {
4312 printf ("%-11s %s\n", c
->name
, c
->descr
);
4314 printf ("\n-soundhw all will enable all of the above\n");
4315 exit (*optarg
!= '?');
4323 if (!strcmp (optarg
, "all")) {
4324 for (c
= soundhw
; c
->name
; ++c
) {
4332 e
= strchr (p
, ',');
4333 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4335 for (c
= soundhw
; c
->name
; ++c
) {
4336 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4345 "Unknown sound card name (too big to show)\n");
4348 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4353 p
+= l
+ (e
!= NULL
);
4357 goto show_valid_cards
;
4362 static void select_vgahw (const char *p
)
4367 vga_interface_type
= VGA_NONE
;
4368 if (strstart(p
, "std", &opts
)) {
4369 vga_interface_type
= VGA_STD
;
4370 } else if (strstart(p
, "cirrus", &opts
)) {
4371 vga_interface_type
= VGA_CIRRUS
;
4372 } else if (strstart(p
, "vmware", &opts
)) {
4373 vga_interface_type
= VGA_VMWARE
;
4374 } else if (strstart(p
, "xenfb", &opts
)) {
4375 vga_interface_type
= VGA_XENFB
;
4376 } else if (!strstart(p
, "none", &opts
)) {
4378 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4382 const char *nextopt
;
4384 if (strstart(opts
, ",retrace=", &nextopt
)) {
4386 if (strstart(opts
, "dumb", &nextopt
))
4387 vga_retrace_method
= VGA_RETRACE_DUMB
;
4388 else if (strstart(opts
, "precise", &nextopt
))
4389 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4390 else goto invalid_vga
;
4391 } else goto invalid_vga
;
4397 static int balloon_parse(const char *arg
)
4401 if (strcmp(arg
, "none") == 0) {
4405 if (!strncmp(arg
, "virtio", 6)) {
4406 if (arg
[6] == ',') {
4407 /* have params -> parse them */
4408 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
4412 /* create empty opts */
4413 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4415 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4424 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4426 exit(STATUS_CONTROL_C_EXIT
);
4431 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4435 if(strlen(str
) != 36)
4438 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4439 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4440 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4446 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4454 static void termsig_handler(int signal
)
4456 qemu_system_shutdown_request();
4459 static void sigchld_handler(int signal
)
4461 waitpid(-1, NULL
, WNOHANG
);
4464 static void sighandler_setup(void)
4466 struct sigaction act
;
4468 memset(&act
, 0, sizeof(act
));
4469 act
.sa_handler
= termsig_handler
;
4470 sigaction(SIGINT
, &act
, NULL
);
4471 sigaction(SIGHUP
, &act
, NULL
);
4472 sigaction(SIGTERM
, &act
, NULL
);
4474 act
.sa_handler
= sigchld_handler
;
4475 act
.sa_flags
= SA_NOCLDSTOP
;
4476 sigaction(SIGCHLD
, &act
, NULL
);
4482 /* Look for support files in the same directory as the executable. */
4483 static char *find_datadir(const char *argv0
)
4489 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4496 while (p
!= buf
&& *p
!= '\\')
4499 if (access(buf
, R_OK
) == 0) {
4500 return qemu_strdup(buf
);
4506 /* Find a likely location for support files using the location of the binary.
4507 For installed binaries this will be "$bindir/../share/qemu". When
4508 running from the build tree this will be "$bindir/../pc-bios". */
4509 #define SHARE_SUFFIX "/share/qemu"
4510 #define BUILD_SUFFIX "/pc-bios"
4511 static char *find_datadir(const char *argv0
)
4519 #if defined(__linux__)
4522 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4528 #elif defined(__FreeBSD__)
4531 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4538 /* If we don't have any way of figuring out the actual executable
4539 location then try argv[0]. */
4541 p
= realpath(argv0
, buf
);
4549 max_len
= strlen(dir
) +
4550 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4551 res
= qemu_mallocz(max_len
);
4552 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4553 if (access(res
, R_OK
)) {
4554 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4555 if (access(res
, R_OK
)) {
4567 char *qemu_find_file(int type
, const char *name
)
4573 /* If name contains path separators then try it as a straight path. */
4574 if ((strchr(name
, '/') || strchr(name
, '\\'))
4575 && access(name
, R_OK
) == 0) {
4576 return qemu_strdup(name
);
4579 case QEMU_FILE_TYPE_BIOS
:
4582 case QEMU_FILE_TYPE_KEYMAP
:
4583 subdir
= "keymaps/";
4588 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4589 buf
= qemu_mallocz(len
);
4590 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4591 if (access(buf
, R_OK
)) {
4598 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4600 return qdev_device_help(opts
);
4603 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4607 dev
= qdev_device_add(opts
);
4613 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4615 CharDriverState
*chr
;
4617 chr
= qemu_chr_open_opts(opts
, NULL
);
4623 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4625 CharDriverState
*chr
;
4626 const char *chardev
;
4630 mode
= qemu_opt_get(opts
, "mode");
4634 if (strcmp(mode
, "readline") == 0) {
4635 flags
= MONITOR_USE_READLINE
;
4636 } else if (strcmp(mode
, "control") == 0) {
4637 flags
= MONITOR_USE_CONTROL
;
4639 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4643 if (qemu_opt_get_bool(opts
, "default", 0))
4644 flags
|= MONITOR_IS_DEFAULT
;
4646 chardev
= qemu_opt_get(opts
, "chardev");
4647 chr
= qemu_chr_find(chardev
);
4649 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4653 monitor_init(chr
, flags
);
4657 static void monitor_parse(const char *optarg
, const char *mode
)
4659 static int monitor_device_index
= 0;
4665 if (strstart(optarg
, "chardev:", &p
)) {
4666 snprintf(label
, sizeof(label
), "%s", p
);
4668 if (monitor_device_index
) {
4669 snprintf(label
, sizeof(label
), "monitor%d",
4670 monitor_device_index
);
4672 snprintf(label
, sizeof(label
), "monitor");
4675 opts
= qemu_chr_parse_compat(label
, optarg
);
4677 fprintf(stderr
, "parse error: %s\n", optarg
);
4682 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4684 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4687 qemu_opt_set(opts
, "mode", mode
);
4688 qemu_opt_set(opts
, "chardev", label
);
4690 qemu_opt_set(opts
, "default", "on");
4691 monitor_device_index
++;
4694 struct device_config
{
4696 DEV_USB
, /* -usbdevice */
4698 DEV_SERIAL
, /* -serial */
4699 DEV_PARALLEL
, /* -parallel */
4700 DEV_VIRTCON
, /* -virtioconsole */
4701 DEV_DEBUGCON
, /* -debugcon */
4703 const char *cmdline
;
4704 QTAILQ_ENTRY(device_config
) next
;
4706 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4708 static void add_device_config(int type
, const char *cmdline
)
4710 struct device_config
*conf
;
4712 conf
= qemu_mallocz(sizeof(*conf
));
4714 conf
->cmdline
= cmdline
;
4715 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4718 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4720 struct device_config
*conf
;
4723 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4724 if (conf
->type
!= type
)
4726 rc
= func(conf
->cmdline
);
4733 static int serial_parse(const char *devname
)
4735 static int index
= 0;
4738 if (strcmp(devname
, "none") == 0)
4740 if (index
== MAX_SERIAL_PORTS
) {
4741 fprintf(stderr
, "qemu: too many serial ports\n");
4744 snprintf(label
, sizeof(label
), "serial%d", index
);
4745 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4746 if (!serial_hds
[index
]) {
4747 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4748 devname
, strerror(errno
));
4755 static int parallel_parse(const char *devname
)
4757 static int index
= 0;
4760 if (strcmp(devname
, "none") == 0)
4762 if (index
== MAX_PARALLEL_PORTS
) {
4763 fprintf(stderr
, "qemu: too many parallel ports\n");
4766 snprintf(label
, sizeof(label
), "parallel%d", index
);
4767 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4768 if (!parallel_hds
[index
]) {
4769 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4770 devname
, strerror(errno
));
4777 static int virtcon_parse(const char *devname
)
4779 static int index
= 0;
4781 QemuOpts
*bus_opts
, *dev_opts
;
4783 if (strcmp(devname
, "none") == 0)
4785 if (index
== MAX_VIRTIO_CONSOLES
) {
4786 fprintf(stderr
, "qemu: too many virtio consoles\n");
4790 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4791 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4793 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4794 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4796 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4797 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4798 if (!virtcon_hds
[index
]) {
4799 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4800 devname
, strerror(errno
));
4803 qemu_opt_set(dev_opts
, "chardev", label
);
4809 static int debugcon_parse(const char *devname
)
4813 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4816 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4818 fprintf(stderr
, "qemu: already have a debugcon device\n");
4821 qemu_opt_set(opts
, "driver", "isa-debugcon");
4822 qemu_opt_set(opts
, "chardev", "debugcon");
4826 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4827 const char **poptarg
, int *poptind
)
4829 const QEMUOption
*popt
;
4830 int optind
= *poptind
;
4831 char *r
= argv
[optind
];
4834 loc_set_cmdline(argv
, optind
, 1);
4836 /* Treat --foo the same as -foo. */
4839 popt
= qemu_options
;
4842 error_report("invalid option");
4845 if (!strcmp(popt
->name
, r
+ 1))
4849 if (popt
->flags
& HAS_ARG
) {
4850 if (optind
>= argc
) {
4851 error_report("requires an argument");
4854 optarg
= argv
[optind
++];
4855 loc_set_cmdline(argv
, optind
- 2, 2);
4866 int main(int argc
, char **argv
, char **envp
)
4868 const char *gdbstub_dev
= NULL
;
4869 uint32_t boot_devices_bitmap
= 0;
4871 int snapshot
, linux_boot
, net_boot
;
4872 const char *icount_option
= NULL
;
4873 const char *initrd_filename
;
4874 const char *kernel_filename
, *kernel_cmdline
;
4875 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4877 DisplayChangeListener
*dcl
;
4878 int cyls
, heads
, secs
, translation
;
4879 QemuOpts
*hda_opts
= NULL
, *opts
;
4882 const char *loadvm
= NULL
;
4883 QEMUMachine
*machine
;
4884 const char *cpu_model
;
4889 const char *pid_file
= NULL
;
4890 const char *incoming
= NULL
;
4893 struct passwd
*pwd
= NULL
;
4894 const char *chroot_dir
= NULL
;
4895 const char *run_as
= NULL
;
4898 int show_vnc_port
= 0;
4901 error_set_progname(argv
[0]);
4905 qemu_cache_utils_init(envp
);
4907 QLIST_INIT (&vm_change_state_head
);
4910 struct sigaction act
;
4911 sigfillset(&act
.sa_mask
);
4913 act
.sa_handler
= SIG_IGN
;
4914 sigaction(SIGPIPE
, &act
, NULL
);
4917 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4918 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4919 QEMU to run on a single CPU */
4924 h
= GetCurrentProcess();
4925 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4926 for(i
= 0; i
< 32; i
++) {
4927 if (mask
& (1 << i
))
4932 SetProcessAffinityMask(h
, mask
);
4938 module_call_init(MODULE_INIT_MACHINE
);
4939 machine
= find_default_machine();
4941 initrd_filename
= NULL
;
4944 kernel_filename
= NULL
;
4945 kernel_cmdline
= "";
4946 cyls
= heads
= secs
= 0;
4947 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4949 for (i
= 0; i
< MAX_NODES
; i
++) {
4951 node_cpumask
[i
] = 0;
4960 /* first pass of option parsing */
4962 while (optind
< argc
) {
4963 if (argv
[optind
][0] != '-') {
4968 const QEMUOption
*popt
;
4970 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4971 switch (popt
->index
) {
4972 case QEMU_OPTION_nodefconfig
:
4983 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
4984 fp
= fopen(fname
, "r");
4986 if (qemu_config_parse(fp
, fname
) != 0) {
4992 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
4993 fp
= fopen(fname
, "r");
4995 if (qemu_config_parse(fp
, fname
) != 0) {
5001 #if defined(cpudef_setup)
5002 cpudef_setup(); /* parse cpu definitions in target config file */
5005 /* second pass of option parsing */
5010 if (argv
[optind
][0] != '-') {
5011 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
5013 const QEMUOption
*popt
;
5015 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
5016 switch(popt
->index
) {
5018 machine
= find_machine(optarg
);
5021 printf("Supported machines are:\n");
5022 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5024 printf("%-10s %s (alias of %s)\n",
5025 m
->alias
, m
->desc
, m
->name
);
5026 printf("%-10s %s%s\n",
5028 m
->is_default
? " (default)" : "");
5030 exit(*optarg
!= '?');
5033 case QEMU_OPTION_cpu
:
5034 /* hw initialization will check this */
5035 if (*optarg
== '?') {
5036 /* XXX: implement xxx_cpu_list for targets that still miss it */
5037 #if defined(cpu_list_id)
5038 cpu_list_id(stdout
, &fprintf
, optarg
);
5039 #elif defined(cpu_list)
5040 cpu_list(stdout
, &fprintf
); /* deprecated */
5047 case QEMU_OPTION_initrd
:
5048 initrd_filename
= optarg
;
5050 case QEMU_OPTION_hda
:
5052 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5054 hda_opts
= drive_add(optarg
, HD_ALIAS
5055 ",cyls=%d,heads=%d,secs=%d%s",
5056 0, cyls
, heads
, secs
,
5057 translation
== BIOS_ATA_TRANSLATION_LBA
?
5059 translation
== BIOS_ATA_TRANSLATION_NONE
?
5060 ",trans=none" : "");
5062 case QEMU_OPTION_hdb
:
5063 case QEMU_OPTION_hdc
:
5064 case QEMU_OPTION_hdd
:
5065 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5067 case QEMU_OPTION_drive
:
5068 drive_add(NULL
, "%s", optarg
);
5070 case QEMU_OPTION_set
:
5071 if (qemu_set_option(optarg
) != 0)
5074 case QEMU_OPTION_global
:
5075 if (qemu_global_option(optarg
) != 0)
5078 case QEMU_OPTION_mtdblock
:
5079 drive_add(optarg
, MTD_ALIAS
);
5081 case QEMU_OPTION_sd
:
5082 drive_add(optarg
, SD_ALIAS
);
5084 case QEMU_OPTION_pflash
:
5085 drive_add(optarg
, PFLASH_ALIAS
);
5087 case QEMU_OPTION_snapshot
:
5090 case QEMU_OPTION_hdachs
:
5094 cyls
= strtol(p
, (char **)&p
, 0);
5095 if (cyls
< 1 || cyls
> 16383)
5100 heads
= strtol(p
, (char **)&p
, 0);
5101 if (heads
< 1 || heads
> 16)
5106 secs
= strtol(p
, (char **)&p
, 0);
5107 if (secs
< 1 || secs
> 63)
5111 if (!strcmp(p
, "none"))
5112 translation
= BIOS_ATA_TRANSLATION_NONE
;
5113 else if (!strcmp(p
, "lba"))
5114 translation
= BIOS_ATA_TRANSLATION_LBA
;
5115 else if (!strcmp(p
, "auto"))
5116 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5119 } else if (*p
!= '\0') {
5121 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5124 if (hda_opts
!= NULL
) {
5126 snprintf(num
, sizeof(num
), "%d", cyls
);
5127 qemu_opt_set(hda_opts
, "cyls", num
);
5128 snprintf(num
, sizeof(num
), "%d", heads
);
5129 qemu_opt_set(hda_opts
, "heads", num
);
5130 snprintf(num
, sizeof(num
), "%d", secs
);
5131 qemu_opt_set(hda_opts
, "secs", num
);
5132 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5133 qemu_opt_set(hda_opts
, "trans", "lba");
5134 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5135 qemu_opt_set(hda_opts
, "trans", "none");
5139 case QEMU_OPTION_numa
:
5140 if (nb_numa_nodes
>= MAX_NODES
) {
5141 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5146 case QEMU_OPTION_nographic
:
5147 display_type
= DT_NOGRAPHIC
;
5149 #ifdef CONFIG_CURSES
5150 case QEMU_OPTION_curses
:
5151 display_type
= DT_CURSES
;
5154 case QEMU_OPTION_portrait
:
5157 case QEMU_OPTION_kernel
:
5158 kernel_filename
= optarg
;
5160 case QEMU_OPTION_append
:
5161 kernel_cmdline
= optarg
;
5163 case QEMU_OPTION_cdrom
:
5164 drive_add(optarg
, CDROM_ALIAS
);
5166 case QEMU_OPTION_boot
:
5168 static const char * const params
[] = {
5169 "order", "once", "menu", NULL
5171 char buf
[sizeof(boot_devices
)];
5172 char *standard_boot_devices
;
5175 if (!strchr(optarg
, '=')) {
5177 pstrcpy(buf
, sizeof(buf
), optarg
);
5178 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5180 "qemu: unknown boot parameter '%s' in '%s'\n",
5186 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5187 boot_devices_bitmap
= parse_bootdevices(buf
);
5188 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5191 if (get_param_value(buf
, sizeof(buf
),
5193 boot_devices_bitmap
|= parse_bootdevices(buf
);
5194 standard_boot_devices
= qemu_strdup(boot_devices
);
5195 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5196 qemu_register_reset(restore_boot_devices
,
5197 standard_boot_devices
);
5199 if (get_param_value(buf
, sizeof(buf
),
5201 if (!strcmp(buf
, "on")) {
5203 } else if (!strcmp(buf
, "off")) {
5207 "qemu: invalid option value '%s'\n",
5215 case QEMU_OPTION_fda
:
5216 case QEMU_OPTION_fdb
:
5217 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5220 case QEMU_OPTION_no_fd_bootchk
:
5224 case QEMU_OPTION_netdev
:
5225 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5229 case QEMU_OPTION_net
:
5230 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5235 case QEMU_OPTION_tftp
:
5236 legacy_tftp_prefix
= optarg
;
5238 case QEMU_OPTION_bootp
:
5239 legacy_bootp_filename
= optarg
;
5242 case QEMU_OPTION_smb
:
5243 if (net_slirp_smb(optarg
) < 0)
5247 case QEMU_OPTION_redir
:
5248 if (net_slirp_redir(optarg
) < 0)
5252 case QEMU_OPTION_bt
:
5253 add_device_config(DEV_BT
, optarg
);
5256 case QEMU_OPTION_audio_help
:
5260 case QEMU_OPTION_soundhw
:
5261 select_soundhw (optarg
);
5267 case QEMU_OPTION_version
:
5271 case QEMU_OPTION_m
: {
5275 value
= strtoul(optarg
, &ptr
, 10);
5277 case 0: case 'M': case 'm':
5284 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5288 /* On 32-bit hosts, QEMU is limited by virtual address space */
5289 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5290 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5293 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5294 fprintf(stderr
, "qemu: ram size too large\n");
5300 case QEMU_OPTION_mempath
:
5304 case QEMU_OPTION_mem_prealloc
:
5311 const CPULogItem
*item
;
5313 mask
= cpu_str_to_log_mask(optarg
);
5315 printf("Log items (comma separated):\n");
5316 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5317 printf("%-10s %s\n", item
->name
, item
->help
);
5325 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5327 case QEMU_OPTION_gdb
:
5328 gdbstub_dev
= optarg
;
5333 case QEMU_OPTION_bios
:
5336 case QEMU_OPTION_singlestep
:
5343 keyboard_layout
= optarg
;
5345 case QEMU_OPTION_localtime
:
5348 case QEMU_OPTION_vga
:
5349 select_vgahw (optarg
);
5351 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5357 w
= strtol(p
, (char **)&p
, 10);
5360 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5366 h
= strtol(p
, (char **)&p
, 10);
5371 depth
= strtol(p
, (char **)&p
, 10);
5372 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5373 depth
!= 24 && depth
!= 32)
5375 } else if (*p
== '\0') {
5376 depth
= graphic_depth
;
5383 graphic_depth
= depth
;
5387 case QEMU_OPTION_echr
:
5390 term_escape_char
= strtol(optarg
, &r
, 0);
5392 printf("Bad argument to echr\n");
5395 case QEMU_OPTION_monitor
:
5396 monitor_parse(optarg
, "readline");
5397 default_monitor
= 0;
5399 case QEMU_OPTION_qmp
:
5400 monitor_parse(optarg
, "control");
5401 default_monitor
= 0;
5403 case QEMU_OPTION_mon
:
5404 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
5406 fprintf(stderr
, "parse error: %s\n", optarg
);
5409 default_monitor
= 0;
5411 case QEMU_OPTION_chardev
:
5412 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
5414 fprintf(stderr
, "parse error: %s\n", optarg
);
5418 case QEMU_OPTION_serial
:
5419 add_device_config(DEV_SERIAL
, optarg
);
5421 if (strncmp(optarg
, "mon:", 4) == 0) {
5422 default_monitor
= 0;
5425 case QEMU_OPTION_watchdog
:
5428 "qemu: only one watchdog option may be given\n");
5433 case QEMU_OPTION_watchdog_action
:
5434 if (select_watchdog_action(optarg
) == -1) {
5435 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5439 case QEMU_OPTION_virtiocon
:
5440 add_device_config(DEV_VIRTCON
, optarg
);
5441 default_virtcon
= 0;
5442 if (strncmp(optarg
, "mon:", 4) == 0) {
5443 default_monitor
= 0;
5446 case QEMU_OPTION_parallel
:
5447 add_device_config(DEV_PARALLEL
, optarg
);
5448 default_parallel
= 0;
5449 if (strncmp(optarg
, "mon:", 4) == 0) {
5450 default_monitor
= 0;
5453 case QEMU_OPTION_debugcon
:
5454 add_device_config(DEV_DEBUGCON
, optarg
);
5456 case QEMU_OPTION_loadvm
:
5459 case QEMU_OPTION_full_screen
:
5463 case QEMU_OPTION_no_frame
:
5466 case QEMU_OPTION_alt_grab
:
5469 case QEMU_OPTION_ctrl_grab
:
5472 case QEMU_OPTION_no_quit
:
5475 case QEMU_OPTION_sdl
:
5476 display_type
= DT_SDL
;
5479 case QEMU_OPTION_pidfile
:
5483 case QEMU_OPTION_win2k_hack
:
5484 win2k_install_hack
= 1;
5486 case QEMU_OPTION_rtc_td_hack
:
5489 case QEMU_OPTION_acpitable
:
5490 if(acpi_table_add(optarg
) < 0) {
5491 fprintf(stderr
, "Wrong acpi table provided\n");
5495 case QEMU_OPTION_smbios
:
5496 if(smbios_entry_add(optarg
) < 0) {
5497 fprintf(stderr
, "Wrong smbios provided\n");
5503 case QEMU_OPTION_enable_kvm
:
5507 case QEMU_OPTION_usb
:
5510 case QEMU_OPTION_usbdevice
:
5512 add_device_config(DEV_USB
, optarg
);
5514 case QEMU_OPTION_device
:
5515 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
5519 case QEMU_OPTION_smp
:
5522 fprintf(stderr
, "Invalid number of CPUs\n");
5525 if (max_cpus
< smp_cpus
) {
5526 fprintf(stderr
, "maxcpus must be equal to or greater than "
5530 if (max_cpus
> 255) {
5531 fprintf(stderr
, "Unsupported number of maxcpus\n");
5535 case QEMU_OPTION_vnc
:
5536 display_type
= DT_VNC
;
5537 vnc_display
= optarg
;
5540 case QEMU_OPTION_no_acpi
:
5543 case QEMU_OPTION_no_hpet
:
5546 case QEMU_OPTION_balloon
:
5547 if (balloon_parse(optarg
) < 0) {
5548 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5553 case QEMU_OPTION_no_reboot
:
5556 case QEMU_OPTION_no_shutdown
:
5559 case QEMU_OPTION_show_cursor
:
5562 case QEMU_OPTION_uuid
:
5563 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5564 fprintf(stderr
, "Fail to parse UUID string."
5565 " Wrong format.\n");
5570 case QEMU_OPTION_daemonize
:
5574 case QEMU_OPTION_option_rom
:
5575 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5576 fprintf(stderr
, "Too many option ROMs\n");
5579 option_rom
[nb_option_roms
] = optarg
;
5582 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5583 case QEMU_OPTION_semihosting
:
5584 semihosting_enabled
= 1;
5587 case QEMU_OPTION_name
:
5588 qemu_name
= qemu_strdup(optarg
);
5590 char *p
= strchr(qemu_name
, ',');
5593 if (strncmp(p
, "process=", 8)) {
5594 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5602 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5603 case QEMU_OPTION_prom_env
:
5604 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5605 fprintf(stderr
, "Too many prom variables\n");
5608 prom_envs
[nb_prom_envs
] = optarg
;
5613 case QEMU_OPTION_old_param
:
5617 case QEMU_OPTION_clock
:
5618 configure_alarms(optarg
);
5620 case QEMU_OPTION_startdate
:
5621 configure_rtc_date_offset(optarg
, 1);
5623 case QEMU_OPTION_rtc
:
5624 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
5626 fprintf(stderr
, "parse error: %s\n", optarg
);
5629 configure_rtc(opts
);
5631 case QEMU_OPTION_tb_size
:
5632 tb_size
= strtol(optarg
, NULL
, 0);
5636 case QEMU_OPTION_icount
:
5637 icount_option
= optarg
;
5639 case QEMU_OPTION_incoming
:
5642 case QEMU_OPTION_nodefaults
:
5644 default_parallel
= 0;
5645 default_virtcon
= 0;
5646 default_monitor
= 0;
5654 case QEMU_OPTION_chroot
:
5655 chroot_dir
= optarg
;
5657 case QEMU_OPTION_runas
:
5662 case QEMU_OPTION_xen_domid
:
5663 xen_domid
= atoi(optarg
);
5665 case QEMU_OPTION_xen_create
:
5666 xen_mode
= XEN_CREATE
;
5668 case QEMU_OPTION_xen_attach
:
5669 xen_mode
= XEN_ATTACH
;
5672 case QEMU_OPTION_readconfig
:
5675 fp
= fopen(optarg
, "r");
5677 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5680 if (qemu_config_parse(fp
, optarg
) != 0) {
5686 case QEMU_OPTION_writeconfig
:
5689 if (strcmp(optarg
, "-") == 0) {
5692 fp
= fopen(optarg
, "w");
5694 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5698 qemu_config_write(fp
);
5707 /* If no data_dir is specified then try to find it relative to the
5710 data_dir
= find_datadir(argv
[0]);
5712 /* If all else fails use the install patch specified when building. */
5714 data_dir
= CONFIG_QEMU_SHAREDIR
;
5718 * Default to max_cpus = smp_cpus, in case the user doesn't
5719 * specify a max_cpus value.
5722 max_cpus
= smp_cpus
;
5724 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5725 if (smp_cpus
> machine
->max_cpus
) {
5726 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5727 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5732 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5733 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5735 if (machine
->no_serial
) {
5738 if (machine
->no_parallel
) {
5739 default_parallel
= 0;
5741 if (!machine
->use_virtcon
) {
5742 default_virtcon
= 0;
5744 if (machine
->no_vga
) {
5747 if (machine
->no_floppy
) {
5750 if (machine
->no_cdrom
) {
5753 if (machine
->no_sdcard
) {
5757 if (display_type
== DT_NOGRAPHIC
) {
5758 if (default_parallel
)
5759 add_device_config(DEV_PARALLEL
, "null");
5760 if (default_serial
&& default_monitor
) {
5761 add_device_config(DEV_SERIAL
, "mon:stdio");
5762 } else if (default_virtcon
&& default_monitor
) {
5763 add_device_config(DEV_VIRTCON
, "mon:stdio");
5766 add_device_config(DEV_SERIAL
, "stdio");
5767 if (default_virtcon
)
5768 add_device_config(DEV_VIRTCON
, "stdio");
5769 if (default_monitor
)
5770 monitor_parse("stdio", "readline");
5774 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5775 if (default_parallel
)
5776 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5777 if (default_monitor
)
5778 monitor_parse("vc:80Cx24C", "readline");
5779 if (default_virtcon
)
5780 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5783 vga_interface_type
= VGA_CIRRUS
;
5785 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5792 if (pipe(fds
) == -1)
5803 len
= read(fds
[0], &status
, 1);
5804 if (len
== -1 && (errno
== EINTR
))
5809 else if (status
== 1) {
5810 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5818 qemu_set_cloexec(fds
[1]);
5830 signal(SIGTSTP
, SIG_IGN
);
5831 signal(SIGTTOU
, SIG_IGN
);
5832 signal(SIGTTIN
, SIG_IGN
);
5836 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5840 if (write(fds
[1], &status
, 1) != 1) {
5841 perror("daemonize. Writing to pipe\n");
5845 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5849 if (kvm_enabled()) {
5852 ret
= kvm_init(smp_cpus
);
5854 fprintf(stderr
, "failed to initialize KVM\n");
5859 if (qemu_init_main_loop()) {
5860 fprintf(stderr
, "qemu_init_main_loop failed\n");
5863 linux_boot
= (kernel_filename
!= NULL
);
5865 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5866 fprintf(stderr
, "-append only allowed with -kernel option\n");
5870 if (!linux_boot
&& initrd_filename
!= NULL
) {
5871 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5876 /* Win32 doesn't support line-buffering and requires size >= 2 */
5877 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5880 if (init_timer_alarm() < 0) {
5881 fprintf(stderr
, "could not initialize alarm timer\n");
5884 configure_icount(icount_option
);
5890 if (net_init_clients() < 0) {
5894 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5895 net_set_boot_mask(net_boot
);
5897 /* init the bluetooth world */
5898 if (foreach_device_config(DEV_BT
, bt_parse
))
5901 /* init the memory */
5903 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5905 /* init the dynamic translator */
5906 cpu_exec_init_all(tb_size
* 1024 * 1024);
5908 bdrv_init_with_whitelist();
5912 if (default_cdrom
) {
5913 /* we always create the cdrom drive, even if no disk is there */
5914 drive_add(NULL
, CDROM_ALIAS
);
5917 if (default_floppy
) {
5918 /* we always create at least one floppy */
5919 drive_add(NULL
, FD_ALIAS
, 0);
5922 if (default_sdcard
) {
5923 /* we always create one sd slot, even if no card is in it */
5924 drive_add(NULL
, SD_ALIAS
);
5927 /* open the virtual block devices */
5929 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5930 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5933 vmstate_register(0, &vmstate_timers
,&timers_state
);
5934 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5937 if (nb_numa_nodes
> 0) {
5940 if (nb_numa_nodes
> smp_cpus
) {
5941 nb_numa_nodes
= smp_cpus
;
5944 /* If no memory size if given for any node, assume the default case
5945 * and distribute the available memory equally across all nodes
5947 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5948 if (node_mem
[i
] != 0)
5951 if (i
== nb_numa_nodes
) {
5952 uint64_t usedmem
= 0;
5954 /* On Linux, the each node's border has to be 8MB aligned,
5955 * the final node gets the rest.
5957 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5958 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5959 usedmem
+= node_mem
[i
];
5961 node_mem
[i
] = ram_size
- usedmem
;
5964 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5965 if (node_cpumask
[i
] != 0)
5968 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5969 * must cope with this anyway, because there are BIOSes out there in
5970 * real machines which also use this scheme.
5972 if (i
== nb_numa_nodes
) {
5973 for (i
= 0; i
< smp_cpus
; i
++) {
5974 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5979 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5981 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5983 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5985 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5988 module_call_init(MODULE_INIT_DEVICE
);
5990 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5994 i
= select_watchdog(watchdog
);
5996 exit (i
== 1 ? 1 : 0);
5999 if (machine
->compat_props
) {
6000 qdev_prop_register_global_list(machine
->compat_props
);
6004 machine
->init(ram_size
, boot_devices
,
6005 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6007 cpu_synchronize_all_post_init();
6010 /* must be after terminal init, SDL library changes signal handlers */
6014 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
6015 for (i
= 0; i
< nb_numa_nodes
; i
++) {
6016 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
6022 current_machine
= machine
;
6024 /* init USB devices */
6026 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
6030 /* init generic devices */
6031 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
6034 net_check_clients();
6036 /* just use the first displaystate for the moment */
6037 ds
= get_displaystate();
6039 if (display_type
== DT_DEFAULT
) {
6040 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6041 display_type
= DT_SDL
;
6043 display_type
= DT_VNC
;
6044 vnc_display
= "localhost:0,to=99";
6050 switch (display_type
) {
6053 #if defined(CONFIG_CURSES)
6055 curses_display_init(ds
, full_screen
);
6058 #if defined(CONFIG_SDL)
6060 sdl_display_init(ds
, full_screen
, no_frame
);
6062 #elif defined(CONFIG_COCOA)
6064 cocoa_display_init(ds
, full_screen
);
6068 vnc_display_init(ds
);
6069 if (vnc_display_open(ds
, vnc_display
) < 0)
6072 if (show_vnc_port
) {
6073 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6081 dcl
= ds
->listeners
;
6082 while (dcl
!= NULL
) {
6083 if (dcl
->dpy_refresh
!= NULL
) {
6084 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6085 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6090 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6091 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6092 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6095 text_consoles_set_display(ds
);
6097 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
6100 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6101 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6106 qdev_machine_creation_done();
6108 if (rom_load_all() != 0) {
6109 fprintf(stderr
, "rom loading failed\n");
6113 qemu_system_reset();
6115 if (load_vmstate(loadvm
) < 0) {
6121 qemu_start_incoming_migration(incoming
);
6122 } else if (autostart
) {
6132 len
= write(fds
[1], &status
, 1);
6133 if (len
== -1 && (errno
== EINTR
))
6140 perror("not able to chdir to /");
6143 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
6149 pwd
= getpwnam(run_as
);
6151 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6157 if (chroot(chroot_dir
) < 0) {
6158 fprintf(stderr
, "chroot failed\n");
6162 perror("not able to chdir to /");
6168 if (setgid(pwd
->pw_gid
) < 0) {
6169 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6172 if (setuid(pwd
->pw_uid
) < 0) {
6173 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6176 if (setuid(0) != -1) {
6177 fprintf(stderr
, "Dropping privileges failed\n");