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 static int timer_alarm_pending
= 1;
262 /* Conversion factor from emulated instructions to virtual clock ticks. */
263 static int icount_time_shift
;
264 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
265 #define MAX_ICOUNT_SHIFT 10
266 /* Compensate for varying guest execution speed. */
267 static int64_t qemu_icount_bias
;
268 static QEMUTimer
*icount_rt_timer
;
269 static QEMUTimer
*icount_vm_timer
;
270 static QEMUTimer
*nographic_timer
;
272 uint8_t qemu_uuid
[16];
274 static QEMUBootSetHandler
*boot_set_handler
;
275 static void *boot_set_opaque
;
278 #define SIG_IPI (SIGRTMIN+4)
280 #define SIG_IPI SIGUSR1
283 static int default_serial
= 1;
284 static int default_parallel
= 1;
285 static int default_virtcon
= 1;
286 static int default_monitor
= 1;
287 static int default_vga
= 1;
288 static int default_floppy
= 1;
289 static int default_cdrom
= 1;
290 static int default_sdcard
= 1;
296 { .driver
= "isa-serial", .flag
= &default_serial
},
297 { .driver
= "isa-parallel", .flag
= &default_parallel
},
298 { .driver
= "isa-fdc", .flag
= &default_floppy
},
299 { .driver
= "ide-drive", .flag
= &default_cdrom
},
300 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
301 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
302 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
303 { .driver
= "VGA", .flag
= &default_vga
},
304 { .driver
= "cirrus-vga", .flag
= &default_vga
},
305 { .driver
= "vmware-svga", .flag
= &default_vga
},
308 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
310 const char *driver
= qemu_opt_get(opts
, "driver");
315 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
316 if (strcmp(default_list
[i
].driver
, driver
) != 0)
318 *(default_list
[i
].flag
) = 0;
323 /***********************************************************/
324 /* x86 ISA bus support */
326 target_phys_addr_t isa_mem_base
= 0;
329 /***********************************************************/
330 void hw_error(const char *fmt
, ...)
336 fprintf(stderr
, "qemu: hardware error: ");
337 vfprintf(stderr
, fmt
, ap
);
338 fprintf(stderr
, "\n");
339 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
340 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
342 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
344 cpu_dump_state(env
, stderr
, fprintf
, 0);
351 static void set_proc_name(const char *s
)
353 #if defined(__linux__) && defined(PR_SET_NAME)
357 name
[sizeof(name
) - 1] = 0;
358 strncpy(name
, s
, sizeof(name
));
359 /* Could rewrite argv[0] too, but that's a bit more complicated.
360 This simple way is enough for `top'. */
361 prctl(PR_SET_NAME
, name
);
368 static QEMUBalloonEvent
*qemu_balloon_event
;
369 void *qemu_balloon_event_opaque
;
371 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
373 qemu_balloon_event
= func
;
374 qemu_balloon_event_opaque
= opaque
;
377 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
379 if (qemu_balloon_event
) {
380 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
387 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
389 if (qemu_balloon_event
) {
390 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
398 /***********************************************************/
399 /* real time host monotonic timer */
401 /* compute with 96 bit intermediate result: (a*b)/c */
402 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
407 #ifdef HOST_WORDS_BIGENDIAN
417 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
418 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
421 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
425 static int64_t get_clock_realtime(void)
429 gettimeofday(&tv
, NULL
);
430 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
435 static int64_t clock_freq
;
437 static void init_get_clock(void)
441 ret
= QueryPerformanceFrequency(&freq
);
443 fprintf(stderr
, "Could not calibrate ticks\n");
446 clock_freq
= freq
.QuadPart
;
449 static int64_t get_clock(void)
452 QueryPerformanceCounter(&ti
);
453 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
458 static int use_rt_clock
;
460 static void init_get_clock(void)
463 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
464 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
467 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
474 static int64_t get_clock(void)
476 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
477 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
480 clock_gettime(CLOCK_MONOTONIC
, &ts
);
481 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
485 /* XXX: using gettimeofday leads to problems if the date
486 changes, so it should be avoided. */
487 return get_clock_realtime();
492 /* Return the virtual CPU time, based on the instruction counter. */
493 static int64_t cpu_get_icount(void)
496 CPUState
*env
= cpu_single_env
;;
497 icount
= qemu_icount
;
500 fprintf(stderr
, "Bad clock read\n");
501 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
503 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
506 /***********************************************************/
507 /* guest cycle counter */
509 typedef struct TimersState
{
510 int64_t cpu_ticks_prev
;
511 int64_t cpu_ticks_offset
;
512 int64_t cpu_clock_offset
;
513 int32_t cpu_ticks_enabled
;
517 TimersState timers_state
;
519 /* return the host CPU cycle counter and handle stop/restart */
520 int64_t cpu_get_ticks(void)
523 return cpu_get_icount();
525 if (!timers_state
.cpu_ticks_enabled
) {
526 return timers_state
.cpu_ticks_offset
;
529 ticks
= cpu_get_real_ticks();
530 if (timers_state
.cpu_ticks_prev
> ticks
) {
531 /* Note: non increasing ticks may happen if the host uses
533 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
535 timers_state
.cpu_ticks_prev
= ticks
;
536 return ticks
+ timers_state
.cpu_ticks_offset
;
540 /* return the host CPU monotonic timer and handle stop/restart */
541 static int64_t cpu_get_clock(void)
544 if (!timers_state
.cpu_ticks_enabled
) {
545 return timers_state
.cpu_clock_offset
;
548 return ti
+ timers_state
.cpu_clock_offset
;
552 /* enable cpu_get_ticks() */
553 void cpu_enable_ticks(void)
555 if (!timers_state
.cpu_ticks_enabled
) {
556 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
557 timers_state
.cpu_clock_offset
-= get_clock();
558 timers_state
.cpu_ticks_enabled
= 1;
562 /* disable cpu_get_ticks() : the clock is stopped. You must not call
563 cpu_get_ticks() after that. */
564 void cpu_disable_ticks(void)
566 if (timers_state
.cpu_ticks_enabled
) {
567 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
568 timers_state
.cpu_clock_offset
= cpu_get_clock();
569 timers_state
.cpu_ticks_enabled
= 0;
573 /***********************************************************/
576 #define QEMU_CLOCK_REALTIME 0
577 #define QEMU_CLOCK_VIRTUAL 1
578 #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
);
600 unsigned int expired
;
603 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
605 return t
&& t
->rearm
;
608 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
610 if (!alarm_has_dynticks(t
))
616 /* TODO: MIN_TIMER_REARM_US should be optimized */
617 #define MIN_TIMER_REARM_US 250
619 static struct qemu_alarm_timer
*alarm_timer
;
623 struct qemu_alarm_win32
{
626 } alarm_win32_data
= {0, 0};
628 static int win32_start_timer(struct qemu_alarm_timer
*t
);
629 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
630 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
634 static int unix_start_timer(struct qemu_alarm_timer
*t
);
635 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
639 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
640 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
641 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
643 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
644 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
646 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
647 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
649 #endif /* __linux__ */
653 /* Correlation between real and virtual time is always going to be
654 fairly approximate, so ignore small variation.
655 When the guest is idle real and virtual time will be aligned in
657 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
659 static void icount_adjust(void)
664 static int64_t last_delta
;
665 /* If the VM is not running, then do nothing. */
669 cur_time
= cpu_get_clock();
670 cur_icount
= qemu_get_clock(vm_clock
);
671 delta
= cur_icount
- cur_time
;
672 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
674 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
675 && icount_time_shift
> 0) {
676 /* The guest is getting too far ahead. Slow time down. */
680 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
681 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
682 /* The guest is getting too far behind. Speed time up. */
686 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
689 static void icount_adjust_rt(void * opaque
)
691 qemu_mod_timer(icount_rt_timer
,
692 qemu_get_clock(rt_clock
) + 1000);
696 static void icount_adjust_vm(void * opaque
)
698 qemu_mod_timer(icount_vm_timer
,
699 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
703 static void init_icount_adjust(void)
705 /* Have both realtime and virtual time triggers for speed adjustment.
706 The realtime trigger catches emulated time passing too slowly,
707 the virtual time trigger catches emulated time passing too fast.
708 Realtime triggers occur even when idle, so use them less frequently
710 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
711 qemu_mod_timer(icount_rt_timer
,
712 qemu_get_clock(rt_clock
) + 1000);
713 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
714 qemu_mod_timer(icount_vm_timer
,
715 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
718 static struct qemu_alarm_timer alarm_timers
[] = {
721 {"dynticks", dynticks_start_timer
,
722 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
723 /* HPET - if available - is preferred */
724 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
725 /* ...otherwise try RTC */
726 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
728 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
730 {"dynticks", win32_start_timer
,
731 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
732 {"win32", win32_start_timer
,
733 win32_stop_timer
, NULL
, &alarm_win32_data
},
738 static void show_available_alarms(void)
742 printf("Available alarm timers, in order of precedence:\n");
743 for (i
= 0; alarm_timers
[i
].name
; i
++)
744 printf("%s\n", alarm_timers
[i
].name
);
747 static void configure_alarms(char const *opt
)
751 int count
= ARRAY_SIZE(alarm_timers
) - 1;
754 struct qemu_alarm_timer tmp
;
756 if (!strcmp(opt
, "?")) {
757 show_available_alarms();
761 arg
= qemu_strdup(opt
);
763 /* Reorder the array */
764 name
= strtok(arg
, ",");
766 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
767 if (!strcmp(alarm_timers
[i
].name
, name
))
772 fprintf(stderr
, "Unknown clock %s\n", name
);
781 tmp
= alarm_timers
[i
];
782 alarm_timers
[i
] = alarm_timers
[cur
];
783 alarm_timers
[cur
] = tmp
;
787 name
= strtok(NULL
, ",");
793 /* Disable remaining timers */
794 for (i
= cur
; i
< count
; i
++)
795 alarm_timers
[i
].name
= NULL
;
797 show_available_alarms();
802 #define QEMU_NUM_CLOCKS 3
806 QEMUClock
*host_clock
;
808 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
810 static QEMUClock
*qemu_new_clock(int type
)
813 clock
= qemu_mallocz(sizeof(QEMUClock
));
818 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
822 ts
= qemu_mallocz(sizeof(QEMUTimer
));
829 void qemu_free_timer(QEMUTimer
*ts
)
834 /* stop a timer, but do not dealloc it */
835 void qemu_del_timer(QEMUTimer
*ts
)
839 /* NOTE: this code must be signal safe because
840 qemu_timer_expired() can be called from a signal. */
841 pt
= &active_timers
[ts
->clock
->type
];
854 /* modify the current timer so that it will be fired when current_time
855 >= expire_time. The corresponding callback will be called. */
856 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
862 /* add the timer in the sorted list */
863 /* NOTE: this code must be signal safe because
864 qemu_timer_expired() can be called from a signal. */
865 pt
= &active_timers
[ts
->clock
->type
];
870 if (t
->expire_time
> expire_time
)
874 ts
->expire_time
= expire_time
;
878 /* Rearm if necessary */
879 if (pt
== &active_timers
[ts
->clock
->type
]) {
880 if (!alarm_timer
->expired
) {
881 qemu_rearm_alarm_timer(alarm_timer
);
883 /* Interrupt execution to force deadline recalculation. */
889 int qemu_timer_pending(QEMUTimer
*ts
)
892 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
899 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
903 return (timer_head
->expire_time
<= current_time
);
906 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
912 if (!ts
|| ts
->expire_time
> current_time
)
914 /* remove timer from the list before calling the callback */
915 *ptimer_head
= ts
->next
;
918 /* run the callback (the timer list can be modified) */
923 int64_t qemu_get_clock(QEMUClock
*clock
)
925 switch(clock
->type
) {
926 case QEMU_CLOCK_REALTIME
:
927 return get_clock() / 1000000;
929 case QEMU_CLOCK_VIRTUAL
:
931 return cpu_get_icount();
933 return cpu_get_clock();
935 case QEMU_CLOCK_HOST
:
936 return get_clock_realtime();
940 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
942 switch(clock
->type
) {
943 case QEMU_CLOCK_REALTIME
:
946 case QEMU_CLOCK_VIRTUAL
:
948 return cpu_get_icount();
950 return cpu_get_clock();
952 case QEMU_CLOCK_HOST
:
953 return get_clock_realtime();
957 static void init_clocks(void)
960 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
961 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
962 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
964 rtc_clock
= host_clock
;
968 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
970 uint64_t expire_time
;
972 if (qemu_timer_pending(ts
)) {
973 expire_time
= ts
->expire_time
;
977 qemu_put_be64(f
, expire_time
);
980 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
982 uint64_t expire_time
;
984 expire_time
= qemu_get_be64(f
);
985 if (expire_time
!= -1) {
986 qemu_mod_timer(ts
, expire_time
);
992 static const VMStateDescription vmstate_timers
= {
995 .minimum_version_id
= 1,
996 .minimum_version_id_old
= 1,
997 .fields
= (VMStateField
[]) {
998 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
999 VMSTATE_INT64(dummy
, TimersState
),
1000 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1001 VMSTATE_END_OF_LIST()
1005 static void qemu_event_increment(void);
1008 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1009 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1012 static void host_alarm_handler(int host_signum
)
1016 #define DISP_FREQ 1000
1018 static int64_t delta_min
= INT64_MAX
;
1019 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1021 ti
= qemu_get_clock(vm_clock
);
1022 if (last_clock
!= 0) {
1023 delta
= ti
- last_clock
;
1024 if (delta
< delta_min
)
1026 if (delta
> delta_max
)
1029 if (++count
== DISP_FREQ
) {
1030 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1031 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1032 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1033 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1034 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1036 delta_min
= INT64_MAX
;
1044 if (alarm_has_dynticks(alarm_timer
) ||
1046 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1047 qemu_get_clock(vm_clock
))) ||
1048 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1049 qemu_get_clock(rt_clock
)) ||
1050 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1051 qemu_get_clock(host_clock
))) {
1052 qemu_event_increment();
1053 if (alarm_timer
) alarm_timer
->expired
= 1;
1055 #ifndef CONFIG_IOTHREAD
1057 /* stop the currently executing cpu because a timer occured */
1061 timer_alarm_pending
= 1;
1062 qemu_notify_event();
1066 static int64_t qemu_next_deadline(void)
1068 /* To avoid problems with overflow limit this to 2^32. */
1069 int64_t delta
= INT32_MAX
;
1071 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1072 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1073 qemu_get_clock(vm_clock
);
1075 if (active_timers
[QEMU_CLOCK_HOST
]) {
1076 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1077 qemu_get_clock(host_clock
);
1088 #if defined(__linux__)
1089 static uint64_t qemu_next_deadline_dyntick(void)
1097 delta
= (qemu_next_deadline() + 999) / 1000;
1099 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1100 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1101 qemu_get_clock(rt_clock
))*1000;
1102 if (rtdelta
< delta
)
1106 if (delta
< MIN_TIMER_REARM_US
)
1107 delta
= MIN_TIMER_REARM_US
;
1115 /* Sets a specific flag */
1116 static int fcntl_setfl(int fd
, int flag
)
1120 flags
= fcntl(fd
, F_GETFL
);
1124 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1130 #if defined(__linux__)
1132 #define RTC_FREQ 1024
1134 static void enable_sigio_timer(int fd
)
1136 struct sigaction act
;
1139 sigfillset(&act
.sa_mask
);
1141 act
.sa_handler
= host_alarm_handler
;
1143 sigaction(SIGIO
, &act
, NULL
);
1144 fcntl_setfl(fd
, O_ASYNC
);
1145 fcntl(fd
, F_SETOWN
, getpid());
1148 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1150 struct hpet_info info
;
1153 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1158 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1160 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1161 "error, but for better emulation accuracy type:\n"
1162 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1166 /* Check capabilities */
1167 r
= ioctl(fd
, HPET_INFO
, &info
);
1171 /* Enable periodic mode */
1172 r
= ioctl(fd
, HPET_EPI
, 0);
1173 if (info
.hi_flags
&& (r
< 0))
1176 /* Enable interrupt */
1177 r
= ioctl(fd
, HPET_IE_ON
, 0);
1181 enable_sigio_timer(fd
);
1182 t
->priv
= (void *)(long)fd
;
1190 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1192 int fd
= (long)t
->priv
;
1197 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1200 unsigned long current_rtc_freq
= 0;
1202 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1205 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1206 if (current_rtc_freq
!= RTC_FREQ
&&
1207 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1208 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1209 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1210 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1213 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1219 enable_sigio_timer(rtc_fd
);
1221 t
->priv
= (void *)(long)rtc_fd
;
1226 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1228 int rtc_fd
= (long)t
->priv
;
1233 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1237 struct sigaction act
;
1239 sigfillset(&act
.sa_mask
);
1241 act
.sa_handler
= host_alarm_handler
;
1243 sigaction(SIGALRM
, &act
, NULL
);
1246 * Initialize ev struct to 0 to avoid valgrind complaining
1247 * about uninitialized data in timer_create call
1249 memset(&ev
, 0, sizeof(ev
));
1250 ev
.sigev_value
.sival_int
= 0;
1251 ev
.sigev_notify
= SIGEV_SIGNAL
;
1252 ev
.sigev_signo
= SIGALRM
;
1254 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1255 perror("timer_create");
1257 /* disable dynticks */
1258 fprintf(stderr
, "Dynamic Ticks disabled\n");
1263 t
->priv
= (void *)(long)host_timer
;
1268 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1270 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1272 timer_delete(host_timer
);
1275 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1277 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1278 struct itimerspec timeout
;
1279 int64_t nearest_delta_us
= INT64_MAX
;
1282 assert(alarm_has_dynticks(t
));
1283 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1284 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1285 !active_timers
[QEMU_CLOCK_HOST
])
1288 nearest_delta_us
= qemu_next_deadline_dyntick();
1290 /* check whether a timer is already running */
1291 if (timer_gettime(host_timer
, &timeout
)) {
1293 fprintf(stderr
, "Internal timer error: aborting\n");
1296 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1297 if (current_us
&& current_us
<= nearest_delta_us
)
1300 timeout
.it_interval
.tv_sec
= 0;
1301 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1302 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1303 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1304 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1306 fprintf(stderr
, "Internal timer error: aborting\n");
1311 #endif /* defined(__linux__) */
1313 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1315 struct sigaction act
;
1316 struct itimerval itv
;
1320 sigfillset(&act
.sa_mask
);
1322 act
.sa_handler
= host_alarm_handler
;
1324 sigaction(SIGALRM
, &act
, NULL
);
1326 itv
.it_interval
.tv_sec
= 0;
1327 /* for i386 kernel 2.6 to get 1 ms */
1328 itv
.it_interval
.tv_usec
= 999;
1329 itv
.it_value
.tv_sec
= 0;
1330 itv
.it_value
.tv_usec
= 10 * 1000;
1332 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1339 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1341 struct itimerval itv
;
1343 memset(&itv
, 0, sizeof(itv
));
1344 setitimer(ITIMER_REAL
, &itv
, NULL
);
1347 #endif /* !defined(_WIN32) */
1352 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1355 struct qemu_alarm_win32
*data
= t
->priv
;
1358 memset(&tc
, 0, sizeof(tc
));
1359 timeGetDevCaps(&tc
, sizeof(tc
));
1361 data
->period
= tc
.wPeriodMin
;
1362 timeBeginPeriod(data
->period
);
1364 flags
= TIME_CALLBACK_FUNCTION
;
1365 if (alarm_has_dynticks(t
))
1366 flags
|= TIME_ONESHOT
;
1368 flags
|= TIME_PERIODIC
;
1370 data
->timerId
= timeSetEvent(1, // interval (ms)
1371 data
->period
, // resolution
1372 host_alarm_handler
, // function
1373 (DWORD
)t
, // parameter
1376 if (!data
->timerId
) {
1377 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1379 timeEndPeriod(data
->period
);
1386 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1388 struct qemu_alarm_win32
*data
= t
->priv
;
1390 timeKillEvent(data
->timerId
);
1391 timeEndPeriod(data
->period
);
1394 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1396 struct qemu_alarm_win32
*data
= t
->priv
;
1398 assert(alarm_has_dynticks(t
));
1399 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1400 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1401 !active_timers
[QEMU_CLOCK_HOST
])
1404 timeKillEvent(data
->timerId
);
1406 data
->timerId
= timeSetEvent(1,
1410 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1412 if (!data
->timerId
) {
1413 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1416 timeEndPeriod(data
->period
);
1423 static int init_timer_alarm(void)
1425 struct qemu_alarm_timer
*t
= NULL
;
1428 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1429 t
= &alarm_timers
[i
];
1449 static void quit_timers(void)
1451 alarm_timer
->stop(alarm_timer
);
1455 /***********************************************************/
1456 /* host time/date access */
1457 void qemu_get_timedate(struct tm
*tm
, int offset
)
1464 if (rtc_date_offset
== -1) {
1468 ret
= localtime(&ti
);
1470 ti
-= rtc_date_offset
;
1474 memcpy(tm
, ret
, sizeof(struct tm
));
1477 int qemu_timedate_diff(struct tm
*tm
)
1481 if (rtc_date_offset
== -1)
1483 seconds
= mktimegm(tm
);
1485 seconds
= mktime(tm
);
1487 seconds
= mktimegm(tm
) + rtc_date_offset
;
1489 return seconds
- time(NULL
);
1492 void rtc_change_mon_event(struct tm
*tm
)
1496 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
1497 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
1498 qobject_decref(data
);
1501 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1503 time_t rtc_start_date
;
1506 if (!strcmp(startdate
, "now") && legacy
) {
1507 rtc_date_offset
= -1;
1509 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1517 } else if (sscanf(startdate
, "%d-%d-%d",
1520 &tm
.tm_mday
) == 3) {
1529 rtc_start_date
= mktimegm(&tm
);
1530 if (rtc_start_date
== -1) {
1532 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1533 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1536 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1540 static void configure_rtc(QemuOpts
*opts
)
1544 value
= qemu_opt_get(opts
, "base");
1546 if (!strcmp(value
, "utc")) {
1548 } else if (!strcmp(value
, "localtime")) {
1551 configure_rtc_date_offset(value
, 0);
1554 value
= qemu_opt_get(opts
, "clock");
1556 if (!strcmp(value
, "host")) {
1557 rtc_clock
= host_clock
;
1558 } else if (!strcmp(value
, "vm")) {
1559 rtc_clock
= vm_clock
;
1561 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1565 #ifdef CONFIG_TARGET_I386
1566 value
= qemu_opt_get(opts
, "driftfix");
1568 if (!strcmp(buf
, "slew")) {
1570 } else if (!strcmp(buf
, "none")) {
1573 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1581 static void socket_cleanup(void)
1586 static int socket_init(void)
1591 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1593 err
= WSAGetLastError();
1594 fprintf(stderr
, "WSAStartup: %d\n", err
);
1597 atexit(socket_cleanup
);
1602 /***********************************************************/
1603 /* Bluetooth support */
1606 static struct HCIInfo
*hci_table
[MAX_NICS
];
1608 static struct bt_vlan_s
{
1609 struct bt_scatternet_s net
;
1611 struct bt_vlan_s
*next
;
1614 /* find or alloc a new bluetooth "VLAN" */
1615 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1617 struct bt_vlan_s
**pvlan
, *vlan
;
1618 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1622 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1624 pvlan
= &first_bt_vlan
;
1625 while (*pvlan
!= NULL
)
1626 pvlan
= &(*pvlan
)->next
;
1631 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1635 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1640 static struct HCIInfo null_hci
= {
1641 .cmd_send
= null_hci_send
,
1642 .sco_send
= null_hci_send
,
1643 .acl_send
= null_hci_send
,
1644 .bdaddr_set
= null_hci_addr_set
,
1647 struct HCIInfo
*qemu_next_hci(void)
1649 if (cur_hci
== nb_hcis
)
1652 return hci_table
[cur_hci
++];
1655 static struct HCIInfo
*hci_init(const char *str
)
1658 struct bt_scatternet_s
*vlan
= 0;
1660 if (!strcmp(str
, "null"))
1663 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1665 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1666 else if (!strncmp(str
, "hci", 3)) {
1669 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1670 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1675 vlan
= qemu_find_bt_vlan(0);
1677 return bt_new_hci(vlan
);
1680 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1685 static int bt_hci_parse(const char *str
)
1687 struct HCIInfo
*hci
;
1690 if (nb_hcis
>= MAX_NICS
) {
1691 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1695 hci
= hci_init(str
);
1704 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1705 hci
->bdaddr_set(hci
, bdaddr
.b
);
1707 hci_table
[nb_hcis
++] = hci
;
1712 static void bt_vhci_add(int vlan_id
)
1714 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1717 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1718 "an empty scatternet %i\n", vlan_id
);
1720 bt_vhci_init(bt_new_hci(vlan
));
1723 static struct bt_device_s
*bt_device_add(const char *opt
)
1725 struct bt_scatternet_s
*vlan
;
1727 char *endp
= strstr(opt
, ",vlan=");
1728 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1731 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1734 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1736 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1741 vlan
= qemu_find_bt_vlan(vlan_id
);
1744 fprintf(stderr
, "qemu: warning: adding a slave device to "
1745 "an empty scatternet %i\n", vlan_id
);
1747 if (!strcmp(devname
, "keyboard"))
1748 return bt_keyboard_init(vlan
);
1750 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1754 static int bt_parse(const char *opt
)
1756 const char *endp
, *p
;
1759 if (strstart(opt
, "hci", &endp
)) {
1760 if (!*endp
|| *endp
== ',') {
1762 if (!strstart(endp
, ",vlan=", 0))
1765 return bt_hci_parse(opt
);
1767 } else if (strstart(opt
, "vhci", &endp
)) {
1768 if (!*endp
|| *endp
== ',') {
1770 if (strstart(endp
, ",vlan=", &p
)) {
1771 vlan
= strtol(p
, (char **) &endp
, 0);
1773 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1777 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1786 } else if (strstart(opt
, "device:", &endp
))
1787 return !bt_device_add(endp
);
1789 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1793 /***********************************************************/
1794 /* QEMU Block devices */
1796 #define HD_ALIAS "index=%d,media=disk"
1797 #define CDROM_ALIAS "index=2,media=cdrom"
1798 #define FD_ALIAS "index=%d,if=floppy"
1799 #define PFLASH_ALIAS "if=pflash"
1800 #define MTD_ALIAS "if=mtd"
1801 #define SD_ALIAS "index=0,if=sd"
1803 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1810 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1813 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
1815 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1816 __FUNCTION__
, optstr
);
1820 qemu_opt_set(opts
, "file", file
);
1824 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1828 /* seek interface, bus and unit */
1830 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1831 if (dinfo
->type
== type
&&
1832 dinfo
->bus
== bus
&&
1833 dinfo
->unit
== unit
)
1840 DriveInfo
*drive_get_by_id(const char *id
)
1844 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1845 if (strcmp(id
, dinfo
->id
))
1852 int drive_get_max_bus(BlockInterfaceType type
)
1858 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1859 if(dinfo
->type
== type
&&
1860 dinfo
->bus
> max_bus
)
1861 max_bus
= dinfo
->bus
;
1866 const char *drive_get_serial(BlockDriverState
*bdrv
)
1870 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1871 if (dinfo
->bdrv
== bdrv
)
1872 return dinfo
->serial
;
1878 BlockInterfaceErrorAction
drive_get_on_error(
1879 BlockDriverState
*bdrv
, int is_read
)
1883 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1884 if (dinfo
->bdrv
== bdrv
)
1885 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1888 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1891 static void bdrv_format_print(void *opaque
, const char *name
)
1893 fprintf(stderr
, " %s", name
);
1896 void drive_uninit(DriveInfo
*dinfo
)
1898 qemu_opts_del(dinfo
->opts
);
1899 bdrv_delete(dinfo
->bdrv
);
1900 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1904 static int parse_block_error_action(const char *buf
, int is_read
)
1906 if (!strcmp(buf
, "ignore")) {
1907 return BLOCK_ERR_IGNORE
;
1908 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1909 return BLOCK_ERR_STOP_ENOSPC
;
1910 } else if (!strcmp(buf
, "stop")) {
1911 return BLOCK_ERR_STOP_ANY
;
1912 } else if (!strcmp(buf
, "report")) {
1913 return BLOCK_ERR_REPORT
;
1915 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1916 buf
, is_read
? "read" : "write");
1921 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1925 const char *file
= NULL
;
1928 const char *mediastr
= "";
1929 BlockInterfaceType type
;
1930 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1931 int bus_id
, unit_id
;
1932 int cyls
, heads
, secs
, translation
;
1933 BlockDriver
*drv
= NULL
;
1934 QEMUMachine
*machine
= opaque
;
1941 int on_read_error
, on_write_error
;
1942 const char *devaddr
;
1948 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1951 if (machine
&& machine
->use_scsi
) {
1953 max_devs
= MAX_SCSI_DEVS
;
1954 pstrcpy(devname
, sizeof(devname
), "scsi");
1957 max_devs
= MAX_IDE_DEVS
;
1958 pstrcpy(devname
, sizeof(devname
), "ide");
1962 /* extract parameters */
1963 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1964 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1965 index
= qemu_opt_get_number(opts
, "index", -1);
1967 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1968 heads
= qemu_opt_get_number(opts
, "heads", 0);
1969 secs
= qemu_opt_get_number(opts
, "secs", 0);
1971 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1972 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1974 file
= qemu_opt_get(opts
, "file");
1975 serial
= qemu_opt_get(opts
, "serial");
1977 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1978 pstrcpy(devname
, sizeof(devname
), buf
);
1979 if (!strcmp(buf
, "ide")) {
1981 max_devs
= MAX_IDE_DEVS
;
1982 } else if (!strcmp(buf
, "scsi")) {
1984 max_devs
= MAX_SCSI_DEVS
;
1985 } else if (!strcmp(buf
, "floppy")) {
1988 } else if (!strcmp(buf
, "pflash")) {
1991 } else if (!strcmp(buf
, "mtd")) {
1994 } else if (!strcmp(buf
, "sd")) {
1997 } else if (!strcmp(buf
, "virtio")) {
2000 } else if (!strcmp(buf
, "xen")) {
2003 } else if (!strcmp(buf
, "none")) {
2007 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2012 if (cyls
|| heads
|| secs
) {
2013 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2014 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2017 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2018 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2021 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2022 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2027 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2030 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2034 if (!strcmp(buf
, "none"))
2035 translation
= BIOS_ATA_TRANSLATION_NONE
;
2036 else if (!strcmp(buf
, "lba"))
2037 translation
= BIOS_ATA_TRANSLATION_LBA
;
2038 else if (!strcmp(buf
, "auto"))
2039 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2041 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2046 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2047 if (!strcmp(buf
, "disk")) {
2049 } else if (!strcmp(buf
, "cdrom")) {
2050 if (cyls
|| secs
|| heads
) {
2052 "qemu: '%s' invalid physical CHS format\n", buf
);
2055 media
= MEDIA_CDROM
;
2057 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2062 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2063 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2065 else if (!strcmp(buf
, "writethrough"))
2067 else if (!strcmp(buf
, "writeback"))
2070 fprintf(stderr
, "qemu: invalid cache option\n");
2075 #ifdef CONFIG_LINUX_AIO
2076 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2077 if (!strcmp(buf
, "threads"))
2079 else if (!strcmp(buf
, "native"))
2082 fprintf(stderr
, "qemu: invalid aio option\n");
2088 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2089 if (strcmp(buf
, "?") == 0) {
2090 fprintf(stderr
, "qemu: Supported formats:");
2091 bdrv_iterate_format(bdrv_format_print
, NULL
);
2092 fprintf(stderr
, "\n");
2095 drv
= bdrv_find_whitelisted_format(buf
);
2097 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2102 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2103 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2104 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2105 fprintf(stderr
, "werror is no supported by this format\n");
2109 on_write_error
= parse_block_error_action(buf
, 0);
2110 if (on_write_error
< 0) {
2115 on_read_error
= BLOCK_ERR_REPORT
;
2116 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2117 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2118 fprintf(stderr
, "rerror is no supported by this format\n");
2122 on_read_error
= parse_block_error_action(buf
, 1);
2123 if (on_read_error
< 0) {
2128 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2129 if (type
!= IF_VIRTIO
) {
2130 fprintf(stderr
, "addr is not supported\n");
2135 /* compute bus and unit according index */
2138 if (bus_id
!= 0 || unit_id
!= -1) {
2140 "qemu: index cannot be used with bus and unit\n");
2148 unit_id
= index
% max_devs
;
2149 bus_id
= index
/ max_devs
;
2153 /* if user doesn't specify a unit_id,
2154 * try to find the first free
2157 if (unit_id
== -1) {
2159 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2161 if (max_devs
&& unit_id
>= max_devs
) {
2162 unit_id
-= max_devs
;
2170 if (max_devs
&& unit_id
>= max_devs
) {
2171 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2172 unit_id
, max_devs
- 1);
2177 * ignore multiple definitions
2180 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2187 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2188 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2189 dinfo
->id
= qemu_strdup(buf
);
2191 /* no id supplied -> create one */
2192 dinfo
->id
= qemu_mallocz(32);
2193 if (type
== IF_IDE
|| type
== IF_SCSI
)
2194 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2196 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2197 devname
, bus_id
, mediastr
, unit_id
);
2199 snprintf(dinfo
->id
, 32, "%s%s%i",
2200 devname
, mediastr
, unit_id
);
2202 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2203 dinfo
->devaddr
= devaddr
;
2205 dinfo
->bus
= bus_id
;
2206 dinfo
->unit
= unit_id
;
2207 dinfo
->on_read_error
= on_read_error
;
2208 dinfo
->on_write_error
= on_write_error
;
2211 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2212 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2222 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2223 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2227 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2232 /* FIXME: This isn't really a floppy, but it's a reasonable
2235 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2241 /* add virtio block device */
2242 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2243 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2244 qemu_opt_set(opts
, "drive", dinfo
->id
);
2246 qemu_opt_set(opts
, "addr", devaddr
);
2257 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2258 cache
= 2; /* always use write-back with snapshot */
2260 if (cache
== 0) /* no caching */
2261 bdrv_flags
|= BDRV_O_NOCACHE
;
2262 else if (cache
== 2) /* write-back */
2263 bdrv_flags
|= BDRV_O_CACHE_WB
;
2266 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2268 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2272 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2273 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2278 * cdrom is read-only. Set it now, after above interface checking
2279 * since readonly attribute not explicitly required, so no error.
2281 if (media
== MEDIA_CDROM
) {
2284 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2286 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2287 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2288 file
, strerror(errno
));
2292 if (bdrv_key_required(dinfo
->bdrv
))
2298 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2300 QEMUMachine
*machine
= opaque
;
2301 int fatal_error
= 0;
2303 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2310 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2312 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2313 qemu_opt_set(opts
, "snapshot", "on");
2318 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2320 boot_set_handler
= func
;
2321 boot_set_opaque
= opaque
;
2324 int qemu_boot_set(const char *boot_devices
)
2326 if (!boot_set_handler
) {
2329 return boot_set_handler(boot_set_opaque
, boot_devices
);
2332 static int parse_bootdevices(char *devices
)
2334 /* We just do some generic consistency checks */
2338 for (p
= devices
; *p
!= '\0'; p
++) {
2339 /* Allowed boot devices are:
2340 * a-b: floppy disk drives
2341 * c-f: IDE disk drives
2342 * g-m: machine implementation dependant drives
2343 * n-p: network devices
2344 * It's up to each machine implementation to check if the given boot
2345 * devices match the actual hardware implementation and firmware
2348 if (*p
< 'a' || *p
> 'p') {
2349 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2352 if (bitmap
& (1 << (*p
- 'a'))) {
2353 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2356 bitmap
|= 1 << (*p
- 'a');
2361 static void restore_boot_devices(void *opaque
)
2363 char *standard_boot_devices
= opaque
;
2365 qemu_boot_set(standard_boot_devices
);
2367 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2368 qemu_free(standard_boot_devices
);
2371 static void numa_add(const char *optarg
)
2375 unsigned long long value
, endvalue
;
2378 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2379 if (!strcmp(option
, "node")) {
2380 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2381 nodenr
= nb_numa_nodes
;
2383 nodenr
= strtoull(option
, NULL
, 10);
2386 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2387 node_mem
[nodenr
] = 0;
2389 value
= strtoull(option
, &endptr
, 0);
2391 case 0: case 'M': case 'm':
2398 node_mem
[nodenr
] = value
;
2400 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2401 node_cpumask
[nodenr
] = 0;
2403 value
= strtoull(option
, &endptr
, 10);
2406 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2408 if (*endptr
== '-') {
2409 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2410 if (endvalue
>= 63) {
2413 "only 63 CPUs in NUMA mode supported.\n");
2415 value
= (2ULL << endvalue
) - (1ULL << value
);
2417 value
= 1ULL << value
;
2420 node_cpumask
[nodenr
] = value
;
2427 static void smp_parse(const char *optarg
)
2429 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2433 smp
= strtoul(optarg
, &endptr
, 10);
2434 if (endptr
!= optarg
) {
2435 if (*endptr
== ',') {
2439 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2440 sockets
= strtoull(option
, NULL
, 10);
2441 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2442 cores
= strtoull(option
, NULL
, 10);
2443 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2444 threads
= strtoull(option
, NULL
, 10);
2445 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2446 max_cpus
= strtoull(option
, NULL
, 10);
2448 /* compute missing values, prefer sockets over cores over threads */
2449 if (smp
== 0 || sockets
== 0) {
2450 sockets
= sockets
> 0 ? sockets
: 1;
2451 cores
= cores
> 0 ? cores
: 1;
2452 threads
= threads
> 0 ? threads
: 1;
2454 smp
= cores
* threads
* sockets
;
2458 threads
= threads
> 0 ? threads
: 1;
2459 cores
= smp
/ (sockets
* threads
);
2462 threads
= smp
/ (cores
* sockets
);
2467 smp_cores
= cores
> 0 ? cores
: 1;
2468 smp_threads
= threads
> 0 ? threads
: 1;
2470 max_cpus
= smp_cpus
;
2473 /***********************************************************/
2476 static int usb_device_add(const char *devname
, int is_hotplug
)
2479 USBDevice
*dev
= NULL
;
2484 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2485 dev
= usbdevice_create(devname
);
2489 /* the other ones */
2490 if (strstart(devname
, "host:", &p
)) {
2491 dev
= usb_host_device_open(p
);
2492 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2493 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2494 bt_new_hci(qemu_find_bt_vlan(0)));
2505 static int usb_device_del(const char *devname
)
2510 if (strstart(devname
, "host:", &p
))
2511 return usb_host_device_close(p
);
2516 p
= strchr(devname
, '.');
2519 bus_num
= strtoul(devname
, NULL
, 0);
2520 addr
= strtoul(p
+ 1, NULL
, 0);
2522 return usb_device_delete_addr(bus_num
, addr
);
2525 static int usb_parse(const char *cmdline
)
2528 r
= usb_device_add(cmdline
, 0);
2530 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2535 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2537 const char *devname
= qdict_get_str(qdict
, "devname");
2538 if (usb_device_add(devname
, 1) < 0) {
2539 error_report("could not add USB device '%s'", devname
);
2543 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2545 const char *devname
= qdict_get_str(qdict
, "devname");
2546 if (usb_device_del(devname
) < 0) {
2547 error_report("could not delete USB device '%s'", devname
);
2551 /***********************************************************/
2552 /* PCMCIA/Cardbus */
2554 static struct pcmcia_socket_entry_s
{
2555 PCMCIASocket
*socket
;
2556 struct pcmcia_socket_entry_s
*next
;
2557 } *pcmcia_sockets
= 0;
2559 void pcmcia_socket_register(PCMCIASocket
*socket
)
2561 struct pcmcia_socket_entry_s
*entry
;
2563 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2564 entry
->socket
= socket
;
2565 entry
->next
= pcmcia_sockets
;
2566 pcmcia_sockets
= entry
;
2569 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2571 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2573 ptr
= &pcmcia_sockets
;
2574 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2575 if (entry
->socket
== socket
) {
2581 void pcmcia_info(Monitor
*mon
)
2583 struct pcmcia_socket_entry_s
*iter
;
2585 if (!pcmcia_sockets
)
2586 monitor_printf(mon
, "No PCMCIA sockets\n");
2588 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2589 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2590 iter
->socket
->attached
? iter
->socket
->card_string
:
2594 /***********************************************************/
2597 typedef struct IOHandlerRecord
{
2599 IOCanRWHandler
*fd_read_poll
;
2601 IOHandler
*fd_write
;
2604 /* temporary data */
2606 struct IOHandlerRecord
*next
;
2609 static IOHandlerRecord
*first_io_handler
;
2611 /* XXX: fd_read_poll should be suppressed, but an API change is
2612 necessary in the character devices to suppress fd_can_read(). */
2613 int qemu_set_fd_handler2(int fd
,
2614 IOCanRWHandler
*fd_read_poll
,
2616 IOHandler
*fd_write
,
2619 IOHandlerRecord
**pioh
, *ioh
;
2621 if (!fd_read
&& !fd_write
) {
2622 pioh
= &first_io_handler
;
2627 if (ioh
->fd
== fd
) {
2634 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2638 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2639 ioh
->next
= first_io_handler
;
2640 first_io_handler
= ioh
;
2643 ioh
->fd_read_poll
= fd_read_poll
;
2644 ioh
->fd_read
= fd_read
;
2645 ioh
->fd_write
= fd_write
;
2646 ioh
->opaque
= opaque
;
2652 int qemu_set_fd_handler(int fd
,
2654 IOHandler
*fd_write
,
2657 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2661 /***********************************************************/
2662 /* Polling handling */
2664 typedef struct PollingEntry
{
2667 struct PollingEntry
*next
;
2670 static PollingEntry
*first_polling_entry
;
2672 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2674 PollingEntry
**ppe
, *pe
;
2675 pe
= qemu_mallocz(sizeof(PollingEntry
));
2677 pe
->opaque
= opaque
;
2678 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2683 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2685 PollingEntry
**ppe
, *pe
;
2686 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2688 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2696 /***********************************************************/
2697 /* Wait objects support */
2698 typedef struct WaitObjects
{
2700 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2701 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2702 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2705 static WaitObjects wait_objects
= {0};
2707 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2709 WaitObjects
*w
= &wait_objects
;
2711 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2713 w
->events
[w
->num
] = handle
;
2714 w
->func
[w
->num
] = func
;
2715 w
->opaque
[w
->num
] = opaque
;
2720 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2723 WaitObjects
*w
= &wait_objects
;
2726 for (i
= 0; i
< w
->num
; i
++) {
2727 if (w
->events
[i
] == handle
)
2730 w
->events
[i
] = w
->events
[i
+ 1];
2731 w
->func
[i
] = w
->func
[i
+ 1];
2732 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2740 /***********************************************************/
2741 /* ram save/restore */
2743 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2744 #define RAM_SAVE_FLAG_COMPRESS 0x02
2745 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2746 #define RAM_SAVE_FLAG_PAGE 0x08
2747 #define RAM_SAVE_FLAG_EOS 0x10
2749 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2751 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2752 uint32_t *array
= (uint32_t *)page
;
2755 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2756 if (array
[i
] != val
)
2763 static int ram_save_block(QEMUFile
*f
)
2765 static ram_addr_t current_addr
= 0;
2766 ram_addr_t saved_addr
= current_addr
;
2767 ram_addr_t addr
= 0;
2770 while (addr
< last_ram_offset
) {
2771 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2774 cpu_physical_memory_reset_dirty(current_addr
,
2775 current_addr
+ TARGET_PAGE_SIZE
,
2776 MIGRATION_DIRTY_FLAG
);
2778 p
= qemu_get_ram_ptr(current_addr
);
2780 if (is_dup_page(p
, *p
)) {
2781 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2782 qemu_put_byte(f
, *p
);
2784 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2785 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2791 addr
+= TARGET_PAGE_SIZE
;
2792 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2798 static uint64_t bytes_transferred
;
2800 static ram_addr_t
ram_save_remaining(void)
2803 ram_addr_t count
= 0;
2805 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2806 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2813 uint64_t ram_bytes_remaining(void)
2815 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2818 uint64_t ram_bytes_transferred(void)
2820 return bytes_transferred
;
2823 uint64_t ram_bytes_total(void)
2825 return last_ram_offset
;
2828 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2831 uint64_t bytes_transferred_last
;
2833 uint64_t expected_time
= 0;
2836 cpu_physical_memory_set_dirty_tracking(0);
2840 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2841 qemu_file_set_error(f
);
2846 bytes_transferred
= 0;
2848 /* Make sure all dirty bits are set */
2849 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2850 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2851 cpu_physical_memory_set_dirty(addr
);
2854 /* Enable dirty memory tracking */
2855 cpu_physical_memory_set_dirty_tracking(1);
2857 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2860 bytes_transferred_last
= bytes_transferred
;
2861 bwidth
= qemu_get_clock_ns(rt_clock
);
2863 while (!qemu_file_rate_limit(f
)) {
2866 ret
= ram_save_block(f
);
2867 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2868 if (ret
== 0) /* no more blocks */
2872 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
2873 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2875 /* if we haven't transferred anything this round, force expected_time to a
2876 * a very high value, but without crashing */
2880 /* try transferring iterative blocks of memory */
2882 /* flush all remaining blocks regardless of rate limiting */
2883 while (ram_save_block(f
) != 0) {
2884 bytes_transferred
+= TARGET_PAGE_SIZE
;
2886 cpu_physical_memory_set_dirty_tracking(0);
2889 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2891 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2893 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2896 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2901 if (version_id
!= 3)
2905 addr
= qemu_get_be64(f
);
2907 flags
= addr
& ~TARGET_PAGE_MASK
;
2908 addr
&= TARGET_PAGE_MASK
;
2910 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2911 if (addr
!= last_ram_offset
)
2915 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2916 uint8_t ch
= qemu_get_byte(f
);
2917 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2920 (!kvm_enabled() || kvm_has_sync_mmu())) {
2921 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2924 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2925 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2927 if (qemu_file_has_error(f
)) {
2930 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2935 void qemu_service_io(void)
2937 qemu_notify_event();
2940 /***********************************************************/
2941 /* machine registration */
2943 static QEMUMachine
*first_machine
= NULL
;
2944 QEMUMachine
*current_machine
= NULL
;
2946 int qemu_register_machine(QEMUMachine
*m
)
2949 pm
= &first_machine
;
2957 static QEMUMachine
*find_machine(const char *name
)
2961 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2962 if (!strcmp(m
->name
, name
))
2964 if (m
->alias
&& !strcmp(m
->alias
, name
))
2970 static QEMUMachine
*find_default_machine(void)
2974 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2975 if (m
->is_default
) {
2982 /***********************************************************/
2983 /* main execution loop */
2985 static void gui_update(void *opaque
)
2987 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2988 DisplayState
*ds
= opaque
;
2989 DisplayChangeListener
*dcl
= ds
->listeners
;
2991 qemu_flush_coalesced_mmio_buffer();
2994 while (dcl
!= NULL
) {
2995 if (dcl
->gui_timer_interval
&&
2996 dcl
->gui_timer_interval
< interval
)
2997 interval
= dcl
->gui_timer_interval
;
3000 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3003 static void nographic_update(void *opaque
)
3005 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3007 qemu_flush_coalesced_mmio_buffer();
3008 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3011 void cpu_synchronize_all_states(void)
3015 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3016 cpu_synchronize_state(cpu
);
3020 void cpu_synchronize_all_post_reset(void)
3024 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3025 cpu_synchronize_post_reset(cpu
);
3029 void cpu_synchronize_all_post_init(void)
3033 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3034 cpu_synchronize_post_init(cpu
);
3038 struct vm_change_state_entry
{
3039 VMChangeStateHandler
*cb
;
3041 QLIST_ENTRY (vm_change_state_entry
) entries
;
3044 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3046 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3049 VMChangeStateEntry
*e
;
3051 e
= qemu_mallocz(sizeof (*e
));
3055 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3059 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3061 QLIST_REMOVE (e
, entries
);
3065 static void vm_state_notify(int running
, int reason
)
3067 VMChangeStateEntry
*e
;
3069 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3070 e
->cb(e
->opaque
, running
, reason
);
3074 static void resume_all_vcpus(void);
3075 static void pause_all_vcpus(void);
3082 vm_state_notify(1, 0);
3083 qemu_rearm_alarm_timer(alarm_timer
);
3088 /* reset/shutdown handler */
3090 typedef struct QEMUResetEntry
{
3091 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3092 QEMUResetHandler
*func
;
3096 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3097 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3098 static int reset_requested
;
3099 static int shutdown_requested
;
3100 static int powerdown_requested
;
3101 static int debug_requested
;
3102 static int vmstop_requested
;
3104 int qemu_shutdown_requested(void)
3106 int r
= shutdown_requested
;
3107 shutdown_requested
= 0;
3111 int qemu_reset_requested(void)
3113 int r
= reset_requested
;
3114 reset_requested
= 0;
3118 int qemu_powerdown_requested(void)
3120 int r
= powerdown_requested
;
3121 powerdown_requested
= 0;
3125 static int qemu_debug_requested(void)
3127 int r
= debug_requested
;
3128 debug_requested
= 0;
3132 static int qemu_vmstop_requested(void)
3134 int r
= vmstop_requested
;
3135 vmstop_requested
= 0;
3139 static void do_vm_stop(int reason
)
3142 cpu_disable_ticks();
3145 vm_state_notify(0, reason
);
3146 monitor_protocol_event(QEVENT_STOP
, NULL
);
3150 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3152 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3155 re
->opaque
= opaque
;
3156 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3159 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3163 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3164 if (re
->func
== func
&& re
->opaque
== opaque
) {
3165 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3172 void qemu_system_reset(void)
3174 QEMUResetEntry
*re
, *nre
;
3176 /* reset all devices */
3177 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3178 re
->func(re
->opaque
);
3180 monitor_protocol_event(QEVENT_RESET
, NULL
);
3181 cpu_synchronize_all_post_reset();
3184 void qemu_system_reset_request(void)
3187 shutdown_requested
= 1;
3189 reset_requested
= 1;
3191 qemu_notify_event();
3194 void qemu_system_shutdown_request(void)
3196 shutdown_requested
= 1;
3197 qemu_notify_event();
3200 void qemu_system_powerdown_request(void)
3202 powerdown_requested
= 1;
3203 qemu_notify_event();
3206 #ifdef CONFIG_IOTHREAD
3207 static void qemu_system_vmstop_request(int reason
)
3209 vmstop_requested
= reason
;
3210 qemu_notify_event();
3215 static int io_thread_fd
= -1;
3217 static void qemu_event_increment(void)
3219 /* Write 8 bytes to be compatible with eventfd. */
3220 static uint64_t val
= 1;
3223 if (io_thread_fd
== -1)
3227 ret
= write(io_thread_fd
, &val
, sizeof(val
));
3228 } while (ret
< 0 && errno
== EINTR
);
3230 /* EAGAIN is fine, a read must be pending. */
3231 if (ret
< 0 && errno
!= EAGAIN
) {
3232 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3238 static void qemu_event_read(void *opaque
)
3240 int fd
= (unsigned long)opaque
;
3244 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
3246 len
= read(fd
, buffer
, sizeof(buffer
));
3247 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
3250 static int qemu_event_init(void)
3255 err
= qemu_eventfd(fds
);
3259 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3263 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3267 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3268 (void *)(unsigned long)fds
[0]);
3270 io_thread_fd
= fds
[1];
3279 HANDLE qemu_event_handle
;
3281 static void dummy_event_handler(void *opaque
)
3285 static int qemu_event_init(void)
3287 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3288 if (!qemu_event_handle
) {
3289 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3292 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3296 static void qemu_event_increment(void)
3298 if (!SetEvent(qemu_event_handle
)) {
3299 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3306 static int cpu_can_run(CPUState
*env
)
3317 #ifndef CONFIG_IOTHREAD
3318 static int qemu_init_main_loop(void)
3320 return qemu_event_init();
3323 void qemu_init_vcpu(void *_env
)
3325 CPUState
*env
= _env
;
3327 env
->nr_cores
= smp_cores
;
3328 env
->nr_threads
= smp_threads
;
3334 int qemu_cpu_self(void *env
)
3339 static void resume_all_vcpus(void)
3343 static void pause_all_vcpus(void)
3347 void qemu_cpu_kick(void *env
)
3352 void qemu_notify_event(void)
3354 CPUState
*env
= cpu_single_env
;
3361 void qemu_mutex_lock_iothread(void) {}
3362 void qemu_mutex_unlock_iothread(void) {}
3364 void vm_stop(int reason
)
3369 #else /* CONFIG_IOTHREAD */
3371 #include "qemu-thread.h"
3373 QemuMutex qemu_global_mutex
;
3374 static QemuMutex qemu_fair_mutex
;
3376 static QemuThread io_thread
;
3378 static QemuThread
*tcg_cpu_thread
;
3379 static QemuCond
*tcg_halt_cond
;
3381 static int qemu_system_ready
;
3383 static QemuCond qemu_cpu_cond
;
3385 static QemuCond qemu_system_cond
;
3386 static QemuCond qemu_pause_cond
;
3388 static void tcg_block_io_signals(void);
3389 static void kvm_block_io_signals(CPUState
*env
);
3390 static void unblock_io_signals(void);
3391 static int tcg_has_work(void);
3392 static int cpu_has_work(CPUState
*env
);
3394 static int qemu_init_main_loop(void)
3398 ret
= qemu_event_init();
3402 qemu_cond_init(&qemu_pause_cond
);
3403 qemu_mutex_init(&qemu_fair_mutex
);
3404 qemu_mutex_init(&qemu_global_mutex
);
3405 qemu_mutex_lock(&qemu_global_mutex
);
3407 unblock_io_signals();
3408 qemu_thread_self(&io_thread
);
3413 static void qemu_wait_io_event_common(CPUState
*env
)
3418 qemu_cond_signal(&qemu_pause_cond
);
3422 static void qemu_wait_io_event(CPUState
*env
)
3424 while (!tcg_has_work())
3425 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3427 qemu_mutex_unlock(&qemu_global_mutex
);
3430 * Users of qemu_global_mutex can be starved, having no chance
3431 * to acquire it since this path will get to it first.
3432 * So use another lock to provide fairness.
3434 qemu_mutex_lock(&qemu_fair_mutex
);
3435 qemu_mutex_unlock(&qemu_fair_mutex
);
3437 qemu_mutex_lock(&qemu_global_mutex
);
3438 qemu_wait_io_event_common(env
);
3441 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
3448 ts
.tv_sec
= timeout
/ 1000;
3449 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
3451 sigemptyset(&waitset
);
3452 sigaddset(&waitset
, SIG_IPI
);
3454 qemu_mutex_unlock(&qemu_global_mutex
);
3455 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
3457 qemu_mutex_lock(&qemu_global_mutex
);
3459 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
3460 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
3465 static void qemu_kvm_wait_io_event(CPUState
*env
)
3467 while (!cpu_has_work(env
))
3468 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3470 qemu_kvm_eat_signal(env
, 0);
3471 qemu_wait_io_event_common(env
);
3474 static int qemu_cpu_exec(CPUState
*env
);
3476 static void *kvm_cpu_thread_fn(void *arg
)
3478 CPUState
*env
= arg
;
3480 qemu_thread_self(env
->thread
);
3484 kvm_block_io_signals(env
);
3486 /* signal CPU creation */
3487 qemu_mutex_lock(&qemu_global_mutex
);
3489 qemu_cond_signal(&qemu_cpu_cond
);
3491 /* and wait for machine initialization */
3492 while (!qemu_system_ready
)
3493 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3496 if (cpu_can_run(env
))
3498 qemu_kvm_wait_io_event(env
);
3504 static void tcg_cpu_exec(void);
3506 static void *tcg_cpu_thread_fn(void *arg
)
3508 CPUState
*env
= arg
;
3510 tcg_block_io_signals();
3511 qemu_thread_self(env
->thread
);
3513 /* signal CPU creation */
3514 qemu_mutex_lock(&qemu_global_mutex
);
3515 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3517 qemu_cond_signal(&qemu_cpu_cond
);
3519 /* and wait for machine initialization */
3520 while (!qemu_system_ready
)
3521 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3525 qemu_wait_io_event(cur_cpu
);
3531 void qemu_cpu_kick(void *_env
)
3533 CPUState
*env
= _env
;
3534 qemu_cond_broadcast(env
->halt_cond
);
3536 qemu_thread_signal(env
->thread
, SIG_IPI
);
3539 int qemu_cpu_self(void *_env
)
3541 CPUState
*env
= _env
;
3544 qemu_thread_self(&this);
3546 return qemu_thread_equal(&this, env
->thread
);
3549 static void cpu_signal(int sig
)
3552 cpu_exit(cpu_single_env
);
3555 static void tcg_block_io_signals(void)
3558 struct sigaction sigact
;
3561 sigaddset(&set
, SIGUSR2
);
3562 sigaddset(&set
, SIGIO
);
3563 sigaddset(&set
, SIGALRM
);
3564 sigaddset(&set
, SIGCHLD
);
3565 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3568 sigaddset(&set
, SIG_IPI
);
3569 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3571 memset(&sigact
, 0, sizeof(sigact
));
3572 sigact
.sa_handler
= cpu_signal
;
3573 sigaction(SIG_IPI
, &sigact
, NULL
);
3576 static void dummy_signal(int sig
)
3580 static void kvm_block_io_signals(CPUState
*env
)
3584 struct sigaction sigact
;
3587 sigaddset(&set
, SIGUSR2
);
3588 sigaddset(&set
, SIGIO
);
3589 sigaddset(&set
, SIGALRM
);
3590 sigaddset(&set
, SIGCHLD
);
3591 sigaddset(&set
, SIG_IPI
);
3592 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3594 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
3595 sigdelset(&set
, SIG_IPI
);
3597 memset(&sigact
, 0, sizeof(sigact
));
3598 sigact
.sa_handler
= dummy_signal
;
3599 sigaction(SIG_IPI
, &sigact
, NULL
);
3601 r
= kvm_set_signal_mask(env
, &set
);
3603 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
3608 static void unblock_io_signals(void)
3613 sigaddset(&set
, SIGUSR2
);
3614 sigaddset(&set
, SIGIO
);
3615 sigaddset(&set
, SIGALRM
);
3616 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3619 sigaddset(&set
, SIG_IPI
);
3620 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3623 static void qemu_signal_lock(unsigned int msecs
)
3625 qemu_mutex_lock(&qemu_fair_mutex
);
3627 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3628 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
3629 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3632 qemu_mutex_unlock(&qemu_fair_mutex
);
3635 void qemu_mutex_lock_iothread(void)
3637 if (kvm_enabled()) {
3638 qemu_mutex_lock(&qemu_fair_mutex
);
3639 qemu_mutex_lock(&qemu_global_mutex
);
3640 qemu_mutex_unlock(&qemu_fair_mutex
);
3642 qemu_signal_lock(100);
3645 void qemu_mutex_unlock_iothread(void)
3647 qemu_mutex_unlock(&qemu_global_mutex
);
3650 static int all_vcpus_paused(void)
3652 CPUState
*penv
= first_cpu
;
3657 penv
= (CPUState
*)penv
->next_cpu
;
3663 static void pause_all_vcpus(void)
3665 CPUState
*penv
= first_cpu
;
3669 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3670 qemu_cpu_kick(penv
);
3671 penv
= (CPUState
*)penv
->next_cpu
;
3674 while (!all_vcpus_paused()) {
3675 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3678 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3679 penv
= (CPUState
*)penv
->next_cpu
;
3684 static void resume_all_vcpus(void)
3686 CPUState
*penv
= first_cpu
;
3691 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3692 qemu_cpu_kick(penv
);
3693 penv
= (CPUState
*)penv
->next_cpu
;
3697 static void tcg_init_vcpu(void *_env
)
3699 CPUState
*env
= _env
;
3700 /* share a single thread for all cpus with TCG */
3701 if (!tcg_cpu_thread
) {
3702 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3703 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3704 qemu_cond_init(env
->halt_cond
);
3705 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3706 while (env
->created
== 0)
3707 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3708 tcg_cpu_thread
= env
->thread
;
3709 tcg_halt_cond
= env
->halt_cond
;
3711 env
->thread
= tcg_cpu_thread
;
3712 env
->halt_cond
= tcg_halt_cond
;
3716 static void kvm_start_vcpu(CPUState
*env
)
3718 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3719 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3720 qemu_cond_init(env
->halt_cond
);
3721 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3722 while (env
->created
== 0)
3723 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3726 void qemu_init_vcpu(void *_env
)
3728 CPUState
*env
= _env
;
3730 env
->nr_cores
= smp_cores
;
3731 env
->nr_threads
= smp_threads
;
3733 kvm_start_vcpu(env
);
3738 void qemu_notify_event(void)
3740 qemu_event_increment();
3743 void vm_stop(int reason
)
3746 qemu_thread_self(&me
);
3748 if (!qemu_thread_equal(&me
, &io_thread
)) {
3749 qemu_system_vmstop_request(reason
);
3751 * FIXME: should not return to device code in case
3752 * vm_stop() has been requested.
3754 if (cpu_single_env
) {
3755 cpu_exit(cpu_single_env
);
3756 cpu_single_env
->stop
= 1;
3767 static void host_main_loop_wait(int *timeout
)
3773 /* XXX: need to suppress polling by better using win32 events */
3775 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3776 ret
|= pe
->func(pe
->opaque
);
3780 WaitObjects
*w
= &wait_objects
;
3782 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3783 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3784 if (w
->func
[ret
- WAIT_OBJECT_0
])
3785 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3787 /* Check for additional signaled events */
3788 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3790 /* Check if event is signaled */
3791 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3792 if(ret2
== WAIT_OBJECT_0
) {
3794 w
->func
[i
](w
->opaque
[i
]);
3795 } else if (ret2
== WAIT_TIMEOUT
) {
3797 err
= GetLastError();
3798 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3801 } else if (ret
== WAIT_TIMEOUT
) {
3803 err
= GetLastError();
3804 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3811 static void host_main_loop_wait(int *timeout
)
3816 void main_loop_wait(int timeout
)
3818 IOHandlerRecord
*ioh
;
3819 fd_set rfds
, wfds
, xfds
;
3823 qemu_bh_update_timeout(&timeout
);
3825 host_main_loop_wait(&timeout
);
3827 /* poll any events */
3828 /* XXX: separate device handlers from system ones */
3833 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3837 (!ioh
->fd_read_poll
||
3838 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3839 FD_SET(ioh
->fd
, &rfds
);
3843 if (ioh
->fd_write
) {
3844 FD_SET(ioh
->fd
, &wfds
);
3850 tv
.tv_sec
= timeout
/ 1000;
3851 tv
.tv_usec
= (timeout
% 1000) * 1000;
3853 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3855 qemu_mutex_unlock_iothread();
3856 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3857 qemu_mutex_lock_iothread();
3859 IOHandlerRecord
**pioh
;
3861 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3862 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3863 ioh
->fd_read(ioh
->opaque
);
3865 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3866 ioh
->fd_write(ioh
->opaque
);
3870 /* remove deleted IO handlers */
3871 pioh
= &first_io_handler
;
3882 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3884 /* rearm timer, if not periodic */
3885 if (alarm_timer
->expired
) {
3886 alarm_timer
->expired
= 0;
3887 qemu_rearm_alarm_timer(alarm_timer
);
3890 /* vm time timers */
3892 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3893 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3894 qemu_get_clock(vm_clock
));
3897 /* real time timers */
3898 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3899 qemu_get_clock(rt_clock
));
3901 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3902 qemu_get_clock(host_clock
));
3904 /* Check bottom-halves last in case any of the earlier events triggered
3910 static int qemu_cpu_exec(CPUState
*env
)
3913 #ifdef CONFIG_PROFILER
3917 #ifdef CONFIG_PROFILER
3918 ti
= profile_getclock();
3923 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3924 env
->icount_decr
.u16
.low
= 0;
3925 env
->icount_extra
= 0;
3926 count
= qemu_next_deadline();
3927 count
= (count
+ (1 << icount_time_shift
) - 1)
3928 >> icount_time_shift
;
3929 qemu_icount
+= count
;
3930 decr
= (count
> 0xffff) ? 0xffff : count
;
3932 env
->icount_decr
.u16
.low
= decr
;
3933 env
->icount_extra
= count
;
3935 ret
= cpu_exec(env
);
3936 #ifdef CONFIG_PROFILER
3937 qemu_time
+= profile_getclock() - ti
;
3940 /* Fold pending instructions back into the
3941 instruction counter, and clear the interrupt flag. */
3942 qemu_icount
-= (env
->icount_decr
.u16
.low
3943 + env
->icount_extra
);
3944 env
->icount_decr
.u32
= 0;
3945 env
->icount_extra
= 0;
3950 static void tcg_cpu_exec(void)
3954 if (next_cpu
== NULL
)
3955 next_cpu
= first_cpu
;
3956 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3957 CPUState
*env
= cur_cpu
= next_cpu
;
3959 if (timer_alarm_pending
) {
3960 timer_alarm_pending
= 0;
3963 if (cpu_can_run(env
))
3964 ret
= qemu_cpu_exec(env
);
3968 if (ret
== EXCP_DEBUG
) {
3969 gdb_set_stop_cpu(env
);
3970 debug_requested
= 1;
3976 static int cpu_has_work(CPUState
*env
)
3984 if (qemu_cpu_has_work(env
))
3989 static int tcg_has_work(void)
3993 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3994 if (cpu_has_work(env
))
3999 static int qemu_calculate_timeout(void)
4001 #ifndef CONFIG_IOTHREAD
4006 else if (tcg_has_work())
4008 else if (!use_icount
)
4011 /* XXX: use timeout computed from timers */
4014 /* Advance virtual time to the next event. */
4015 if (use_icount
== 1) {
4016 /* When not using an adaptive execution frequency
4017 we tend to get badly out of sync with real time,
4018 so just delay for a reasonable amount of time. */
4021 delta
= cpu_get_icount() - cpu_get_clock();
4024 /* If virtual time is ahead of real time then just
4026 timeout
= (delta
/ 1000000) + 1;
4028 /* Wait for either IO to occur or the next
4030 add
= qemu_next_deadline();
4031 /* We advance the timer before checking for IO.
4032 Limit the amount we advance so that early IO
4033 activity won't get the guest too far ahead. */
4037 add
= (add
+ (1 << icount_time_shift
) - 1)
4038 >> icount_time_shift
;
4040 timeout
= delta
/ 1000000;
4047 #else /* CONFIG_IOTHREAD */
4052 static int vm_can_run(void)
4054 if (powerdown_requested
)
4056 if (reset_requested
)
4058 if (shutdown_requested
)
4060 if (debug_requested
)
4065 qemu_irq qemu_system_powerdown
;
4067 static void main_loop(void)
4071 #ifdef CONFIG_IOTHREAD
4072 qemu_system_ready
= 1;
4073 qemu_cond_broadcast(&qemu_system_cond
);
4078 #ifdef CONFIG_PROFILER
4081 #ifndef CONFIG_IOTHREAD
4084 #ifdef CONFIG_PROFILER
4085 ti
= profile_getclock();
4087 main_loop_wait(qemu_calculate_timeout());
4088 #ifdef CONFIG_PROFILER
4089 dev_time
+= profile_getclock() - ti
;
4091 } while (vm_can_run());
4093 if (qemu_debug_requested()) {
4094 vm_stop(EXCP_DEBUG
);
4096 if (qemu_shutdown_requested()) {
4097 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4104 if (qemu_reset_requested()) {
4106 qemu_system_reset();
4109 if (qemu_powerdown_requested()) {
4110 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4111 qemu_irq_raise(qemu_system_powerdown
);
4113 if ((r
= qemu_vmstop_requested())) {
4120 static void version(void)
4122 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4125 static void help(int exitcode
)
4127 const char *options_help
=
4128 #define DEF(option, opt_arg, opt_enum, opt_help) \
4130 #define DEFHEADING(text) stringify(text) "\n"
4131 #include "qemu-options.h"
4137 printf("usage: %s [options] [disk_image]\n"
4139 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4142 "During emulation, the following keys are useful:\n"
4143 "ctrl-alt-f toggle full screen\n"
4144 "ctrl-alt-n switch to virtual console 'n'\n"
4145 "ctrl-alt toggle mouse and keyboard grab\n"
4147 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4153 #define HAS_ARG 0x0001
4156 #define DEF(option, opt_arg, opt_enum, opt_help) \
4158 #define DEFHEADING(text)
4159 #include "qemu-options.h"
4165 typedef struct QEMUOption
{
4171 static const QEMUOption qemu_options
[] = {
4172 { "h", 0, QEMU_OPTION_h
},
4173 #define DEF(option, opt_arg, opt_enum, opt_help) \
4174 { option, opt_arg, opt_enum },
4175 #define DEFHEADING(text)
4176 #include "qemu-options.h"
4184 struct soundhw soundhw
[] = {
4185 #ifdef HAS_AUDIO_CHOICE
4186 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4192 { .init_isa
= pcspk_audio_init
}
4199 "Creative Sound Blaster 16",
4202 { .init_isa
= SB16_init
}
4206 #ifdef CONFIG_CS4231A
4212 { .init_isa
= cs4231a_init
}
4220 "Yamaha YMF262 (OPL3)",
4222 "Yamaha YM3812 (OPL2)",
4226 { .init_isa
= Adlib_init
}
4233 "Gravis Ultrasound GF1",
4236 { .init_isa
= GUS_init
}
4243 "Intel 82801AA AC97 Audio",
4246 { .init_pci
= ac97_init
}
4250 #ifdef CONFIG_ES1370
4253 "ENSONIQ AudioPCI ES1370",
4256 { .init_pci
= es1370_init
}
4260 #endif /* HAS_AUDIO_CHOICE */
4262 { NULL
, NULL
, 0, 0, { NULL
} }
4265 static void select_soundhw (const char *optarg
)
4269 if (*optarg
== '?') {
4272 printf ("Valid sound card names (comma separated):\n");
4273 for (c
= soundhw
; c
->name
; ++c
) {
4274 printf ("%-11s %s\n", c
->name
, c
->descr
);
4276 printf ("\n-soundhw all will enable all of the above\n");
4277 exit (*optarg
!= '?');
4285 if (!strcmp (optarg
, "all")) {
4286 for (c
= soundhw
; c
->name
; ++c
) {
4294 e
= strchr (p
, ',');
4295 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4297 for (c
= soundhw
; c
->name
; ++c
) {
4298 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4307 "Unknown sound card name (too big to show)\n");
4310 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4315 p
+= l
+ (e
!= NULL
);
4319 goto show_valid_cards
;
4324 static void select_vgahw (const char *p
)
4329 vga_interface_type
= VGA_NONE
;
4330 if (strstart(p
, "std", &opts
)) {
4331 vga_interface_type
= VGA_STD
;
4332 } else if (strstart(p
, "cirrus", &opts
)) {
4333 vga_interface_type
= VGA_CIRRUS
;
4334 } else if (strstart(p
, "vmware", &opts
)) {
4335 vga_interface_type
= VGA_VMWARE
;
4336 } else if (strstart(p
, "xenfb", &opts
)) {
4337 vga_interface_type
= VGA_XENFB
;
4338 } else if (!strstart(p
, "none", &opts
)) {
4340 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4344 const char *nextopt
;
4346 if (strstart(opts
, ",retrace=", &nextopt
)) {
4348 if (strstart(opts
, "dumb", &nextopt
))
4349 vga_retrace_method
= VGA_RETRACE_DUMB
;
4350 else if (strstart(opts
, "precise", &nextopt
))
4351 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4352 else goto invalid_vga
;
4353 } else goto invalid_vga
;
4359 static int balloon_parse(const char *arg
)
4363 if (strcmp(arg
, "none") == 0) {
4367 if (!strncmp(arg
, "virtio", 6)) {
4368 if (arg
[6] == ',') {
4369 /* have params -> parse them */
4370 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
4374 /* create empty opts */
4375 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4377 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4386 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4388 exit(STATUS_CONTROL_C_EXIT
);
4393 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4397 if(strlen(str
) != 36)
4400 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4401 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4402 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4408 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4416 static void termsig_handler(int signal
)
4418 qemu_system_shutdown_request();
4421 static void sigchld_handler(int signal
)
4423 waitpid(-1, NULL
, WNOHANG
);
4426 static void sighandler_setup(void)
4428 struct sigaction act
;
4430 memset(&act
, 0, sizeof(act
));
4431 act
.sa_handler
= termsig_handler
;
4432 sigaction(SIGINT
, &act
, NULL
);
4433 sigaction(SIGHUP
, &act
, NULL
);
4434 sigaction(SIGTERM
, &act
, NULL
);
4436 act
.sa_handler
= sigchld_handler
;
4437 act
.sa_flags
= SA_NOCLDSTOP
;
4438 sigaction(SIGCHLD
, &act
, NULL
);
4444 /* Look for support files in the same directory as the executable. */
4445 static char *find_datadir(const char *argv0
)
4451 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4458 while (p
!= buf
&& *p
!= '\\')
4461 if (access(buf
, R_OK
) == 0) {
4462 return qemu_strdup(buf
);
4468 /* Find a likely location for support files using the location of the binary.
4469 For installed binaries this will be "$bindir/../share/qemu". When
4470 running from the build tree this will be "$bindir/../pc-bios". */
4471 #define SHARE_SUFFIX "/share/qemu"
4472 #define BUILD_SUFFIX "/pc-bios"
4473 static char *find_datadir(const char *argv0
)
4481 #if defined(__linux__)
4484 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4490 #elif defined(__FreeBSD__)
4493 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4500 /* If we don't have any way of figuring out the actual executable
4501 location then try argv[0]. */
4503 p
= realpath(argv0
, buf
);
4511 max_len
= strlen(dir
) +
4512 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4513 res
= qemu_mallocz(max_len
);
4514 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4515 if (access(res
, R_OK
)) {
4516 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4517 if (access(res
, R_OK
)) {
4529 char *qemu_find_file(int type
, const char *name
)
4535 /* If name contains path separators then try it as a straight path. */
4536 if ((strchr(name
, '/') || strchr(name
, '\\'))
4537 && access(name
, R_OK
) == 0) {
4538 return qemu_strdup(name
);
4541 case QEMU_FILE_TYPE_BIOS
:
4544 case QEMU_FILE_TYPE_KEYMAP
:
4545 subdir
= "keymaps/";
4550 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4551 buf
= qemu_mallocz(len
);
4552 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4553 if (access(buf
, R_OK
)) {
4560 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4562 return qdev_device_help(opts
);
4565 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4569 dev
= qdev_device_add(opts
);
4575 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4577 CharDriverState
*chr
;
4579 chr
= qemu_chr_open_opts(opts
, NULL
);
4585 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4587 CharDriverState
*chr
;
4588 const char *chardev
;
4592 mode
= qemu_opt_get(opts
, "mode");
4596 if (strcmp(mode
, "readline") == 0) {
4597 flags
= MONITOR_USE_READLINE
;
4598 } else if (strcmp(mode
, "control") == 0) {
4599 flags
= MONITOR_USE_CONTROL
;
4601 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4605 if (qemu_opt_get_bool(opts
, "default", 0))
4606 flags
|= MONITOR_IS_DEFAULT
;
4608 chardev
= qemu_opt_get(opts
, "chardev");
4609 chr
= qemu_chr_find(chardev
);
4611 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4615 monitor_init(chr
, flags
);
4619 static void monitor_parse(const char *optarg
, const char *mode
)
4621 static int monitor_device_index
= 0;
4627 if (strstart(optarg
, "chardev:", &p
)) {
4628 snprintf(label
, sizeof(label
), "%s", p
);
4630 if (monitor_device_index
) {
4631 snprintf(label
, sizeof(label
), "monitor%d",
4632 monitor_device_index
);
4634 snprintf(label
, sizeof(label
), "monitor");
4637 opts
= qemu_chr_parse_compat(label
, optarg
);
4639 fprintf(stderr
, "parse error: %s\n", optarg
);
4644 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4646 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4649 qemu_opt_set(opts
, "mode", mode
);
4650 qemu_opt_set(opts
, "chardev", label
);
4652 qemu_opt_set(opts
, "default", "on");
4653 monitor_device_index
++;
4656 struct device_config
{
4658 DEV_USB
, /* -usbdevice */
4660 DEV_SERIAL
, /* -serial */
4661 DEV_PARALLEL
, /* -parallel */
4662 DEV_VIRTCON
, /* -virtioconsole */
4663 DEV_DEBUGCON
, /* -debugcon */
4665 const char *cmdline
;
4666 QTAILQ_ENTRY(device_config
) next
;
4668 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4670 static void add_device_config(int type
, const char *cmdline
)
4672 struct device_config
*conf
;
4674 conf
= qemu_mallocz(sizeof(*conf
));
4676 conf
->cmdline
= cmdline
;
4677 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4680 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4682 struct device_config
*conf
;
4685 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4686 if (conf
->type
!= type
)
4688 rc
= func(conf
->cmdline
);
4695 static int serial_parse(const char *devname
)
4697 static int index
= 0;
4700 if (strcmp(devname
, "none") == 0)
4702 if (index
== MAX_SERIAL_PORTS
) {
4703 fprintf(stderr
, "qemu: too many serial ports\n");
4706 snprintf(label
, sizeof(label
), "serial%d", index
);
4707 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4708 if (!serial_hds
[index
]) {
4709 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4710 devname
, strerror(errno
));
4717 static int parallel_parse(const char *devname
)
4719 static int index
= 0;
4722 if (strcmp(devname
, "none") == 0)
4724 if (index
== MAX_PARALLEL_PORTS
) {
4725 fprintf(stderr
, "qemu: too many parallel ports\n");
4728 snprintf(label
, sizeof(label
), "parallel%d", index
);
4729 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4730 if (!parallel_hds
[index
]) {
4731 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4732 devname
, strerror(errno
));
4739 static int virtcon_parse(const char *devname
)
4741 static int index
= 0;
4743 QemuOpts
*bus_opts
, *dev_opts
;
4745 if (strcmp(devname
, "none") == 0)
4747 if (index
== MAX_VIRTIO_CONSOLES
) {
4748 fprintf(stderr
, "qemu: too many virtio consoles\n");
4752 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4753 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4755 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4756 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4758 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4759 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4760 if (!virtcon_hds
[index
]) {
4761 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4762 devname
, strerror(errno
));
4765 qemu_opt_set(dev_opts
, "chardev", label
);
4771 static int debugcon_parse(const char *devname
)
4775 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4778 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4780 fprintf(stderr
, "qemu: already have a debugcon device\n");
4783 qemu_opt_set(opts
, "driver", "isa-debugcon");
4784 qemu_opt_set(opts
, "chardev", "debugcon");
4788 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4789 const char **poptarg
, int *poptind
)
4791 const QEMUOption
*popt
;
4792 int optind
= *poptind
;
4793 char *r
= argv
[optind
];
4796 loc_set_cmdline(argv
, optind
, 1);
4798 /* Treat --foo the same as -foo. */
4801 popt
= qemu_options
;
4804 error_report("invalid option");
4807 if (!strcmp(popt
->name
, r
+ 1))
4811 if (popt
->flags
& HAS_ARG
) {
4812 if (optind
>= argc
) {
4813 error_report("requires an argument");
4816 optarg
= argv
[optind
++];
4817 loc_set_cmdline(argv
, optind
- 2, 2);
4828 int main(int argc
, char **argv
, char **envp
)
4830 const char *gdbstub_dev
= NULL
;
4831 uint32_t boot_devices_bitmap
= 0;
4833 int snapshot
, linux_boot
, net_boot
;
4834 const char *initrd_filename
;
4835 const char *kernel_filename
, *kernel_cmdline
;
4836 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4838 DisplayChangeListener
*dcl
;
4839 int cyls
, heads
, secs
, translation
;
4840 QemuOpts
*hda_opts
= NULL
, *opts
;
4843 const char *loadvm
= NULL
;
4844 QEMUMachine
*machine
;
4845 const char *cpu_model
;
4850 const char *pid_file
= NULL
;
4851 const char *incoming
= NULL
;
4854 struct passwd
*pwd
= NULL
;
4855 const char *chroot_dir
= NULL
;
4856 const char *run_as
= NULL
;
4859 int show_vnc_port
= 0;
4862 error_set_progname(argv
[0]);
4866 qemu_cache_utils_init(envp
);
4868 QLIST_INIT (&vm_change_state_head
);
4871 struct sigaction act
;
4872 sigfillset(&act
.sa_mask
);
4874 act
.sa_handler
= SIG_IGN
;
4875 sigaction(SIGPIPE
, &act
, NULL
);
4878 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4879 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4880 QEMU to run on a single CPU */
4885 h
= GetCurrentProcess();
4886 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4887 for(i
= 0; i
< 32; i
++) {
4888 if (mask
& (1 << i
))
4893 SetProcessAffinityMask(h
, mask
);
4899 module_call_init(MODULE_INIT_MACHINE
);
4900 machine
= find_default_machine();
4902 initrd_filename
= NULL
;
4905 kernel_filename
= NULL
;
4906 kernel_cmdline
= "";
4907 cyls
= heads
= secs
= 0;
4908 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4910 for (i
= 0; i
< MAX_NODES
; i
++) {
4912 node_cpumask
[i
] = 0;
4921 /* first pass of option parsing */
4923 while (optind
< argc
) {
4924 if (argv
[optind
][0] != '-') {
4929 const QEMUOption
*popt
;
4931 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4932 switch (popt
->index
) {
4933 case QEMU_OPTION_nodefconfig
:
4944 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
4945 fp
= fopen(fname
, "r");
4947 if (qemu_config_parse(fp
, fname
) != 0) {
4953 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
4954 fp
= fopen(fname
, "r");
4956 if (qemu_config_parse(fp
, fname
) != 0) {
4962 #if defined(cpudef_setup)
4963 cpudef_setup(); /* parse cpu definitions in target config file */
4966 /* second pass of option parsing */
4971 if (argv
[optind
][0] != '-') {
4972 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4974 const QEMUOption
*popt
;
4976 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4977 switch(popt
->index
) {
4979 machine
= find_machine(optarg
);
4982 printf("Supported machines are:\n");
4983 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4985 printf("%-10s %s (alias of %s)\n",
4986 m
->alias
, m
->desc
, m
->name
);
4987 printf("%-10s %s%s\n",
4989 m
->is_default
? " (default)" : "");
4991 exit(*optarg
!= '?');
4994 case QEMU_OPTION_cpu
:
4995 /* hw initialization will check this */
4996 if (*optarg
== '?') {
4997 /* XXX: implement xxx_cpu_list for targets that still miss it */
4998 #if defined(cpu_list_id)
4999 cpu_list_id(stdout
, &fprintf
, optarg
);
5000 #elif defined(cpu_list)
5001 cpu_list(stdout
, &fprintf
); /* deprecated */
5008 case QEMU_OPTION_initrd
:
5009 initrd_filename
= optarg
;
5011 case QEMU_OPTION_hda
:
5013 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5015 hda_opts
= drive_add(optarg
, HD_ALIAS
5016 ",cyls=%d,heads=%d,secs=%d%s",
5017 0, cyls
, heads
, secs
,
5018 translation
== BIOS_ATA_TRANSLATION_LBA
?
5020 translation
== BIOS_ATA_TRANSLATION_NONE
?
5021 ",trans=none" : "");
5023 case QEMU_OPTION_hdb
:
5024 case QEMU_OPTION_hdc
:
5025 case QEMU_OPTION_hdd
:
5026 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5028 case QEMU_OPTION_drive
:
5029 drive_add(NULL
, "%s", optarg
);
5031 case QEMU_OPTION_set
:
5032 if (qemu_set_option(optarg
) != 0)
5035 case QEMU_OPTION_global
:
5036 if (qemu_global_option(optarg
) != 0)
5039 case QEMU_OPTION_mtdblock
:
5040 drive_add(optarg
, MTD_ALIAS
);
5042 case QEMU_OPTION_sd
:
5043 drive_add(optarg
, SD_ALIAS
);
5045 case QEMU_OPTION_pflash
:
5046 drive_add(optarg
, PFLASH_ALIAS
);
5048 case QEMU_OPTION_snapshot
:
5051 case QEMU_OPTION_hdachs
:
5055 cyls
= strtol(p
, (char **)&p
, 0);
5056 if (cyls
< 1 || cyls
> 16383)
5061 heads
= strtol(p
, (char **)&p
, 0);
5062 if (heads
< 1 || heads
> 16)
5067 secs
= strtol(p
, (char **)&p
, 0);
5068 if (secs
< 1 || secs
> 63)
5072 if (!strcmp(p
, "none"))
5073 translation
= BIOS_ATA_TRANSLATION_NONE
;
5074 else if (!strcmp(p
, "lba"))
5075 translation
= BIOS_ATA_TRANSLATION_LBA
;
5076 else if (!strcmp(p
, "auto"))
5077 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5080 } else if (*p
!= '\0') {
5082 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5085 if (hda_opts
!= NULL
) {
5087 snprintf(num
, sizeof(num
), "%d", cyls
);
5088 qemu_opt_set(hda_opts
, "cyls", num
);
5089 snprintf(num
, sizeof(num
), "%d", heads
);
5090 qemu_opt_set(hda_opts
, "heads", num
);
5091 snprintf(num
, sizeof(num
), "%d", secs
);
5092 qemu_opt_set(hda_opts
, "secs", num
);
5093 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5094 qemu_opt_set(hda_opts
, "trans", "lba");
5095 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5096 qemu_opt_set(hda_opts
, "trans", "none");
5100 case QEMU_OPTION_numa
:
5101 if (nb_numa_nodes
>= MAX_NODES
) {
5102 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5107 case QEMU_OPTION_nographic
:
5108 display_type
= DT_NOGRAPHIC
;
5110 #ifdef CONFIG_CURSES
5111 case QEMU_OPTION_curses
:
5112 display_type
= DT_CURSES
;
5115 case QEMU_OPTION_portrait
:
5118 case QEMU_OPTION_kernel
:
5119 kernel_filename
= optarg
;
5121 case QEMU_OPTION_append
:
5122 kernel_cmdline
= optarg
;
5124 case QEMU_OPTION_cdrom
:
5125 drive_add(optarg
, CDROM_ALIAS
);
5127 case QEMU_OPTION_boot
:
5129 static const char * const params
[] = {
5130 "order", "once", "menu", NULL
5132 char buf
[sizeof(boot_devices
)];
5133 char *standard_boot_devices
;
5136 if (!strchr(optarg
, '=')) {
5138 pstrcpy(buf
, sizeof(buf
), optarg
);
5139 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5141 "qemu: unknown boot parameter '%s' in '%s'\n",
5147 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5148 boot_devices_bitmap
= parse_bootdevices(buf
);
5149 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5152 if (get_param_value(buf
, sizeof(buf
),
5154 boot_devices_bitmap
|= parse_bootdevices(buf
);
5155 standard_boot_devices
= qemu_strdup(boot_devices
);
5156 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5157 qemu_register_reset(restore_boot_devices
,
5158 standard_boot_devices
);
5160 if (get_param_value(buf
, sizeof(buf
),
5162 if (!strcmp(buf
, "on")) {
5164 } else if (!strcmp(buf
, "off")) {
5168 "qemu: invalid option value '%s'\n",
5176 case QEMU_OPTION_fda
:
5177 case QEMU_OPTION_fdb
:
5178 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5181 case QEMU_OPTION_no_fd_bootchk
:
5185 case QEMU_OPTION_netdev
:
5186 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5190 case QEMU_OPTION_net
:
5191 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5196 case QEMU_OPTION_tftp
:
5197 legacy_tftp_prefix
= optarg
;
5199 case QEMU_OPTION_bootp
:
5200 legacy_bootp_filename
= optarg
;
5203 case QEMU_OPTION_smb
:
5204 if (net_slirp_smb(optarg
) < 0)
5208 case QEMU_OPTION_redir
:
5209 if (net_slirp_redir(optarg
) < 0)
5213 case QEMU_OPTION_bt
:
5214 add_device_config(DEV_BT
, optarg
);
5217 case QEMU_OPTION_audio_help
:
5221 case QEMU_OPTION_soundhw
:
5222 select_soundhw (optarg
);
5228 case QEMU_OPTION_version
:
5232 case QEMU_OPTION_m
: {
5236 value
= strtoul(optarg
, &ptr
, 10);
5238 case 0: case 'M': case 'm':
5245 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5249 /* On 32-bit hosts, QEMU is limited by virtual address space */
5250 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5251 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5254 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5255 fprintf(stderr
, "qemu: ram size too large\n");
5261 case QEMU_OPTION_mempath
:
5265 case QEMU_OPTION_mem_prealloc
:
5272 const CPULogItem
*item
;
5274 mask
= cpu_str_to_log_mask(optarg
);
5276 printf("Log items (comma separated):\n");
5277 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5278 printf("%-10s %s\n", item
->name
, item
->help
);
5286 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5288 case QEMU_OPTION_gdb
:
5289 gdbstub_dev
= optarg
;
5294 case QEMU_OPTION_bios
:
5297 case QEMU_OPTION_singlestep
:
5304 keyboard_layout
= optarg
;
5306 case QEMU_OPTION_localtime
:
5309 case QEMU_OPTION_vga
:
5310 select_vgahw (optarg
);
5312 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5318 w
= strtol(p
, (char **)&p
, 10);
5321 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5327 h
= strtol(p
, (char **)&p
, 10);
5332 depth
= strtol(p
, (char **)&p
, 10);
5333 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5334 depth
!= 24 && depth
!= 32)
5336 } else if (*p
== '\0') {
5337 depth
= graphic_depth
;
5344 graphic_depth
= depth
;
5348 case QEMU_OPTION_echr
:
5351 term_escape_char
= strtol(optarg
, &r
, 0);
5353 printf("Bad argument to echr\n");
5356 case QEMU_OPTION_monitor
:
5357 monitor_parse(optarg
, "readline");
5358 default_monitor
= 0;
5360 case QEMU_OPTION_qmp
:
5361 monitor_parse(optarg
, "control");
5362 default_monitor
= 0;
5364 case QEMU_OPTION_mon
:
5365 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
5367 fprintf(stderr
, "parse error: %s\n", optarg
);
5370 default_monitor
= 0;
5372 case QEMU_OPTION_chardev
:
5373 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
5375 fprintf(stderr
, "parse error: %s\n", optarg
);
5379 case QEMU_OPTION_serial
:
5380 add_device_config(DEV_SERIAL
, optarg
);
5382 if (strncmp(optarg
, "mon:", 4) == 0) {
5383 default_monitor
= 0;
5386 case QEMU_OPTION_watchdog
:
5389 "qemu: only one watchdog option may be given\n");
5394 case QEMU_OPTION_watchdog_action
:
5395 if (select_watchdog_action(optarg
) == -1) {
5396 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5400 case QEMU_OPTION_virtiocon
:
5401 add_device_config(DEV_VIRTCON
, optarg
);
5402 default_virtcon
= 0;
5403 if (strncmp(optarg
, "mon:", 4) == 0) {
5404 default_monitor
= 0;
5407 case QEMU_OPTION_parallel
:
5408 add_device_config(DEV_PARALLEL
, optarg
);
5409 default_parallel
= 0;
5410 if (strncmp(optarg
, "mon:", 4) == 0) {
5411 default_monitor
= 0;
5414 case QEMU_OPTION_debugcon
:
5415 add_device_config(DEV_DEBUGCON
, optarg
);
5417 case QEMU_OPTION_loadvm
:
5420 case QEMU_OPTION_full_screen
:
5424 case QEMU_OPTION_no_frame
:
5427 case QEMU_OPTION_alt_grab
:
5430 case QEMU_OPTION_ctrl_grab
:
5433 case QEMU_OPTION_no_quit
:
5436 case QEMU_OPTION_sdl
:
5437 display_type
= DT_SDL
;
5440 case QEMU_OPTION_pidfile
:
5444 case QEMU_OPTION_win2k_hack
:
5445 win2k_install_hack
= 1;
5447 case QEMU_OPTION_rtc_td_hack
:
5450 case QEMU_OPTION_acpitable
:
5451 if(acpi_table_add(optarg
) < 0) {
5452 fprintf(stderr
, "Wrong acpi table provided\n");
5456 case QEMU_OPTION_smbios
:
5457 if(smbios_entry_add(optarg
) < 0) {
5458 fprintf(stderr
, "Wrong smbios provided\n");
5464 case QEMU_OPTION_enable_kvm
:
5468 case QEMU_OPTION_usb
:
5471 case QEMU_OPTION_usbdevice
:
5473 add_device_config(DEV_USB
, optarg
);
5475 case QEMU_OPTION_device
:
5476 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
5480 case QEMU_OPTION_smp
:
5483 fprintf(stderr
, "Invalid number of CPUs\n");
5486 if (max_cpus
< smp_cpus
) {
5487 fprintf(stderr
, "maxcpus must be equal to or greater than "
5491 if (max_cpus
> 255) {
5492 fprintf(stderr
, "Unsupported number of maxcpus\n");
5496 case QEMU_OPTION_vnc
:
5497 display_type
= DT_VNC
;
5498 vnc_display
= optarg
;
5501 case QEMU_OPTION_no_acpi
:
5504 case QEMU_OPTION_no_hpet
:
5507 case QEMU_OPTION_balloon
:
5508 if (balloon_parse(optarg
) < 0) {
5509 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5514 case QEMU_OPTION_no_reboot
:
5517 case QEMU_OPTION_no_shutdown
:
5520 case QEMU_OPTION_show_cursor
:
5523 case QEMU_OPTION_uuid
:
5524 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5525 fprintf(stderr
, "Fail to parse UUID string."
5526 " Wrong format.\n");
5531 case QEMU_OPTION_daemonize
:
5535 case QEMU_OPTION_option_rom
:
5536 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5537 fprintf(stderr
, "Too many option ROMs\n");
5540 option_rom
[nb_option_roms
] = optarg
;
5543 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5544 case QEMU_OPTION_semihosting
:
5545 semihosting_enabled
= 1;
5548 case QEMU_OPTION_name
:
5549 qemu_name
= qemu_strdup(optarg
);
5551 char *p
= strchr(qemu_name
, ',');
5554 if (strncmp(p
, "process=", 8)) {
5555 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5563 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5564 case QEMU_OPTION_prom_env
:
5565 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5566 fprintf(stderr
, "Too many prom variables\n");
5569 prom_envs
[nb_prom_envs
] = optarg
;
5574 case QEMU_OPTION_old_param
:
5578 case QEMU_OPTION_clock
:
5579 configure_alarms(optarg
);
5581 case QEMU_OPTION_startdate
:
5582 configure_rtc_date_offset(optarg
, 1);
5584 case QEMU_OPTION_rtc
:
5585 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
5587 fprintf(stderr
, "parse error: %s\n", optarg
);
5590 configure_rtc(opts
);
5592 case QEMU_OPTION_tb_size
:
5593 tb_size
= strtol(optarg
, NULL
, 0);
5597 case QEMU_OPTION_icount
:
5599 if (strcmp(optarg
, "auto") == 0) {
5600 icount_time_shift
= -1;
5602 icount_time_shift
= strtol(optarg
, NULL
, 0);
5605 case QEMU_OPTION_incoming
:
5608 case QEMU_OPTION_nodefaults
:
5610 default_parallel
= 0;
5611 default_virtcon
= 0;
5612 default_monitor
= 0;
5620 case QEMU_OPTION_chroot
:
5621 chroot_dir
= optarg
;
5623 case QEMU_OPTION_runas
:
5628 case QEMU_OPTION_xen_domid
:
5629 xen_domid
= atoi(optarg
);
5631 case QEMU_OPTION_xen_create
:
5632 xen_mode
= XEN_CREATE
;
5634 case QEMU_OPTION_xen_attach
:
5635 xen_mode
= XEN_ATTACH
;
5638 case QEMU_OPTION_readconfig
:
5641 fp
= fopen(optarg
, "r");
5643 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5646 if (qemu_config_parse(fp
, optarg
) != 0) {
5652 case QEMU_OPTION_writeconfig
:
5655 if (strcmp(optarg
, "-") == 0) {
5658 fp
= fopen(optarg
, "w");
5660 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5664 qemu_config_write(fp
);
5673 /* If no data_dir is specified then try to find it relative to the
5676 data_dir
= find_datadir(argv
[0]);
5678 /* If all else fails use the install patch specified when building. */
5680 data_dir
= CONFIG_QEMU_SHAREDIR
;
5684 * Default to max_cpus = smp_cpus, in case the user doesn't
5685 * specify a max_cpus value.
5688 max_cpus
= smp_cpus
;
5690 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5691 if (smp_cpus
> machine
->max_cpus
) {
5692 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5693 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5698 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5699 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5701 if (machine
->no_serial
) {
5704 if (machine
->no_parallel
) {
5705 default_parallel
= 0;
5707 if (!machine
->use_virtcon
) {
5708 default_virtcon
= 0;
5710 if (machine
->no_vga
) {
5713 if (machine
->no_floppy
) {
5716 if (machine
->no_cdrom
) {
5719 if (machine
->no_sdcard
) {
5723 if (display_type
== DT_NOGRAPHIC
) {
5724 if (default_parallel
)
5725 add_device_config(DEV_PARALLEL
, "null");
5726 if (default_serial
&& default_monitor
) {
5727 add_device_config(DEV_SERIAL
, "mon:stdio");
5728 } else if (default_virtcon
&& default_monitor
) {
5729 add_device_config(DEV_VIRTCON
, "mon:stdio");
5732 add_device_config(DEV_SERIAL
, "stdio");
5733 if (default_virtcon
)
5734 add_device_config(DEV_VIRTCON
, "stdio");
5735 if (default_monitor
)
5736 monitor_parse("stdio", "readline");
5740 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5741 if (default_parallel
)
5742 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5743 if (default_monitor
)
5744 monitor_parse("vc:80Cx24C", "readline");
5745 if (default_virtcon
)
5746 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5749 vga_interface_type
= VGA_CIRRUS
;
5751 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5758 if (pipe(fds
) == -1)
5769 len
= read(fds
[0], &status
, 1);
5770 if (len
== -1 && (errno
== EINTR
))
5775 else if (status
== 1) {
5776 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5784 qemu_set_cloexec(fds
[1]);
5796 signal(SIGTSTP
, SIG_IGN
);
5797 signal(SIGTTOU
, SIG_IGN
);
5798 signal(SIGTTIN
, SIG_IGN
);
5802 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5806 if (write(fds
[1], &status
, 1) != 1) {
5807 perror("daemonize. Writing to pipe\n");
5811 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5815 if (kvm_enabled()) {
5818 ret
= kvm_init(smp_cpus
);
5820 fprintf(stderr
, "failed to initialize KVM\n");
5825 if (qemu_init_main_loop()) {
5826 fprintf(stderr
, "qemu_init_main_loop failed\n");
5829 linux_boot
= (kernel_filename
!= NULL
);
5831 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5832 fprintf(stderr
, "-append only allowed with -kernel option\n");
5836 if (!linux_boot
&& initrd_filename
!= NULL
) {
5837 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5842 /* Win32 doesn't support line-buffering and requires size >= 2 */
5843 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5846 if (init_timer_alarm() < 0) {
5847 fprintf(stderr
, "could not initialize alarm timer\n");
5850 if (use_icount
&& icount_time_shift
< 0) {
5852 /* 125MIPS seems a reasonable initial guess at the guest speed.
5853 It will be corrected fairly quickly anyway. */
5854 icount_time_shift
= 3;
5855 init_icount_adjust();
5862 if (net_init_clients() < 0) {
5866 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5867 net_set_boot_mask(net_boot
);
5869 /* init the bluetooth world */
5870 if (foreach_device_config(DEV_BT
, bt_parse
))
5873 /* init the memory */
5875 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5877 /* init the dynamic translator */
5878 cpu_exec_init_all(tb_size
* 1024 * 1024);
5880 bdrv_init_with_whitelist();
5884 if (default_cdrom
) {
5885 /* we always create the cdrom drive, even if no disk is there */
5886 drive_add(NULL
, CDROM_ALIAS
);
5889 if (default_floppy
) {
5890 /* we always create at least one floppy */
5891 drive_add(NULL
, FD_ALIAS
, 0);
5894 if (default_sdcard
) {
5895 /* we always create one sd slot, even if no card is in it */
5896 drive_add(NULL
, SD_ALIAS
);
5899 /* open the virtual block devices */
5901 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5902 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5905 vmstate_register(0, &vmstate_timers
,&timers_state
);
5906 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5909 if (nb_numa_nodes
> 0) {
5912 if (nb_numa_nodes
> smp_cpus
) {
5913 nb_numa_nodes
= smp_cpus
;
5916 /* If no memory size if given for any node, assume the default case
5917 * and distribute the available memory equally across all nodes
5919 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5920 if (node_mem
[i
] != 0)
5923 if (i
== nb_numa_nodes
) {
5924 uint64_t usedmem
= 0;
5926 /* On Linux, the each node's border has to be 8MB aligned,
5927 * the final node gets the rest.
5929 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5930 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5931 usedmem
+= node_mem
[i
];
5933 node_mem
[i
] = ram_size
- usedmem
;
5936 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5937 if (node_cpumask
[i
] != 0)
5940 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5941 * must cope with this anyway, because there are BIOSes out there in
5942 * real machines which also use this scheme.
5944 if (i
== nb_numa_nodes
) {
5945 for (i
= 0; i
< smp_cpus
; i
++) {
5946 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5951 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5953 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5955 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5957 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5960 module_call_init(MODULE_INIT_DEVICE
);
5962 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5966 i
= select_watchdog(watchdog
);
5968 exit (i
== 1 ? 1 : 0);
5971 if (machine
->compat_props
) {
5972 qdev_prop_register_global_list(machine
->compat_props
);
5976 machine
->init(ram_size
, boot_devices
,
5977 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5979 cpu_synchronize_all_post_init();
5982 /* must be after terminal init, SDL library changes signal handlers */
5986 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5987 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5988 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5994 current_machine
= machine
;
5996 /* init USB devices */
5998 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
6002 /* init generic devices */
6003 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
6006 net_check_clients();
6008 /* just use the first displaystate for the moment */
6009 ds
= get_displaystate();
6011 if (display_type
== DT_DEFAULT
) {
6012 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6013 display_type
= DT_SDL
;
6015 display_type
= DT_VNC
;
6016 vnc_display
= "localhost:0,to=99";
6022 switch (display_type
) {
6025 #if defined(CONFIG_CURSES)
6027 curses_display_init(ds
, full_screen
);
6030 #if defined(CONFIG_SDL)
6032 sdl_display_init(ds
, full_screen
, no_frame
);
6034 #elif defined(CONFIG_COCOA)
6036 cocoa_display_init(ds
, full_screen
);
6040 vnc_display_init(ds
);
6041 if (vnc_display_open(ds
, vnc_display
) < 0)
6044 if (show_vnc_port
) {
6045 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6053 dcl
= ds
->listeners
;
6054 while (dcl
!= NULL
) {
6055 if (dcl
->dpy_refresh
!= NULL
) {
6056 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6057 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6062 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6063 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6064 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6067 text_consoles_set_display(ds
);
6069 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
6072 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6073 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6078 qdev_machine_creation_done();
6080 if (rom_load_all() != 0) {
6081 fprintf(stderr
, "rom loading failed\n");
6085 qemu_system_reset();
6087 if (load_vmstate(loadvm
) < 0) {
6093 qemu_start_incoming_migration(incoming
);
6094 } else if (autostart
) {
6104 len
= write(fds
[1], &status
, 1);
6105 if (len
== -1 && (errno
== EINTR
))
6112 perror("not able to chdir to /");
6115 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
6121 pwd
= getpwnam(run_as
);
6123 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6129 if (chroot(chroot_dir
) < 0) {
6130 fprintf(stderr
, "chroot failed\n");
6134 perror("not able to chdir to /");
6140 if (setgid(pwd
->pw_gid
) < 0) {
6141 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6144 if (setuid(pwd
->pw_uid
) < 0) {
6145 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6148 if (setuid(0) != -1) {
6149 fprintf(stderr
, "Dropping privileges failed\n");