4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <linux/rtc.h>
63 #include <sys/prctl.h>
65 /* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67 /* #include <linux/hpet.h> */
70 #include <linux/ppdev.h>
71 #include <linux/parport.h>
75 #include <sys/ethernet.h>
76 #include <sys/sockio.h>
77 #include <netinet/arp.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> // must come after ip.h
82 #include <netinet/udp.h>
83 #include <netinet/tcp.h>
87 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89 extern int madvise(caddr_t
, size_t, int);
94 #if defined(__OpenBSD__)
98 #if defined(CONFIG_VDE)
99 #include <libvdeplug.h>
104 #include <mmsystem.h>
108 #if defined(__APPLE__) || defined(main)
110 int qemu_main(int argc
, char **argv
, char **envp
);
111 int main(int argc
, char **argv
)
113 return qemu_main(argc
, argv
, NULL
);
116 #define main qemu_main
118 #endif /* CONFIG_SDL */
122 #define main qemu_main
123 #endif /* CONFIG_COCOA */
126 #include "hw/boards.h"
128 #include "hw/pcmcia.h"
130 #include "hw/audiodev.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "block_int.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
157 #include "qemu-option.h"
158 #include "qemu-config.h"
159 #include "qemu-objects.h"
163 #include "exec-all.h"
165 #include "qemu_socket.h"
167 #include "slirp/libslirp.h"
169 #include "qemu-queue.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 #define MAX_VIRTIO_CONSOLES 1
178 static const char *data_dir
;
179 const char *bios_name
= NULL
;
180 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
181 to store the VM snapshots */
182 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
183 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
184 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
185 DisplayType display_type
= DT_DEFAULT
;
186 const char* keyboard_layout
= NULL
;
188 const char *mem_path
= NULL
;
190 int mem_prealloc
= 0; /* force preallocation of physical target memory */
193 NICInfo nd_table
[MAX_NICS
];
196 static int rtc_utc
= 1;
197 static int rtc_date_offset
= -1; /* -1 means no change */
198 QEMUClock
*rtc_clock
;
199 int vga_interface_type
= VGA_NONE
;
201 int graphic_width
= 1024;
202 int graphic_height
= 768;
203 int graphic_depth
= 8;
205 int graphic_width
= 800;
206 int graphic_height
= 600;
207 int graphic_depth
= 15;
209 static int full_screen
= 0;
211 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
216 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
218 int win2k_install_hack
= 0;
227 const char *vnc_display
;
228 int acpi_enabled
= 1;
234 int graphic_rotate
= 0;
235 uint8_t irq0override
= 1;
239 const char *watchdog
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
249 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
250 unsigned int nb_prom_envs
= 0;
251 const char *prom_envs
[MAX_PROM_ENVS
];
256 uint64_t node_mem
[MAX_NODES
];
257 uint64_t node_cpumask
[MAX_NODES
];
259 static CPUState
*cur_cpu
;
260 static CPUState
*next_cpu
;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
269 static QEMUTimer
*nographic_timer
;
271 uint8_t qemu_uuid
[16];
273 static QEMUBootSetHandler
*boot_set_handler
;
274 static void *boot_set_opaque
;
277 #define SIG_IPI (SIGRTMIN+4)
279 #define SIG_IPI SIGUSR1
282 static int default_serial
= 1;
283 static int default_parallel
= 1;
284 static int default_virtcon
= 1;
285 static int default_monitor
= 1;
286 static int default_vga
= 1;
287 static int default_floppy
= 1;
288 static int default_cdrom
= 1;
289 static int default_sdcard
= 1;
295 { .driver
= "isa-serial", .flag
= &default_serial
},
296 { .driver
= "isa-parallel", .flag
= &default_parallel
},
297 { .driver
= "isa-fdc", .flag
= &default_floppy
},
298 { .driver
= "ide-drive", .flag
= &default_cdrom
},
299 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
300 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
301 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
302 { .driver
= "VGA", .flag
= &default_vga
},
303 { .driver
= "cirrus-vga", .flag
= &default_vga
},
304 { .driver
= "vmware-svga", .flag
= &default_vga
},
307 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
309 const char *driver
= qemu_opt_get(opts
, "driver");
314 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
315 if (strcmp(default_list
[i
].driver
, driver
) != 0)
317 *(default_list
[i
].flag
) = 0;
322 /***********************************************************/
323 /* x86 ISA bus support */
325 target_phys_addr_t isa_mem_base
= 0;
328 /***********************************************************/
329 void hw_error(const char *fmt
, ...)
335 fprintf(stderr
, "qemu: hardware error: ");
336 vfprintf(stderr
, fmt
, ap
);
337 fprintf(stderr
, "\n");
338 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
339 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
341 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
343 cpu_dump_state(env
, stderr
, fprintf
, 0);
350 static void set_proc_name(const char *s
)
352 #if defined(__linux__) && defined(PR_SET_NAME)
356 name
[sizeof(name
) - 1] = 0;
357 strncpy(name
, s
, sizeof(name
));
358 /* Could rewrite argv[0] too, but that's a bit more complicated.
359 This simple way is enough for `top'. */
360 prctl(PR_SET_NAME
, name
);
367 static QEMUBalloonEvent
*qemu_balloon_event
;
368 void *qemu_balloon_event_opaque
;
370 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
372 qemu_balloon_event
= func
;
373 qemu_balloon_event_opaque
= opaque
;
376 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
378 if (qemu_balloon_event
) {
379 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
386 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
388 if (qemu_balloon_event
) {
389 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
397 /***********************************************************/
398 /* real time host monotonic timer */
400 /* compute with 96 bit intermediate result: (a*b)/c */
401 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
406 #ifdef HOST_WORDS_BIGENDIAN
416 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
417 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
420 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
424 static int64_t get_clock_realtime(void)
428 gettimeofday(&tv
, NULL
);
429 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
434 static int64_t clock_freq
;
436 static void init_get_clock(void)
440 ret
= QueryPerformanceFrequency(&freq
);
442 fprintf(stderr
, "Could not calibrate ticks\n");
445 clock_freq
= freq
.QuadPart
;
448 static int64_t get_clock(void)
451 QueryPerformanceCounter(&ti
);
452 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
457 static int use_rt_clock
;
459 static void init_get_clock(void)
462 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
463 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
466 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
473 static int64_t get_clock(void)
475 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
476 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
479 clock_gettime(CLOCK_MONOTONIC
, &ts
);
480 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
484 /* XXX: using gettimeofday leads to problems if the date
485 changes, so it should be avoided. */
486 return get_clock_realtime();
491 /* Return the virtual CPU time, based on the instruction counter. */
492 static int64_t cpu_get_icount(void)
495 CPUState
*env
= cpu_single_env
;;
496 icount
= qemu_icount
;
499 fprintf(stderr
, "Bad clock read\n");
500 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
502 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
505 /***********************************************************/
506 /* guest cycle counter */
508 typedef struct TimersState
{
509 int64_t cpu_ticks_prev
;
510 int64_t cpu_ticks_offset
;
511 int64_t cpu_clock_offset
;
512 int32_t cpu_ticks_enabled
;
516 TimersState timers_state
;
518 /* return the host CPU cycle counter and handle stop/restart */
519 int64_t cpu_get_ticks(void)
522 return cpu_get_icount();
524 if (!timers_state
.cpu_ticks_enabled
) {
525 return timers_state
.cpu_ticks_offset
;
528 ticks
= cpu_get_real_ticks();
529 if (timers_state
.cpu_ticks_prev
> ticks
) {
530 /* Note: non increasing ticks may happen if the host uses
532 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
534 timers_state
.cpu_ticks_prev
= ticks
;
535 return ticks
+ timers_state
.cpu_ticks_offset
;
539 /* return the host CPU monotonic timer and handle stop/restart */
540 static int64_t cpu_get_clock(void)
543 if (!timers_state
.cpu_ticks_enabled
) {
544 return timers_state
.cpu_clock_offset
;
547 return ti
+ timers_state
.cpu_clock_offset
;
551 /* enable cpu_get_ticks() */
552 void cpu_enable_ticks(void)
554 if (!timers_state
.cpu_ticks_enabled
) {
555 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
556 timers_state
.cpu_clock_offset
-= get_clock();
557 timers_state
.cpu_ticks_enabled
= 1;
561 /* disable cpu_get_ticks() : the clock is stopped. You must not call
562 cpu_get_ticks() after that. */
563 void cpu_disable_ticks(void)
565 if (timers_state
.cpu_ticks_enabled
) {
566 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
567 timers_state
.cpu_clock_offset
= cpu_get_clock();
568 timers_state
.cpu_ticks_enabled
= 0;
572 /***********************************************************/
575 #define QEMU_CLOCK_REALTIME 0
576 #define QEMU_CLOCK_VIRTUAL 1
577 #define QEMU_CLOCK_HOST 2
581 /* XXX: add frequency */
589 struct QEMUTimer
*next
;
592 struct qemu_alarm_timer
{
594 int (*start
)(struct qemu_alarm_timer
*t
);
595 void (*stop
)(struct qemu_alarm_timer
*t
);
596 void (*rearm
)(struct qemu_alarm_timer
*t
);
603 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
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
->pending
) {
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
)
1015 struct qemu_alarm_timer
*t
= alarm_timer
;
1020 #define DISP_FREQ 1000
1022 static int64_t delta_min
= INT64_MAX
;
1023 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1025 ti
= qemu_get_clock(vm_clock
);
1026 if (last_clock
!= 0) {
1027 delta
= ti
- last_clock
;
1028 if (delta
< delta_min
)
1030 if (delta
> delta_max
)
1033 if (++count
== DISP_FREQ
) {
1034 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1035 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1036 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1037 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1038 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1040 delta_min
= INT64_MAX
;
1048 if (alarm_has_dynticks(t
) ||
1050 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1051 qemu_get_clock(vm_clock
))) ||
1052 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1053 qemu_get_clock(rt_clock
)) ||
1054 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1055 qemu_get_clock(host_clock
))) {
1057 t
->expired
= alarm_has_dynticks(t
);
1059 #ifndef CONFIG_IOTHREAD
1061 /* stop the currently executing cpu because a timer occured */
1066 qemu_notify_event();
1070 static int64_t qemu_next_deadline(void)
1072 /* To avoid problems with overflow limit this to 2^32. */
1073 int64_t delta
= INT32_MAX
;
1075 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1076 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1077 qemu_get_clock(vm_clock
);
1079 if (active_timers
[QEMU_CLOCK_HOST
]) {
1080 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1081 qemu_get_clock(host_clock
);
1092 #if defined(__linux__)
1093 static uint64_t qemu_next_deadline_dyntick(void)
1101 delta
= (qemu_next_deadline() + 999) / 1000;
1103 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1104 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1105 qemu_get_clock(rt_clock
))*1000;
1106 if (rtdelta
< delta
)
1110 if (delta
< MIN_TIMER_REARM_US
)
1111 delta
= MIN_TIMER_REARM_US
;
1119 /* Sets a specific flag */
1120 static int fcntl_setfl(int fd
, int flag
)
1124 flags
= fcntl(fd
, F_GETFL
);
1128 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1134 #if defined(__linux__)
1136 #define RTC_FREQ 1024
1138 static void enable_sigio_timer(int fd
)
1140 struct sigaction act
;
1143 sigfillset(&act
.sa_mask
);
1145 act
.sa_handler
= host_alarm_handler
;
1147 sigaction(SIGIO
, &act
, NULL
);
1148 fcntl_setfl(fd
, O_ASYNC
);
1149 fcntl(fd
, F_SETOWN
, getpid());
1152 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1154 struct hpet_info info
;
1157 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1162 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1164 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1165 "error, but for better emulation accuracy type:\n"
1166 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1170 /* Check capabilities */
1171 r
= ioctl(fd
, HPET_INFO
, &info
);
1175 /* Enable periodic mode */
1176 r
= ioctl(fd
, HPET_EPI
, 0);
1177 if (info
.hi_flags
&& (r
< 0))
1180 /* Enable interrupt */
1181 r
= ioctl(fd
, HPET_IE_ON
, 0);
1185 enable_sigio_timer(fd
);
1186 t
->priv
= (void *)(long)fd
;
1194 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1196 int fd
= (long)t
->priv
;
1201 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1204 unsigned long current_rtc_freq
= 0;
1206 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1209 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1210 if (current_rtc_freq
!= RTC_FREQ
&&
1211 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1212 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1213 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1214 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1217 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1223 enable_sigio_timer(rtc_fd
);
1225 t
->priv
= (void *)(long)rtc_fd
;
1230 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1232 int rtc_fd
= (long)t
->priv
;
1237 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1241 struct sigaction act
;
1243 sigfillset(&act
.sa_mask
);
1245 act
.sa_handler
= host_alarm_handler
;
1247 sigaction(SIGALRM
, &act
, NULL
);
1250 * Initialize ev struct to 0 to avoid valgrind complaining
1251 * about uninitialized data in timer_create call
1253 memset(&ev
, 0, sizeof(ev
));
1254 ev
.sigev_value
.sival_int
= 0;
1255 ev
.sigev_notify
= SIGEV_SIGNAL
;
1256 ev
.sigev_signo
= SIGALRM
;
1258 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1259 perror("timer_create");
1261 /* disable dynticks */
1262 fprintf(stderr
, "Dynamic Ticks disabled\n");
1267 t
->priv
= (void *)(long)host_timer
;
1272 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1274 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1276 timer_delete(host_timer
);
1279 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1281 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1282 struct itimerspec timeout
;
1283 int64_t nearest_delta_us
= INT64_MAX
;
1286 assert(alarm_has_dynticks(t
));
1287 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1288 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1289 !active_timers
[QEMU_CLOCK_HOST
])
1292 nearest_delta_us
= qemu_next_deadline_dyntick();
1294 /* check whether a timer is already running */
1295 if (timer_gettime(host_timer
, &timeout
)) {
1297 fprintf(stderr
, "Internal timer error: aborting\n");
1300 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1301 if (current_us
&& current_us
<= nearest_delta_us
)
1304 timeout
.it_interval
.tv_sec
= 0;
1305 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1306 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1307 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1308 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1310 fprintf(stderr
, "Internal timer error: aborting\n");
1315 #endif /* defined(__linux__) */
1317 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1319 struct sigaction act
;
1320 struct itimerval itv
;
1324 sigfillset(&act
.sa_mask
);
1326 act
.sa_handler
= host_alarm_handler
;
1328 sigaction(SIGALRM
, &act
, NULL
);
1330 itv
.it_interval
.tv_sec
= 0;
1331 /* for i386 kernel 2.6 to get 1 ms */
1332 itv
.it_interval
.tv_usec
= 999;
1333 itv
.it_value
.tv_sec
= 0;
1334 itv
.it_value
.tv_usec
= 10 * 1000;
1336 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1343 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1345 struct itimerval itv
;
1347 memset(&itv
, 0, sizeof(itv
));
1348 setitimer(ITIMER_REAL
, &itv
, NULL
);
1351 #endif /* !defined(_WIN32) */
1356 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1359 struct qemu_alarm_win32
*data
= t
->priv
;
1362 memset(&tc
, 0, sizeof(tc
));
1363 timeGetDevCaps(&tc
, sizeof(tc
));
1365 data
->period
= tc
.wPeriodMin
;
1366 timeBeginPeriod(data
->period
);
1368 flags
= TIME_CALLBACK_FUNCTION
;
1369 if (alarm_has_dynticks(t
))
1370 flags
|= TIME_ONESHOT
;
1372 flags
|= TIME_PERIODIC
;
1374 data
->timerId
= timeSetEvent(1, // interval (ms)
1375 data
->period
, // resolution
1376 host_alarm_handler
, // function
1377 (DWORD
)t
, // parameter
1380 if (!data
->timerId
) {
1381 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1383 timeEndPeriod(data
->period
);
1390 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1392 struct qemu_alarm_win32
*data
= t
->priv
;
1394 timeKillEvent(data
->timerId
);
1395 timeEndPeriod(data
->period
);
1398 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1400 struct qemu_alarm_win32
*data
= t
->priv
;
1402 assert(alarm_has_dynticks(t
));
1403 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1404 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1405 !active_timers
[QEMU_CLOCK_HOST
])
1408 timeKillEvent(data
->timerId
);
1410 data
->timerId
= timeSetEvent(1,
1414 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1416 if (!data
->timerId
) {
1417 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1420 timeEndPeriod(data
->period
);
1427 static int init_timer_alarm(void)
1429 struct qemu_alarm_timer
*t
= NULL
;
1432 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1433 t
= &alarm_timers
[i
];
1445 /* first event is at time 0 */
1455 static void quit_timers(void)
1457 struct qemu_alarm_timer
*t
= alarm_timer
;
1462 /***********************************************************/
1463 /* host time/date access */
1464 void qemu_get_timedate(struct tm
*tm
, int offset
)
1471 if (rtc_date_offset
== -1) {
1475 ret
= localtime(&ti
);
1477 ti
-= rtc_date_offset
;
1481 memcpy(tm
, ret
, sizeof(struct tm
));
1484 int qemu_timedate_diff(struct tm
*tm
)
1488 if (rtc_date_offset
== -1)
1490 seconds
= mktimegm(tm
);
1492 seconds
= mktime(tm
);
1494 seconds
= mktimegm(tm
) + rtc_date_offset
;
1496 return seconds
- time(NULL
);
1499 void rtc_change_mon_event(struct tm
*tm
)
1503 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
1504 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
1505 qobject_decref(data
);
1508 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1510 time_t rtc_start_date
;
1513 if (!strcmp(startdate
, "now") && legacy
) {
1514 rtc_date_offset
= -1;
1516 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1524 } else if (sscanf(startdate
, "%d-%d-%d",
1527 &tm
.tm_mday
) == 3) {
1536 rtc_start_date
= mktimegm(&tm
);
1537 if (rtc_start_date
== -1) {
1539 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1540 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1543 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1547 static void configure_rtc(QemuOpts
*opts
)
1551 value
= qemu_opt_get(opts
, "base");
1553 if (!strcmp(value
, "utc")) {
1555 } else if (!strcmp(value
, "localtime")) {
1558 configure_rtc_date_offset(value
, 0);
1561 value
= qemu_opt_get(opts
, "clock");
1563 if (!strcmp(value
, "host")) {
1564 rtc_clock
= host_clock
;
1565 } else if (!strcmp(value
, "vm")) {
1566 rtc_clock
= vm_clock
;
1568 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1572 #ifdef CONFIG_TARGET_I386
1573 value
= qemu_opt_get(opts
, "driftfix");
1575 if (!strcmp(buf
, "slew")) {
1577 } else if (!strcmp(buf
, "none")) {
1580 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1588 static void socket_cleanup(void)
1593 static int socket_init(void)
1598 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1600 err
= WSAGetLastError();
1601 fprintf(stderr
, "WSAStartup: %d\n", err
);
1604 atexit(socket_cleanup
);
1609 /***********************************************************/
1610 /* Bluetooth support */
1613 static struct HCIInfo
*hci_table
[MAX_NICS
];
1615 static struct bt_vlan_s
{
1616 struct bt_scatternet_s net
;
1618 struct bt_vlan_s
*next
;
1621 /* find or alloc a new bluetooth "VLAN" */
1622 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1624 struct bt_vlan_s
**pvlan
, *vlan
;
1625 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1629 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1631 pvlan
= &first_bt_vlan
;
1632 while (*pvlan
!= NULL
)
1633 pvlan
= &(*pvlan
)->next
;
1638 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1642 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1647 static struct HCIInfo null_hci
= {
1648 .cmd_send
= null_hci_send
,
1649 .sco_send
= null_hci_send
,
1650 .acl_send
= null_hci_send
,
1651 .bdaddr_set
= null_hci_addr_set
,
1654 struct HCIInfo
*qemu_next_hci(void)
1656 if (cur_hci
== nb_hcis
)
1659 return hci_table
[cur_hci
++];
1662 static struct HCIInfo
*hci_init(const char *str
)
1665 struct bt_scatternet_s
*vlan
= 0;
1667 if (!strcmp(str
, "null"))
1670 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1672 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1673 else if (!strncmp(str
, "hci", 3)) {
1676 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1677 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1682 vlan
= qemu_find_bt_vlan(0);
1684 return bt_new_hci(vlan
);
1687 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1692 static int bt_hci_parse(const char *str
)
1694 struct HCIInfo
*hci
;
1697 if (nb_hcis
>= MAX_NICS
) {
1698 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1702 hci
= hci_init(str
);
1711 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1712 hci
->bdaddr_set(hci
, bdaddr
.b
);
1714 hci_table
[nb_hcis
++] = hci
;
1719 static void bt_vhci_add(int vlan_id
)
1721 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1724 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1725 "an empty scatternet %i\n", vlan_id
);
1727 bt_vhci_init(bt_new_hci(vlan
));
1730 static struct bt_device_s
*bt_device_add(const char *opt
)
1732 struct bt_scatternet_s
*vlan
;
1734 char *endp
= strstr(opt
, ",vlan=");
1735 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1738 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1741 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1743 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1748 vlan
= qemu_find_bt_vlan(vlan_id
);
1751 fprintf(stderr
, "qemu: warning: adding a slave device to "
1752 "an empty scatternet %i\n", vlan_id
);
1754 if (!strcmp(devname
, "keyboard"))
1755 return bt_keyboard_init(vlan
);
1757 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1761 static int bt_parse(const char *opt
)
1763 const char *endp
, *p
;
1766 if (strstart(opt
, "hci", &endp
)) {
1767 if (!*endp
|| *endp
== ',') {
1769 if (!strstart(endp
, ",vlan=", 0))
1772 return bt_hci_parse(opt
);
1774 } else if (strstart(opt
, "vhci", &endp
)) {
1775 if (!*endp
|| *endp
== ',') {
1777 if (strstart(endp
, ",vlan=", &p
)) {
1778 vlan
= strtol(p
, (char **) &endp
, 0);
1780 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1784 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1793 } else if (strstart(opt
, "device:", &endp
))
1794 return !bt_device_add(endp
);
1796 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1800 /***********************************************************/
1801 /* QEMU Block devices */
1803 #define HD_ALIAS "index=%d,media=disk"
1804 #define CDROM_ALIAS "index=2,media=cdrom"
1805 #define FD_ALIAS "index=%d,if=floppy"
1806 #define PFLASH_ALIAS "if=pflash"
1807 #define MTD_ALIAS "if=mtd"
1808 #define SD_ALIAS "index=0,if=sd"
1810 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1817 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1820 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
1822 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1823 __FUNCTION__
, optstr
);
1827 qemu_opt_set(opts
, "file", file
);
1831 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1835 /* seek interface, bus and unit */
1837 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1838 if (dinfo
->type
== type
&&
1839 dinfo
->bus
== bus
&&
1840 dinfo
->unit
== unit
)
1847 DriveInfo
*drive_get_by_id(const char *id
)
1851 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1852 if (strcmp(id
, dinfo
->id
))
1859 int drive_get_max_bus(BlockInterfaceType type
)
1865 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1866 if(dinfo
->type
== type
&&
1867 dinfo
->bus
> max_bus
)
1868 max_bus
= dinfo
->bus
;
1873 const char *drive_get_serial(BlockDriverState
*bdrv
)
1877 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1878 if (dinfo
->bdrv
== bdrv
)
1879 return dinfo
->serial
;
1885 BlockInterfaceErrorAction
drive_get_on_error(
1886 BlockDriverState
*bdrv
, int is_read
)
1890 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1891 if (dinfo
->bdrv
== bdrv
)
1892 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1895 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1898 static void bdrv_format_print(void *opaque
, const char *name
)
1900 fprintf(stderr
, " %s", name
);
1903 void drive_uninit(DriveInfo
*dinfo
)
1905 qemu_opts_del(dinfo
->opts
);
1906 bdrv_delete(dinfo
->bdrv
);
1907 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1911 static int parse_block_error_action(const char *buf
, int is_read
)
1913 if (!strcmp(buf
, "ignore")) {
1914 return BLOCK_ERR_IGNORE
;
1915 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1916 return BLOCK_ERR_STOP_ENOSPC
;
1917 } else if (!strcmp(buf
, "stop")) {
1918 return BLOCK_ERR_STOP_ANY
;
1919 } else if (!strcmp(buf
, "report")) {
1920 return BLOCK_ERR_REPORT
;
1922 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1923 buf
, is_read
? "read" : "write");
1928 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1932 const char *file
= NULL
;
1935 const char *mediastr
= "";
1936 BlockInterfaceType type
;
1937 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1938 int bus_id
, unit_id
;
1939 int cyls
, heads
, secs
, translation
;
1940 BlockDriver
*drv
= NULL
;
1941 QEMUMachine
*machine
= opaque
;
1948 int on_read_error
, on_write_error
;
1949 const char *devaddr
;
1955 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1958 if (machine
&& machine
->use_scsi
) {
1960 max_devs
= MAX_SCSI_DEVS
;
1961 pstrcpy(devname
, sizeof(devname
), "scsi");
1964 max_devs
= MAX_IDE_DEVS
;
1965 pstrcpy(devname
, sizeof(devname
), "ide");
1969 /* extract parameters */
1970 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1971 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1972 index
= qemu_opt_get_number(opts
, "index", -1);
1974 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1975 heads
= qemu_opt_get_number(opts
, "heads", 0);
1976 secs
= qemu_opt_get_number(opts
, "secs", 0);
1978 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1979 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1981 file
= qemu_opt_get(opts
, "file");
1982 serial
= qemu_opt_get(opts
, "serial");
1984 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1985 pstrcpy(devname
, sizeof(devname
), buf
);
1986 if (!strcmp(buf
, "ide")) {
1988 max_devs
= MAX_IDE_DEVS
;
1989 } else if (!strcmp(buf
, "scsi")) {
1991 max_devs
= MAX_SCSI_DEVS
;
1992 } else if (!strcmp(buf
, "floppy")) {
1995 } else if (!strcmp(buf
, "pflash")) {
1998 } else if (!strcmp(buf
, "mtd")) {
2001 } else if (!strcmp(buf
, "sd")) {
2004 } else if (!strcmp(buf
, "virtio")) {
2007 } else if (!strcmp(buf
, "xen")) {
2010 } else if (!strcmp(buf
, "none")) {
2014 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2019 if (cyls
|| heads
|| secs
) {
2020 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2021 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2024 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2025 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2028 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2029 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2034 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2037 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2041 if (!strcmp(buf
, "none"))
2042 translation
= BIOS_ATA_TRANSLATION_NONE
;
2043 else if (!strcmp(buf
, "lba"))
2044 translation
= BIOS_ATA_TRANSLATION_LBA
;
2045 else if (!strcmp(buf
, "auto"))
2046 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2048 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2053 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2054 if (!strcmp(buf
, "disk")) {
2056 } else if (!strcmp(buf
, "cdrom")) {
2057 if (cyls
|| secs
|| heads
) {
2059 "qemu: '%s' invalid physical CHS format\n", buf
);
2062 media
= MEDIA_CDROM
;
2064 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2069 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2070 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2072 else if (!strcmp(buf
, "writethrough"))
2074 else if (!strcmp(buf
, "writeback"))
2077 fprintf(stderr
, "qemu: invalid cache option\n");
2082 #ifdef CONFIG_LINUX_AIO
2083 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2084 if (!strcmp(buf
, "threads"))
2086 else if (!strcmp(buf
, "native"))
2089 fprintf(stderr
, "qemu: invalid aio option\n");
2095 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2096 if (strcmp(buf
, "?") == 0) {
2097 fprintf(stderr
, "qemu: Supported formats:");
2098 bdrv_iterate_format(bdrv_format_print
, NULL
);
2099 fprintf(stderr
, "\n");
2102 drv
= bdrv_find_whitelisted_format(buf
);
2104 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2109 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2110 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2111 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2112 fprintf(stderr
, "werror is no supported by this format\n");
2116 on_write_error
= parse_block_error_action(buf
, 0);
2117 if (on_write_error
< 0) {
2122 on_read_error
= BLOCK_ERR_REPORT
;
2123 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2124 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2125 fprintf(stderr
, "rerror is no supported by this format\n");
2129 on_read_error
= parse_block_error_action(buf
, 1);
2130 if (on_read_error
< 0) {
2135 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2136 if (type
!= IF_VIRTIO
) {
2137 fprintf(stderr
, "addr is not supported\n");
2142 /* compute bus and unit according index */
2145 if (bus_id
!= 0 || unit_id
!= -1) {
2147 "qemu: index cannot be used with bus and unit\n");
2155 unit_id
= index
% max_devs
;
2156 bus_id
= index
/ max_devs
;
2160 /* if user doesn't specify a unit_id,
2161 * try to find the first free
2164 if (unit_id
== -1) {
2166 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2168 if (max_devs
&& unit_id
>= max_devs
) {
2169 unit_id
-= max_devs
;
2177 if (max_devs
&& unit_id
>= max_devs
) {
2178 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2179 unit_id
, max_devs
- 1);
2184 * ignore multiple definitions
2187 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2194 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2195 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2196 dinfo
->id
= qemu_strdup(buf
);
2198 /* no id supplied -> create one */
2199 dinfo
->id
= qemu_mallocz(32);
2200 if (type
== IF_IDE
|| type
== IF_SCSI
)
2201 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2203 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2204 devname
, bus_id
, mediastr
, unit_id
);
2206 snprintf(dinfo
->id
, 32, "%s%s%i",
2207 devname
, mediastr
, unit_id
);
2209 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2210 dinfo
->devaddr
= devaddr
;
2212 dinfo
->bus
= bus_id
;
2213 dinfo
->unit
= unit_id
;
2214 dinfo
->on_read_error
= on_read_error
;
2215 dinfo
->on_write_error
= on_write_error
;
2218 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2219 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2229 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2230 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2234 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2239 /* FIXME: This isn't really a floppy, but it's a reasonable
2242 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2248 /* add virtio block device */
2249 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2250 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2251 qemu_opt_set(opts
, "drive", dinfo
->id
);
2253 qemu_opt_set(opts
, "addr", devaddr
);
2264 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2265 cache
= 2; /* always use write-back with snapshot */
2267 if (cache
== 0) /* no caching */
2268 bdrv_flags
|= BDRV_O_NOCACHE
;
2269 else if (cache
== 2) /* write-back */
2270 bdrv_flags
|= BDRV_O_CACHE_WB
;
2273 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2275 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2279 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2280 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2285 * cdrom is read-only. Set it now, after above interface checking
2286 * since readonly attribute not explicitly required, so no error.
2288 if (media
== MEDIA_CDROM
) {
2291 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2293 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2294 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2295 file
, strerror(errno
));
2299 if (bdrv_key_required(dinfo
->bdrv
))
2305 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2307 QEMUMachine
*machine
= opaque
;
2308 int fatal_error
= 0;
2310 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2317 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2319 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2320 qemu_opt_set(opts
, "snapshot", "on");
2325 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2327 boot_set_handler
= func
;
2328 boot_set_opaque
= opaque
;
2331 int qemu_boot_set(const char *boot_devices
)
2333 if (!boot_set_handler
) {
2336 return boot_set_handler(boot_set_opaque
, boot_devices
);
2339 static int parse_bootdevices(char *devices
)
2341 /* We just do some generic consistency checks */
2345 for (p
= devices
; *p
!= '\0'; p
++) {
2346 /* Allowed boot devices are:
2347 * a-b: floppy disk drives
2348 * c-f: IDE disk drives
2349 * g-m: machine implementation dependant drives
2350 * n-p: network devices
2351 * It's up to each machine implementation to check if the given boot
2352 * devices match the actual hardware implementation and firmware
2355 if (*p
< 'a' || *p
> 'p') {
2356 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2359 if (bitmap
& (1 << (*p
- 'a'))) {
2360 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2363 bitmap
|= 1 << (*p
- 'a');
2368 static void restore_boot_devices(void *opaque
)
2370 char *standard_boot_devices
= opaque
;
2372 qemu_boot_set(standard_boot_devices
);
2374 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2375 qemu_free(standard_boot_devices
);
2378 static void numa_add(const char *optarg
)
2382 unsigned long long value
, endvalue
;
2385 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2386 if (!strcmp(option
, "node")) {
2387 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2388 nodenr
= nb_numa_nodes
;
2390 nodenr
= strtoull(option
, NULL
, 10);
2393 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2394 node_mem
[nodenr
] = 0;
2396 value
= strtoull(option
, &endptr
, 0);
2398 case 0: case 'M': case 'm':
2405 node_mem
[nodenr
] = value
;
2407 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2408 node_cpumask
[nodenr
] = 0;
2410 value
= strtoull(option
, &endptr
, 10);
2413 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2415 if (*endptr
== '-') {
2416 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2417 if (endvalue
>= 63) {
2420 "only 63 CPUs in NUMA mode supported.\n");
2422 value
= (2ULL << endvalue
) - (1ULL << value
);
2424 value
= 1ULL << value
;
2427 node_cpumask
[nodenr
] = value
;
2434 static void smp_parse(const char *optarg
)
2436 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2440 smp
= strtoul(optarg
, &endptr
, 10);
2441 if (endptr
!= optarg
) {
2442 if (*endptr
== ',') {
2446 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2447 sockets
= strtoull(option
, NULL
, 10);
2448 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2449 cores
= strtoull(option
, NULL
, 10);
2450 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2451 threads
= strtoull(option
, NULL
, 10);
2452 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2453 max_cpus
= strtoull(option
, NULL
, 10);
2455 /* compute missing values, prefer sockets over cores over threads */
2456 if (smp
== 0 || sockets
== 0) {
2457 sockets
= sockets
> 0 ? sockets
: 1;
2458 cores
= cores
> 0 ? cores
: 1;
2459 threads
= threads
> 0 ? threads
: 1;
2461 smp
= cores
* threads
* sockets
;
2465 threads
= threads
> 0 ? threads
: 1;
2466 cores
= smp
/ (sockets
* threads
);
2469 threads
= smp
/ (cores
* sockets
);
2474 smp_cores
= cores
> 0 ? cores
: 1;
2475 smp_threads
= threads
> 0 ? threads
: 1;
2477 max_cpus
= smp_cpus
;
2480 /***********************************************************/
2483 static int usb_device_add(const char *devname
, int is_hotplug
)
2486 USBDevice
*dev
= NULL
;
2491 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2492 dev
= usbdevice_create(devname
);
2496 /* the other ones */
2497 if (strstart(devname
, "host:", &p
)) {
2498 dev
= usb_host_device_open(p
);
2499 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2500 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2501 bt_new_hci(qemu_find_bt_vlan(0)));
2512 static int usb_device_del(const char *devname
)
2517 if (strstart(devname
, "host:", &p
))
2518 return usb_host_device_close(p
);
2523 p
= strchr(devname
, '.');
2526 bus_num
= strtoul(devname
, NULL
, 0);
2527 addr
= strtoul(p
+ 1, NULL
, 0);
2529 return usb_device_delete_addr(bus_num
, addr
);
2532 static int usb_parse(const char *cmdline
)
2535 r
= usb_device_add(cmdline
, 0);
2537 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2542 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2544 const char *devname
= qdict_get_str(qdict
, "devname");
2545 if (usb_device_add(devname
, 1) < 0) {
2546 error_report("could not add USB device '%s'", devname
);
2550 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2552 const char *devname
= qdict_get_str(qdict
, "devname");
2553 if (usb_device_del(devname
) < 0) {
2554 error_report("could not delete USB device '%s'", devname
);
2558 /***********************************************************/
2559 /* PCMCIA/Cardbus */
2561 static struct pcmcia_socket_entry_s
{
2562 PCMCIASocket
*socket
;
2563 struct pcmcia_socket_entry_s
*next
;
2564 } *pcmcia_sockets
= 0;
2566 void pcmcia_socket_register(PCMCIASocket
*socket
)
2568 struct pcmcia_socket_entry_s
*entry
;
2570 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2571 entry
->socket
= socket
;
2572 entry
->next
= pcmcia_sockets
;
2573 pcmcia_sockets
= entry
;
2576 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2578 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2580 ptr
= &pcmcia_sockets
;
2581 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2582 if (entry
->socket
== socket
) {
2588 void pcmcia_info(Monitor
*mon
)
2590 struct pcmcia_socket_entry_s
*iter
;
2592 if (!pcmcia_sockets
)
2593 monitor_printf(mon
, "No PCMCIA sockets\n");
2595 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2596 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2597 iter
->socket
->attached
? iter
->socket
->card_string
:
2601 /***********************************************************/
2604 typedef struct IOHandlerRecord
{
2606 IOCanRWHandler
*fd_read_poll
;
2608 IOHandler
*fd_write
;
2611 /* temporary data */
2613 struct IOHandlerRecord
*next
;
2616 static IOHandlerRecord
*first_io_handler
;
2618 /* XXX: fd_read_poll should be suppressed, but an API change is
2619 necessary in the character devices to suppress fd_can_read(). */
2620 int qemu_set_fd_handler2(int fd
,
2621 IOCanRWHandler
*fd_read_poll
,
2623 IOHandler
*fd_write
,
2626 IOHandlerRecord
**pioh
, *ioh
;
2628 if (!fd_read
&& !fd_write
) {
2629 pioh
= &first_io_handler
;
2634 if (ioh
->fd
== fd
) {
2641 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2645 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2646 ioh
->next
= first_io_handler
;
2647 first_io_handler
= ioh
;
2650 ioh
->fd_read_poll
= fd_read_poll
;
2651 ioh
->fd_read
= fd_read
;
2652 ioh
->fd_write
= fd_write
;
2653 ioh
->opaque
= opaque
;
2659 int qemu_set_fd_handler(int fd
,
2661 IOHandler
*fd_write
,
2664 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2668 /***********************************************************/
2669 /* Polling handling */
2671 typedef struct PollingEntry
{
2674 struct PollingEntry
*next
;
2677 static PollingEntry
*first_polling_entry
;
2679 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2681 PollingEntry
**ppe
, *pe
;
2682 pe
= qemu_mallocz(sizeof(PollingEntry
));
2684 pe
->opaque
= opaque
;
2685 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2690 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2692 PollingEntry
**ppe
, *pe
;
2693 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2695 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2703 /***********************************************************/
2704 /* Wait objects support */
2705 typedef struct WaitObjects
{
2707 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2708 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2709 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2712 static WaitObjects wait_objects
= {0};
2714 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2716 WaitObjects
*w
= &wait_objects
;
2718 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2720 w
->events
[w
->num
] = handle
;
2721 w
->func
[w
->num
] = func
;
2722 w
->opaque
[w
->num
] = opaque
;
2727 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2730 WaitObjects
*w
= &wait_objects
;
2733 for (i
= 0; i
< w
->num
; i
++) {
2734 if (w
->events
[i
] == handle
)
2737 w
->events
[i
] = w
->events
[i
+ 1];
2738 w
->func
[i
] = w
->func
[i
+ 1];
2739 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2747 /***********************************************************/
2748 /* ram save/restore */
2750 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2751 #define RAM_SAVE_FLAG_COMPRESS 0x02
2752 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2753 #define RAM_SAVE_FLAG_PAGE 0x08
2754 #define RAM_SAVE_FLAG_EOS 0x10
2756 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2758 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2759 uint32_t *array
= (uint32_t *)page
;
2762 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2763 if (array
[i
] != val
)
2770 static int ram_save_block(QEMUFile
*f
)
2772 static ram_addr_t current_addr
= 0;
2773 ram_addr_t saved_addr
= current_addr
;
2774 ram_addr_t addr
= 0;
2777 while (addr
< last_ram_offset
) {
2778 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2781 cpu_physical_memory_reset_dirty(current_addr
,
2782 current_addr
+ TARGET_PAGE_SIZE
,
2783 MIGRATION_DIRTY_FLAG
);
2785 p
= qemu_get_ram_ptr(current_addr
);
2787 if (is_dup_page(p
, *p
)) {
2788 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2789 qemu_put_byte(f
, *p
);
2791 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2792 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2798 addr
+= TARGET_PAGE_SIZE
;
2799 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2805 static uint64_t bytes_transferred
;
2807 static ram_addr_t
ram_save_remaining(void)
2810 ram_addr_t count
= 0;
2812 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2813 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2820 uint64_t ram_bytes_remaining(void)
2822 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2825 uint64_t ram_bytes_transferred(void)
2827 return bytes_transferred
;
2830 uint64_t ram_bytes_total(void)
2832 return last_ram_offset
;
2835 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2838 uint64_t bytes_transferred_last
;
2840 uint64_t expected_time
= 0;
2843 cpu_physical_memory_set_dirty_tracking(0);
2847 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2848 qemu_file_set_error(f
);
2853 bytes_transferred
= 0;
2855 /* Make sure all dirty bits are set */
2856 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2857 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2858 cpu_physical_memory_set_dirty(addr
);
2861 /* Enable dirty memory tracking */
2862 cpu_physical_memory_set_dirty_tracking(1);
2864 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2867 bytes_transferred_last
= bytes_transferred
;
2868 bwidth
= qemu_get_clock_ns(rt_clock
);
2870 while (!qemu_file_rate_limit(f
)) {
2873 ret
= ram_save_block(f
);
2874 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2875 if (ret
== 0) /* no more blocks */
2879 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
2880 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2882 /* if we haven't transferred anything this round, force expected_time to a
2883 * a very high value, but without crashing */
2887 /* try transferring iterative blocks of memory */
2889 /* flush all remaining blocks regardless of rate limiting */
2890 while (ram_save_block(f
) != 0) {
2891 bytes_transferred
+= TARGET_PAGE_SIZE
;
2893 cpu_physical_memory_set_dirty_tracking(0);
2896 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2898 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2900 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2903 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2908 if (version_id
!= 3)
2912 addr
= qemu_get_be64(f
);
2914 flags
= addr
& ~TARGET_PAGE_MASK
;
2915 addr
&= TARGET_PAGE_MASK
;
2917 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2918 if (addr
!= last_ram_offset
)
2922 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2923 uint8_t ch
= qemu_get_byte(f
);
2924 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2927 (!kvm_enabled() || kvm_has_sync_mmu())) {
2928 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2931 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2932 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2934 if (qemu_file_has_error(f
)) {
2937 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2942 void qemu_service_io(void)
2944 qemu_notify_event();
2947 /***********************************************************/
2948 /* machine registration */
2950 static QEMUMachine
*first_machine
= NULL
;
2951 QEMUMachine
*current_machine
= NULL
;
2953 int qemu_register_machine(QEMUMachine
*m
)
2956 pm
= &first_machine
;
2964 static QEMUMachine
*find_machine(const char *name
)
2968 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2969 if (!strcmp(m
->name
, name
))
2971 if (m
->alias
&& !strcmp(m
->alias
, name
))
2977 static QEMUMachine
*find_default_machine(void)
2981 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2982 if (m
->is_default
) {
2989 /***********************************************************/
2990 /* main execution loop */
2992 static void gui_update(void *opaque
)
2994 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2995 DisplayState
*ds
= opaque
;
2996 DisplayChangeListener
*dcl
= ds
->listeners
;
2998 qemu_flush_coalesced_mmio_buffer();
3001 while (dcl
!= NULL
) {
3002 if (dcl
->gui_timer_interval
&&
3003 dcl
->gui_timer_interval
< interval
)
3004 interval
= dcl
->gui_timer_interval
;
3007 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3010 static void nographic_update(void *opaque
)
3012 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3014 qemu_flush_coalesced_mmio_buffer();
3015 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3018 void cpu_synchronize_all_states(void)
3022 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3023 cpu_synchronize_state(cpu
);
3027 void cpu_synchronize_all_post_reset(void)
3031 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3032 cpu_synchronize_post_reset(cpu
);
3036 void cpu_synchronize_all_post_init(void)
3040 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
3041 cpu_synchronize_post_init(cpu
);
3045 struct vm_change_state_entry
{
3046 VMChangeStateHandler
*cb
;
3048 QLIST_ENTRY (vm_change_state_entry
) entries
;
3051 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3053 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3056 VMChangeStateEntry
*e
;
3058 e
= qemu_mallocz(sizeof (*e
));
3062 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3066 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3068 QLIST_REMOVE (e
, entries
);
3072 static void vm_state_notify(int running
, int reason
)
3074 VMChangeStateEntry
*e
;
3076 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3077 e
->cb(e
->opaque
, running
, reason
);
3081 static void resume_all_vcpus(void);
3082 static void pause_all_vcpus(void);
3089 vm_state_notify(1, 0);
3090 qemu_rearm_alarm_timer(alarm_timer
);
3095 /* reset/shutdown handler */
3097 typedef struct QEMUResetEntry
{
3098 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3099 QEMUResetHandler
*func
;
3103 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3104 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3105 static int reset_requested
;
3106 static int shutdown_requested
;
3107 static int powerdown_requested
;
3108 static int debug_requested
;
3109 static int vmstop_requested
;
3111 int qemu_shutdown_requested(void)
3113 int r
= shutdown_requested
;
3114 shutdown_requested
= 0;
3118 int qemu_reset_requested(void)
3120 int r
= reset_requested
;
3121 reset_requested
= 0;
3125 int qemu_powerdown_requested(void)
3127 int r
= powerdown_requested
;
3128 powerdown_requested
= 0;
3132 static int qemu_debug_requested(void)
3134 int r
= debug_requested
;
3135 debug_requested
= 0;
3139 static int qemu_vmstop_requested(void)
3141 int r
= vmstop_requested
;
3142 vmstop_requested
= 0;
3146 static void do_vm_stop(int reason
)
3149 cpu_disable_ticks();
3152 vm_state_notify(0, reason
);
3153 monitor_protocol_event(QEVENT_STOP
, NULL
);
3157 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3159 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3162 re
->opaque
= opaque
;
3163 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3166 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3170 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3171 if (re
->func
== func
&& re
->opaque
== opaque
) {
3172 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3179 void qemu_system_reset(void)
3181 QEMUResetEntry
*re
, *nre
;
3183 /* reset all devices */
3184 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3185 re
->func(re
->opaque
);
3187 monitor_protocol_event(QEVENT_RESET
, NULL
);
3188 cpu_synchronize_all_post_reset();
3191 void qemu_system_reset_request(void)
3194 shutdown_requested
= 1;
3196 reset_requested
= 1;
3198 qemu_notify_event();
3201 void qemu_system_shutdown_request(void)
3203 shutdown_requested
= 1;
3204 qemu_notify_event();
3207 void qemu_system_powerdown_request(void)
3209 powerdown_requested
= 1;
3210 qemu_notify_event();
3213 #ifdef CONFIG_IOTHREAD
3214 static void qemu_system_vmstop_request(int reason
)
3216 vmstop_requested
= reason
;
3217 qemu_notify_event();
3222 static int io_thread_fd
= -1;
3224 static void qemu_event_increment(void)
3226 /* Write 8 bytes to be compatible with eventfd. */
3227 static uint64_t val
= 1;
3230 if (io_thread_fd
== -1)
3234 ret
= write(io_thread_fd
, &val
, sizeof(val
));
3235 } while (ret
< 0 && errno
== EINTR
);
3237 /* EAGAIN is fine, a read must be pending. */
3238 if (ret
< 0 && errno
!= EAGAIN
) {
3239 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3245 static void qemu_event_read(void *opaque
)
3247 int fd
= (unsigned long)opaque
;
3251 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
3253 len
= read(fd
, buffer
, sizeof(buffer
));
3254 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
3257 static int qemu_event_init(void)
3262 err
= qemu_eventfd(fds
);
3266 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3270 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3274 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3275 (void *)(unsigned long)fds
[0]);
3277 io_thread_fd
= fds
[1];
3286 HANDLE qemu_event_handle
;
3288 static void dummy_event_handler(void *opaque
)
3292 static int qemu_event_init(void)
3294 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3295 if (!qemu_event_handle
) {
3296 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3299 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3303 static void qemu_event_increment(void)
3305 if (!SetEvent(qemu_event_handle
)) {
3306 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3313 static int cpu_can_run(CPUState
*env
)
3324 #ifndef CONFIG_IOTHREAD
3325 static int qemu_init_main_loop(void)
3327 return qemu_event_init();
3330 void qemu_init_vcpu(void *_env
)
3332 CPUState
*env
= _env
;
3334 env
->nr_cores
= smp_cores
;
3335 env
->nr_threads
= smp_threads
;
3341 int qemu_cpu_self(void *env
)
3346 static void resume_all_vcpus(void)
3350 static void pause_all_vcpus(void)
3354 void qemu_cpu_kick(void *env
)
3359 void qemu_notify_event(void)
3361 CPUState
*env
= cpu_single_env
;
3363 qemu_event_increment ();
3369 void qemu_mutex_lock_iothread(void) {}
3370 void qemu_mutex_unlock_iothread(void) {}
3372 void vm_stop(int reason
)
3377 #else /* CONFIG_IOTHREAD */
3379 #include "qemu-thread.h"
3381 QemuMutex qemu_global_mutex
;
3382 static QemuMutex qemu_fair_mutex
;
3384 static QemuThread io_thread
;
3386 static QemuThread
*tcg_cpu_thread
;
3387 static QemuCond
*tcg_halt_cond
;
3389 static int qemu_system_ready
;
3391 static QemuCond qemu_cpu_cond
;
3393 static QemuCond qemu_system_cond
;
3394 static QemuCond qemu_pause_cond
;
3396 static void tcg_block_io_signals(void);
3397 static void kvm_block_io_signals(CPUState
*env
);
3398 static void unblock_io_signals(void);
3399 static int tcg_has_work(void);
3400 static int cpu_has_work(CPUState
*env
);
3402 static int qemu_init_main_loop(void)
3406 ret
= qemu_event_init();
3410 qemu_cond_init(&qemu_pause_cond
);
3411 qemu_mutex_init(&qemu_fair_mutex
);
3412 qemu_mutex_init(&qemu_global_mutex
);
3413 qemu_mutex_lock(&qemu_global_mutex
);
3415 unblock_io_signals();
3416 qemu_thread_self(&io_thread
);
3421 static void qemu_wait_io_event_common(CPUState
*env
)
3426 qemu_cond_signal(&qemu_pause_cond
);
3430 static void qemu_wait_io_event(CPUState
*env
)
3432 while (!tcg_has_work())
3433 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3435 qemu_mutex_unlock(&qemu_global_mutex
);
3438 * Users of qemu_global_mutex can be starved, having no chance
3439 * to acquire it since this path will get to it first.
3440 * So use another lock to provide fairness.
3442 qemu_mutex_lock(&qemu_fair_mutex
);
3443 qemu_mutex_unlock(&qemu_fair_mutex
);
3445 qemu_mutex_lock(&qemu_global_mutex
);
3446 qemu_wait_io_event_common(env
);
3449 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
3456 ts
.tv_sec
= timeout
/ 1000;
3457 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
3459 sigemptyset(&waitset
);
3460 sigaddset(&waitset
, SIG_IPI
);
3462 qemu_mutex_unlock(&qemu_global_mutex
);
3463 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
3465 qemu_mutex_lock(&qemu_global_mutex
);
3467 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
3468 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
3473 static void qemu_kvm_wait_io_event(CPUState
*env
)
3475 while (!cpu_has_work(env
))
3476 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3478 qemu_kvm_eat_signal(env
, 0);
3479 qemu_wait_io_event_common(env
);
3482 static int qemu_cpu_exec(CPUState
*env
);
3484 static void *kvm_cpu_thread_fn(void *arg
)
3486 CPUState
*env
= arg
;
3488 qemu_thread_self(env
->thread
);
3492 kvm_block_io_signals(env
);
3494 /* signal CPU creation */
3495 qemu_mutex_lock(&qemu_global_mutex
);
3497 qemu_cond_signal(&qemu_cpu_cond
);
3499 /* and wait for machine initialization */
3500 while (!qemu_system_ready
)
3501 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3504 if (cpu_can_run(env
))
3506 qemu_kvm_wait_io_event(env
);
3512 static void tcg_cpu_exec(void);
3514 static void *tcg_cpu_thread_fn(void *arg
)
3516 CPUState
*env
= arg
;
3518 tcg_block_io_signals();
3519 qemu_thread_self(env
->thread
);
3521 /* signal CPU creation */
3522 qemu_mutex_lock(&qemu_global_mutex
);
3523 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3525 qemu_cond_signal(&qemu_cpu_cond
);
3527 /* and wait for machine initialization */
3528 while (!qemu_system_ready
)
3529 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3533 qemu_wait_io_event(cur_cpu
);
3539 void qemu_cpu_kick(void *_env
)
3541 CPUState
*env
= _env
;
3542 qemu_cond_broadcast(env
->halt_cond
);
3544 qemu_thread_signal(env
->thread
, SIG_IPI
);
3547 int qemu_cpu_self(void *_env
)
3549 CPUState
*env
= _env
;
3552 qemu_thread_self(&this);
3554 return qemu_thread_equal(&this, env
->thread
);
3557 static void cpu_signal(int sig
)
3560 cpu_exit(cpu_single_env
);
3563 static void tcg_block_io_signals(void)
3566 struct sigaction sigact
;
3569 sigaddset(&set
, SIGUSR2
);
3570 sigaddset(&set
, SIGIO
);
3571 sigaddset(&set
, SIGALRM
);
3572 sigaddset(&set
, SIGCHLD
);
3573 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3576 sigaddset(&set
, SIG_IPI
);
3577 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3579 memset(&sigact
, 0, sizeof(sigact
));
3580 sigact
.sa_handler
= cpu_signal
;
3581 sigaction(SIG_IPI
, &sigact
, NULL
);
3584 static void dummy_signal(int sig
)
3588 static void kvm_block_io_signals(CPUState
*env
)
3592 struct sigaction sigact
;
3595 sigaddset(&set
, SIGUSR2
);
3596 sigaddset(&set
, SIGIO
);
3597 sigaddset(&set
, SIGALRM
);
3598 sigaddset(&set
, SIGCHLD
);
3599 sigaddset(&set
, SIG_IPI
);
3600 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3602 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
3603 sigdelset(&set
, SIG_IPI
);
3605 memset(&sigact
, 0, sizeof(sigact
));
3606 sigact
.sa_handler
= dummy_signal
;
3607 sigaction(SIG_IPI
, &sigact
, NULL
);
3609 r
= kvm_set_signal_mask(env
, &set
);
3611 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
3616 static void unblock_io_signals(void)
3621 sigaddset(&set
, SIGUSR2
);
3622 sigaddset(&set
, SIGIO
);
3623 sigaddset(&set
, SIGALRM
);
3624 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3627 sigaddset(&set
, SIG_IPI
);
3628 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3631 static void qemu_signal_lock(unsigned int msecs
)
3633 qemu_mutex_lock(&qemu_fair_mutex
);
3635 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3636 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
3637 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3640 qemu_mutex_unlock(&qemu_fair_mutex
);
3643 void qemu_mutex_lock_iothread(void)
3645 if (kvm_enabled()) {
3646 qemu_mutex_lock(&qemu_fair_mutex
);
3647 qemu_mutex_lock(&qemu_global_mutex
);
3648 qemu_mutex_unlock(&qemu_fair_mutex
);
3650 qemu_signal_lock(100);
3653 void qemu_mutex_unlock_iothread(void)
3655 qemu_mutex_unlock(&qemu_global_mutex
);
3658 static int all_vcpus_paused(void)
3660 CPUState
*penv
= first_cpu
;
3665 penv
= (CPUState
*)penv
->next_cpu
;
3671 static void pause_all_vcpus(void)
3673 CPUState
*penv
= first_cpu
;
3677 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3678 qemu_cpu_kick(penv
);
3679 penv
= (CPUState
*)penv
->next_cpu
;
3682 while (!all_vcpus_paused()) {
3683 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3686 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3687 penv
= (CPUState
*)penv
->next_cpu
;
3692 static void resume_all_vcpus(void)
3694 CPUState
*penv
= first_cpu
;
3699 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3700 qemu_cpu_kick(penv
);
3701 penv
= (CPUState
*)penv
->next_cpu
;
3705 static void tcg_init_vcpu(void *_env
)
3707 CPUState
*env
= _env
;
3708 /* share a single thread for all cpus with TCG */
3709 if (!tcg_cpu_thread
) {
3710 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3711 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3712 qemu_cond_init(env
->halt_cond
);
3713 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3714 while (env
->created
== 0)
3715 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3716 tcg_cpu_thread
= env
->thread
;
3717 tcg_halt_cond
= env
->halt_cond
;
3719 env
->thread
= tcg_cpu_thread
;
3720 env
->halt_cond
= tcg_halt_cond
;
3724 static void kvm_start_vcpu(CPUState
*env
)
3726 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3727 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3728 qemu_cond_init(env
->halt_cond
);
3729 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3730 while (env
->created
== 0)
3731 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3734 void qemu_init_vcpu(void *_env
)
3736 CPUState
*env
= _env
;
3738 env
->nr_cores
= smp_cores
;
3739 env
->nr_threads
= smp_threads
;
3741 kvm_start_vcpu(env
);
3746 void qemu_notify_event(void)
3748 qemu_event_increment();
3751 void vm_stop(int reason
)
3754 qemu_thread_self(&me
);
3756 if (!qemu_thread_equal(&me
, &io_thread
)) {
3757 qemu_system_vmstop_request(reason
);
3759 * FIXME: should not return to device code in case
3760 * vm_stop() has been requested.
3762 if (cpu_single_env
) {
3763 cpu_exit(cpu_single_env
);
3764 cpu_single_env
->stop
= 1;
3775 static void host_main_loop_wait(int *timeout
)
3781 /* XXX: need to suppress polling by better using win32 events */
3783 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3784 ret
|= pe
->func(pe
->opaque
);
3788 WaitObjects
*w
= &wait_objects
;
3790 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3791 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3792 if (w
->func
[ret
- WAIT_OBJECT_0
])
3793 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3795 /* Check for additional signaled events */
3796 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3798 /* Check if event is signaled */
3799 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3800 if(ret2
== WAIT_OBJECT_0
) {
3802 w
->func
[i
](w
->opaque
[i
]);
3803 } else if (ret2
== WAIT_TIMEOUT
) {
3805 err
= GetLastError();
3806 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3809 } else if (ret
== WAIT_TIMEOUT
) {
3811 err
= GetLastError();
3812 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3819 static void host_main_loop_wait(int *timeout
)
3824 void main_loop_wait(int timeout
)
3826 IOHandlerRecord
*ioh
;
3827 fd_set rfds
, wfds
, xfds
;
3831 qemu_bh_update_timeout(&timeout
);
3833 host_main_loop_wait(&timeout
);
3835 /* poll any events */
3836 /* XXX: separate device handlers from system ones */
3841 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3845 (!ioh
->fd_read_poll
||
3846 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3847 FD_SET(ioh
->fd
, &rfds
);
3851 if (ioh
->fd_write
) {
3852 FD_SET(ioh
->fd
, &wfds
);
3858 tv
.tv_sec
= timeout
/ 1000;
3859 tv
.tv_usec
= (timeout
% 1000) * 1000;
3861 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3863 qemu_mutex_unlock_iothread();
3864 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3865 qemu_mutex_lock_iothread();
3867 IOHandlerRecord
**pioh
;
3869 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3870 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3871 ioh
->fd_read(ioh
->opaque
);
3873 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3874 ioh
->fd_write(ioh
->opaque
);
3878 /* remove deleted IO handlers */
3879 pioh
= &first_io_handler
;
3890 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3892 /* rearm timer, if not periodic */
3893 if (alarm_timer
->expired
) {
3894 alarm_timer
->expired
= 0;
3895 qemu_rearm_alarm_timer(alarm_timer
);
3898 alarm_timer
->pending
= 0;
3900 /* vm time timers */
3902 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3903 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3904 qemu_get_clock(vm_clock
));
3907 /* real time timers */
3908 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3909 qemu_get_clock(rt_clock
));
3911 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3912 qemu_get_clock(host_clock
));
3914 /* Check bottom-halves last in case any of the earlier events triggered
3920 static int qemu_cpu_exec(CPUState
*env
)
3923 #ifdef CONFIG_PROFILER
3927 #ifdef CONFIG_PROFILER
3928 ti
= profile_getclock();
3933 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3934 env
->icount_decr
.u16
.low
= 0;
3935 env
->icount_extra
= 0;
3936 count
= qemu_next_deadline();
3937 count
= (count
+ (1 << icount_time_shift
) - 1)
3938 >> icount_time_shift
;
3939 qemu_icount
+= count
;
3940 decr
= (count
> 0xffff) ? 0xffff : count
;
3942 env
->icount_decr
.u16
.low
= decr
;
3943 env
->icount_extra
= count
;
3945 ret
= cpu_exec(env
);
3946 #ifdef CONFIG_PROFILER
3947 qemu_time
+= profile_getclock() - ti
;
3950 /* Fold pending instructions back into the
3951 instruction counter, and clear the interrupt flag. */
3952 qemu_icount
-= (env
->icount_decr
.u16
.low
3953 + env
->icount_extra
);
3954 env
->icount_decr
.u32
= 0;
3955 env
->icount_extra
= 0;
3960 static void tcg_cpu_exec(void)
3964 if (next_cpu
== NULL
)
3965 next_cpu
= first_cpu
;
3966 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3967 CPUState
*env
= cur_cpu
= next_cpu
;
3969 if (alarm_timer
->pending
)
3971 if (cpu_can_run(env
))
3972 ret
= qemu_cpu_exec(env
);
3976 if (ret
== EXCP_DEBUG
) {
3977 gdb_set_stop_cpu(env
);
3978 debug_requested
= 1;
3984 static int cpu_has_work(CPUState
*env
)
3992 if (qemu_cpu_has_work(env
))
3997 static int tcg_has_work(void)
4001 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4002 if (cpu_has_work(env
))
4007 static int qemu_calculate_timeout(void)
4009 #ifndef CONFIG_IOTHREAD
4014 else if (tcg_has_work())
4016 else if (!use_icount
)
4019 /* XXX: use timeout computed from timers */
4022 /* Advance virtual time to the next event. */
4023 if (use_icount
== 1) {
4024 /* When not using an adaptive execution frequency
4025 we tend to get badly out of sync with real time,
4026 so just delay for a reasonable amount of time. */
4029 delta
= cpu_get_icount() - cpu_get_clock();
4032 /* If virtual time is ahead of real time then just
4034 timeout
= (delta
/ 1000000) + 1;
4036 /* Wait for either IO to occur or the next
4038 add
= qemu_next_deadline();
4039 /* We advance the timer before checking for IO.
4040 Limit the amount we advance so that early IO
4041 activity won't get the guest too far ahead. */
4045 add
= (add
+ (1 << icount_time_shift
) - 1)
4046 >> icount_time_shift
;
4048 timeout
= delta
/ 1000000;
4055 #else /* CONFIG_IOTHREAD */
4060 static int vm_can_run(void)
4062 if (powerdown_requested
)
4064 if (reset_requested
)
4066 if (shutdown_requested
)
4068 if (debug_requested
)
4073 qemu_irq qemu_system_powerdown
;
4075 static void main_loop(void)
4079 #ifdef CONFIG_IOTHREAD
4080 qemu_system_ready
= 1;
4081 qemu_cond_broadcast(&qemu_system_cond
);
4086 #ifdef CONFIG_PROFILER
4089 #ifndef CONFIG_IOTHREAD
4092 #ifdef CONFIG_PROFILER
4093 ti
= profile_getclock();
4095 main_loop_wait(qemu_calculate_timeout());
4096 #ifdef CONFIG_PROFILER
4097 dev_time
+= profile_getclock() - ti
;
4099 } while (vm_can_run());
4101 if (qemu_debug_requested()) {
4102 vm_stop(EXCP_DEBUG
);
4104 if (qemu_shutdown_requested()) {
4105 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4112 if (qemu_reset_requested()) {
4114 qemu_system_reset();
4117 if (qemu_powerdown_requested()) {
4118 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4119 qemu_irq_raise(qemu_system_powerdown
);
4121 if ((r
= qemu_vmstop_requested())) {
4128 static void version(void)
4130 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4133 static void help(int exitcode
)
4135 const char *options_help
=
4136 #define DEF(option, opt_arg, opt_enum, opt_help) \
4138 #define DEFHEADING(text) stringify(text) "\n"
4139 #include "qemu-options.h"
4145 printf("usage: %s [options] [disk_image]\n"
4147 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4150 "During emulation, the following keys are useful:\n"
4151 "ctrl-alt-f toggle full screen\n"
4152 "ctrl-alt-n switch to virtual console 'n'\n"
4153 "ctrl-alt toggle mouse and keyboard grab\n"
4155 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4161 #define HAS_ARG 0x0001
4164 #define DEF(option, opt_arg, opt_enum, opt_help) \
4166 #define DEFHEADING(text)
4167 #include "qemu-options.h"
4173 typedef struct QEMUOption
{
4179 static const QEMUOption qemu_options
[] = {
4180 { "h", 0, QEMU_OPTION_h
},
4181 #define DEF(option, opt_arg, opt_enum, opt_help) \
4182 { option, opt_arg, opt_enum },
4183 #define DEFHEADING(text)
4184 #include "qemu-options.h"
4192 struct soundhw soundhw
[] = {
4193 #ifdef HAS_AUDIO_CHOICE
4194 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4200 { .init_isa
= pcspk_audio_init
}
4207 "Creative Sound Blaster 16",
4210 { .init_isa
= SB16_init
}
4214 #ifdef CONFIG_CS4231A
4220 { .init_isa
= cs4231a_init
}
4228 "Yamaha YMF262 (OPL3)",
4230 "Yamaha YM3812 (OPL2)",
4234 { .init_isa
= Adlib_init
}
4241 "Gravis Ultrasound GF1",
4244 { .init_isa
= GUS_init
}
4251 "Intel 82801AA AC97 Audio",
4254 { .init_pci
= ac97_init
}
4258 #ifdef CONFIG_ES1370
4261 "ENSONIQ AudioPCI ES1370",
4264 { .init_pci
= es1370_init
}
4268 #endif /* HAS_AUDIO_CHOICE */
4270 { NULL
, NULL
, 0, 0, { NULL
} }
4273 static void select_soundhw (const char *optarg
)
4277 if (*optarg
== '?') {
4280 printf ("Valid sound card names (comma separated):\n");
4281 for (c
= soundhw
; c
->name
; ++c
) {
4282 printf ("%-11s %s\n", c
->name
, c
->descr
);
4284 printf ("\n-soundhw all will enable all of the above\n");
4285 exit (*optarg
!= '?');
4293 if (!strcmp (optarg
, "all")) {
4294 for (c
= soundhw
; c
->name
; ++c
) {
4302 e
= strchr (p
, ',');
4303 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4305 for (c
= soundhw
; c
->name
; ++c
) {
4306 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4315 "Unknown sound card name (too big to show)\n");
4318 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4323 p
+= l
+ (e
!= NULL
);
4327 goto show_valid_cards
;
4332 static void select_vgahw (const char *p
)
4337 vga_interface_type
= VGA_NONE
;
4338 if (strstart(p
, "std", &opts
)) {
4339 vga_interface_type
= VGA_STD
;
4340 } else if (strstart(p
, "cirrus", &opts
)) {
4341 vga_interface_type
= VGA_CIRRUS
;
4342 } else if (strstart(p
, "vmware", &opts
)) {
4343 vga_interface_type
= VGA_VMWARE
;
4344 } else if (strstart(p
, "xenfb", &opts
)) {
4345 vga_interface_type
= VGA_XENFB
;
4346 } else if (!strstart(p
, "none", &opts
)) {
4348 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4352 const char *nextopt
;
4354 if (strstart(opts
, ",retrace=", &nextopt
)) {
4356 if (strstart(opts
, "dumb", &nextopt
))
4357 vga_retrace_method
= VGA_RETRACE_DUMB
;
4358 else if (strstart(opts
, "precise", &nextopt
))
4359 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4360 else goto invalid_vga
;
4361 } else goto invalid_vga
;
4367 static int balloon_parse(const char *arg
)
4371 if (strcmp(arg
, "none") == 0) {
4375 if (!strncmp(arg
, "virtio", 6)) {
4376 if (arg
[6] == ',') {
4377 /* have params -> parse them */
4378 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
4382 /* create empty opts */
4383 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4385 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4394 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4396 exit(STATUS_CONTROL_C_EXIT
);
4401 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4405 if(strlen(str
) != 36)
4408 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4409 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4410 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4416 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4424 static void termsig_handler(int signal
)
4426 qemu_system_shutdown_request();
4429 static void sigchld_handler(int signal
)
4431 waitpid(-1, NULL
, WNOHANG
);
4434 static void sighandler_setup(void)
4436 struct sigaction act
;
4438 memset(&act
, 0, sizeof(act
));
4439 act
.sa_handler
= termsig_handler
;
4440 sigaction(SIGINT
, &act
, NULL
);
4441 sigaction(SIGHUP
, &act
, NULL
);
4442 sigaction(SIGTERM
, &act
, NULL
);
4444 act
.sa_handler
= sigchld_handler
;
4445 act
.sa_flags
= SA_NOCLDSTOP
;
4446 sigaction(SIGCHLD
, &act
, NULL
);
4452 /* Look for support files in the same directory as the executable. */
4453 static char *find_datadir(const char *argv0
)
4459 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4466 while (p
!= buf
&& *p
!= '\\')
4469 if (access(buf
, R_OK
) == 0) {
4470 return qemu_strdup(buf
);
4476 /* Find a likely location for support files using the location of the binary.
4477 For installed binaries this will be "$bindir/../share/qemu". When
4478 running from the build tree this will be "$bindir/../pc-bios". */
4479 #define SHARE_SUFFIX "/share/qemu"
4480 #define BUILD_SUFFIX "/pc-bios"
4481 static char *find_datadir(const char *argv0
)
4489 #if defined(__linux__)
4492 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4498 #elif defined(__FreeBSD__)
4501 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4508 /* If we don't have any way of figuring out the actual executable
4509 location then try argv[0]. */
4511 p
= realpath(argv0
, buf
);
4519 max_len
= strlen(dir
) +
4520 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4521 res
= qemu_mallocz(max_len
);
4522 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4523 if (access(res
, R_OK
)) {
4524 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4525 if (access(res
, R_OK
)) {
4537 char *qemu_find_file(int type
, const char *name
)
4543 /* If name contains path separators then try it as a straight path. */
4544 if ((strchr(name
, '/') || strchr(name
, '\\'))
4545 && access(name
, R_OK
) == 0) {
4546 return qemu_strdup(name
);
4549 case QEMU_FILE_TYPE_BIOS
:
4552 case QEMU_FILE_TYPE_KEYMAP
:
4553 subdir
= "keymaps/";
4558 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4559 buf
= qemu_mallocz(len
);
4560 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4561 if (access(buf
, R_OK
)) {
4568 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4570 return qdev_device_help(opts
);
4573 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4577 dev
= qdev_device_add(opts
);
4583 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4585 CharDriverState
*chr
;
4587 chr
= qemu_chr_open_opts(opts
, NULL
);
4593 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4595 CharDriverState
*chr
;
4596 const char *chardev
;
4600 mode
= qemu_opt_get(opts
, "mode");
4604 if (strcmp(mode
, "readline") == 0) {
4605 flags
= MONITOR_USE_READLINE
;
4606 } else if (strcmp(mode
, "control") == 0) {
4607 flags
= MONITOR_USE_CONTROL
;
4609 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4613 if (qemu_opt_get_bool(opts
, "default", 0))
4614 flags
|= MONITOR_IS_DEFAULT
;
4616 chardev
= qemu_opt_get(opts
, "chardev");
4617 chr
= qemu_chr_find(chardev
);
4619 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4623 monitor_init(chr
, flags
);
4627 static void monitor_parse(const char *optarg
, const char *mode
)
4629 static int monitor_device_index
= 0;
4635 if (strstart(optarg
, "chardev:", &p
)) {
4636 snprintf(label
, sizeof(label
), "%s", p
);
4638 if (monitor_device_index
) {
4639 snprintf(label
, sizeof(label
), "monitor%d",
4640 monitor_device_index
);
4642 snprintf(label
, sizeof(label
), "monitor");
4645 opts
= qemu_chr_parse_compat(label
, optarg
);
4647 fprintf(stderr
, "parse error: %s\n", optarg
);
4652 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4654 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4657 qemu_opt_set(opts
, "mode", mode
);
4658 qemu_opt_set(opts
, "chardev", label
);
4660 qemu_opt_set(opts
, "default", "on");
4661 monitor_device_index
++;
4664 struct device_config
{
4666 DEV_USB
, /* -usbdevice */
4668 DEV_SERIAL
, /* -serial */
4669 DEV_PARALLEL
, /* -parallel */
4670 DEV_VIRTCON
, /* -virtioconsole */
4671 DEV_DEBUGCON
, /* -debugcon */
4673 const char *cmdline
;
4674 QTAILQ_ENTRY(device_config
) next
;
4676 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4678 static void add_device_config(int type
, const char *cmdline
)
4680 struct device_config
*conf
;
4682 conf
= qemu_mallocz(sizeof(*conf
));
4684 conf
->cmdline
= cmdline
;
4685 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4688 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4690 struct device_config
*conf
;
4693 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4694 if (conf
->type
!= type
)
4696 rc
= func(conf
->cmdline
);
4703 static int serial_parse(const char *devname
)
4705 static int index
= 0;
4708 if (strcmp(devname
, "none") == 0)
4710 if (index
== MAX_SERIAL_PORTS
) {
4711 fprintf(stderr
, "qemu: too many serial ports\n");
4714 snprintf(label
, sizeof(label
), "serial%d", index
);
4715 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4716 if (!serial_hds
[index
]) {
4717 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4718 devname
, strerror(errno
));
4725 static int parallel_parse(const char *devname
)
4727 static int index
= 0;
4730 if (strcmp(devname
, "none") == 0)
4732 if (index
== MAX_PARALLEL_PORTS
) {
4733 fprintf(stderr
, "qemu: too many parallel ports\n");
4736 snprintf(label
, sizeof(label
), "parallel%d", index
);
4737 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4738 if (!parallel_hds
[index
]) {
4739 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4740 devname
, strerror(errno
));
4747 static int virtcon_parse(const char *devname
)
4749 static int index
= 0;
4751 QemuOpts
*bus_opts
, *dev_opts
;
4753 if (strcmp(devname
, "none") == 0)
4755 if (index
== MAX_VIRTIO_CONSOLES
) {
4756 fprintf(stderr
, "qemu: too many virtio consoles\n");
4760 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4761 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4763 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4764 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4766 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4767 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4768 if (!virtcon_hds
[index
]) {
4769 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4770 devname
, strerror(errno
));
4773 qemu_opt_set(dev_opts
, "chardev", label
);
4779 static int debugcon_parse(const char *devname
)
4783 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4786 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4788 fprintf(stderr
, "qemu: already have a debugcon device\n");
4791 qemu_opt_set(opts
, "driver", "isa-debugcon");
4792 qemu_opt_set(opts
, "chardev", "debugcon");
4796 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4797 const char **poptarg
, int *poptind
)
4799 const QEMUOption
*popt
;
4800 int optind
= *poptind
;
4801 char *r
= argv
[optind
];
4804 loc_set_cmdline(argv
, optind
, 1);
4806 /* Treat --foo the same as -foo. */
4809 popt
= qemu_options
;
4812 error_report("invalid option");
4815 if (!strcmp(popt
->name
, r
+ 1))
4819 if (popt
->flags
& HAS_ARG
) {
4820 if (optind
>= argc
) {
4821 error_report("requires an argument");
4824 optarg
= argv
[optind
++];
4825 loc_set_cmdline(argv
, optind
- 2, 2);
4836 int main(int argc
, char **argv
, char **envp
)
4838 const char *gdbstub_dev
= NULL
;
4839 uint32_t boot_devices_bitmap
= 0;
4841 int snapshot
, linux_boot
, net_boot
;
4842 const char *initrd_filename
;
4843 const char *kernel_filename
, *kernel_cmdline
;
4844 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4846 DisplayChangeListener
*dcl
;
4847 int cyls
, heads
, secs
, translation
;
4848 QemuOpts
*hda_opts
= NULL
, *opts
;
4851 const char *loadvm
= NULL
;
4852 QEMUMachine
*machine
;
4853 const char *cpu_model
;
4858 const char *pid_file
= NULL
;
4859 const char *incoming
= NULL
;
4862 struct passwd
*pwd
= NULL
;
4863 const char *chroot_dir
= NULL
;
4864 const char *run_as
= NULL
;
4867 int show_vnc_port
= 0;
4870 error_set_progname(argv
[0]);
4874 qemu_cache_utils_init(envp
);
4876 QLIST_INIT (&vm_change_state_head
);
4879 struct sigaction act
;
4880 sigfillset(&act
.sa_mask
);
4882 act
.sa_handler
= SIG_IGN
;
4883 sigaction(SIGPIPE
, &act
, NULL
);
4886 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4887 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4888 QEMU to run on a single CPU */
4893 h
= GetCurrentProcess();
4894 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4895 for(i
= 0; i
< 32; i
++) {
4896 if (mask
& (1 << i
))
4901 SetProcessAffinityMask(h
, mask
);
4907 module_call_init(MODULE_INIT_MACHINE
);
4908 machine
= find_default_machine();
4910 initrd_filename
= NULL
;
4913 kernel_filename
= NULL
;
4914 kernel_cmdline
= "";
4915 cyls
= heads
= secs
= 0;
4916 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4918 for (i
= 0; i
< MAX_NODES
; i
++) {
4920 node_cpumask
[i
] = 0;
4929 /* first pass of option parsing */
4931 while (optind
< argc
) {
4932 if (argv
[optind
][0] != '-') {
4937 const QEMUOption
*popt
;
4939 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4940 switch (popt
->index
) {
4941 case QEMU_OPTION_nodefconfig
:
4952 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
4953 fp
= fopen(fname
, "r");
4955 if (qemu_config_parse(fp
, fname
) != 0) {
4961 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
4962 fp
= fopen(fname
, "r");
4964 if (qemu_config_parse(fp
, fname
) != 0) {
4970 #if defined(cpudef_setup)
4971 cpudef_setup(); /* parse cpu definitions in target config file */
4974 /* second pass of option parsing */
4979 if (argv
[optind
][0] != '-') {
4980 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4982 const QEMUOption
*popt
;
4984 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4985 switch(popt
->index
) {
4987 machine
= find_machine(optarg
);
4990 printf("Supported machines are:\n");
4991 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4993 printf("%-10s %s (alias of %s)\n",
4994 m
->alias
, m
->desc
, m
->name
);
4995 printf("%-10s %s%s\n",
4997 m
->is_default
? " (default)" : "");
4999 exit(*optarg
!= '?');
5002 case QEMU_OPTION_cpu
:
5003 /* hw initialization will check this */
5004 if (*optarg
== '?') {
5005 /* XXX: implement xxx_cpu_list for targets that still miss it */
5006 #if defined(cpu_list_id)
5007 cpu_list_id(stdout
, &fprintf
, optarg
);
5008 #elif defined(cpu_list)
5009 cpu_list(stdout
, &fprintf
); /* deprecated */
5016 case QEMU_OPTION_initrd
:
5017 initrd_filename
= optarg
;
5019 case QEMU_OPTION_hda
:
5021 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5023 hda_opts
= drive_add(optarg
, HD_ALIAS
5024 ",cyls=%d,heads=%d,secs=%d%s",
5025 0, cyls
, heads
, secs
,
5026 translation
== BIOS_ATA_TRANSLATION_LBA
?
5028 translation
== BIOS_ATA_TRANSLATION_NONE
?
5029 ",trans=none" : "");
5031 case QEMU_OPTION_hdb
:
5032 case QEMU_OPTION_hdc
:
5033 case QEMU_OPTION_hdd
:
5034 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5036 case QEMU_OPTION_drive
:
5037 drive_add(NULL
, "%s", optarg
);
5039 case QEMU_OPTION_set
:
5040 if (qemu_set_option(optarg
) != 0)
5043 case QEMU_OPTION_global
:
5044 if (qemu_global_option(optarg
) != 0)
5047 case QEMU_OPTION_mtdblock
:
5048 drive_add(optarg
, MTD_ALIAS
);
5050 case QEMU_OPTION_sd
:
5051 drive_add(optarg
, SD_ALIAS
);
5053 case QEMU_OPTION_pflash
:
5054 drive_add(optarg
, PFLASH_ALIAS
);
5056 case QEMU_OPTION_snapshot
:
5059 case QEMU_OPTION_hdachs
:
5063 cyls
= strtol(p
, (char **)&p
, 0);
5064 if (cyls
< 1 || cyls
> 16383)
5069 heads
= strtol(p
, (char **)&p
, 0);
5070 if (heads
< 1 || heads
> 16)
5075 secs
= strtol(p
, (char **)&p
, 0);
5076 if (secs
< 1 || secs
> 63)
5080 if (!strcmp(p
, "none"))
5081 translation
= BIOS_ATA_TRANSLATION_NONE
;
5082 else if (!strcmp(p
, "lba"))
5083 translation
= BIOS_ATA_TRANSLATION_LBA
;
5084 else if (!strcmp(p
, "auto"))
5085 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5088 } else if (*p
!= '\0') {
5090 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5093 if (hda_opts
!= NULL
) {
5095 snprintf(num
, sizeof(num
), "%d", cyls
);
5096 qemu_opt_set(hda_opts
, "cyls", num
);
5097 snprintf(num
, sizeof(num
), "%d", heads
);
5098 qemu_opt_set(hda_opts
, "heads", num
);
5099 snprintf(num
, sizeof(num
), "%d", secs
);
5100 qemu_opt_set(hda_opts
, "secs", num
);
5101 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5102 qemu_opt_set(hda_opts
, "trans", "lba");
5103 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5104 qemu_opt_set(hda_opts
, "trans", "none");
5108 case QEMU_OPTION_numa
:
5109 if (nb_numa_nodes
>= MAX_NODES
) {
5110 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5115 case QEMU_OPTION_nographic
:
5116 display_type
= DT_NOGRAPHIC
;
5118 #ifdef CONFIG_CURSES
5119 case QEMU_OPTION_curses
:
5120 display_type
= DT_CURSES
;
5123 case QEMU_OPTION_portrait
:
5126 case QEMU_OPTION_kernel
:
5127 kernel_filename
= optarg
;
5129 case QEMU_OPTION_append
:
5130 kernel_cmdline
= optarg
;
5132 case QEMU_OPTION_cdrom
:
5133 drive_add(optarg
, CDROM_ALIAS
);
5135 case QEMU_OPTION_boot
:
5137 static const char * const params
[] = {
5138 "order", "once", "menu", NULL
5140 char buf
[sizeof(boot_devices
)];
5141 char *standard_boot_devices
;
5144 if (!strchr(optarg
, '=')) {
5146 pstrcpy(buf
, sizeof(buf
), optarg
);
5147 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5149 "qemu: unknown boot parameter '%s' in '%s'\n",
5155 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5156 boot_devices_bitmap
= parse_bootdevices(buf
);
5157 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5160 if (get_param_value(buf
, sizeof(buf
),
5162 boot_devices_bitmap
|= parse_bootdevices(buf
);
5163 standard_boot_devices
= qemu_strdup(boot_devices
);
5164 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5165 qemu_register_reset(restore_boot_devices
,
5166 standard_boot_devices
);
5168 if (get_param_value(buf
, sizeof(buf
),
5170 if (!strcmp(buf
, "on")) {
5172 } else if (!strcmp(buf
, "off")) {
5176 "qemu: invalid option value '%s'\n",
5184 case QEMU_OPTION_fda
:
5185 case QEMU_OPTION_fdb
:
5186 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5189 case QEMU_OPTION_no_fd_bootchk
:
5193 case QEMU_OPTION_netdev
:
5194 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5198 case QEMU_OPTION_net
:
5199 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5204 case QEMU_OPTION_tftp
:
5205 legacy_tftp_prefix
= optarg
;
5207 case QEMU_OPTION_bootp
:
5208 legacy_bootp_filename
= optarg
;
5211 case QEMU_OPTION_smb
:
5212 if (net_slirp_smb(optarg
) < 0)
5216 case QEMU_OPTION_redir
:
5217 if (net_slirp_redir(optarg
) < 0)
5221 case QEMU_OPTION_bt
:
5222 add_device_config(DEV_BT
, optarg
);
5225 case QEMU_OPTION_audio_help
:
5229 case QEMU_OPTION_soundhw
:
5230 select_soundhw (optarg
);
5236 case QEMU_OPTION_version
:
5240 case QEMU_OPTION_m
: {
5244 value
= strtoul(optarg
, &ptr
, 10);
5246 case 0: case 'M': case 'm':
5253 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5257 /* On 32-bit hosts, QEMU is limited by virtual address space */
5258 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5259 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5262 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5263 fprintf(stderr
, "qemu: ram size too large\n");
5269 case QEMU_OPTION_mempath
:
5273 case QEMU_OPTION_mem_prealloc
:
5280 const CPULogItem
*item
;
5282 mask
= cpu_str_to_log_mask(optarg
);
5284 printf("Log items (comma separated):\n");
5285 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5286 printf("%-10s %s\n", item
->name
, item
->help
);
5294 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5296 case QEMU_OPTION_gdb
:
5297 gdbstub_dev
= optarg
;
5302 case QEMU_OPTION_bios
:
5305 case QEMU_OPTION_singlestep
:
5312 keyboard_layout
= optarg
;
5314 case QEMU_OPTION_localtime
:
5317 case QEMU_OPTION_vga
:
5318 select_vgahw (optarg
);
5320 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5326 w
= strtol(p
, (char **)&p
, 10);
5329 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5335 h
= strtol(p
, (char **)&p
, 10);
5340 depth
= strtol(p
, (char **)&p
, 10);
5341 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5342 depth
!= 24 && depth
!= 32)
5344 } else if (*p
== '\0') {
5345 depth
= graphic_depth
;
5352 graphic_depth
= depth
;
5356 case QEMU_OPTION_echr
:
5359 term_escape_char
= strtol(optarg
, &r
, 0);
5361 printf("Bad argument to echr\n");
5364 case QEMU_OPTION_monitor
:
5365 monitor_parse(optarg
, "readline");
5366 default_monitor
= 0;
5368 case QEMU_OPTION_qmp
:
5369 monitor_parse(optarg
, "control");
5370 default_monitor
= 0;
5372 case QEMU_OPTION_mon
:
5373 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
5375 fprintf(stderr
, "parse error: %s\n", optarg
);
5378 default_monitor
= 0;
5380 case QEMU_OPTION_chardev
:
5381 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
5383 fprintf(stderr
, "parse error: %s\n", optarg
);
5387 case QEMU_OPTION_serial
:
5388 add_device_config(DEV_SERIAL
, optarg
);
5390 if (strncmp(optarg
, "mon:", 4) == 0) {
5391 default_monitor
= 0;
5394 case QEMU_OPTION_watchdog
:
5397 "qemu: only one watchdog option may be given\n");
5402 case QEMU_OPTION_watchdog_action
:
5403 if (select_watchdog_action(optarg
) == -1) {
5404 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5408 case QEMU_OPTION_virtiocon
:
5409 add_device_config(DEV_VIRTCON
, optarg
);
5410 default_virtcon
= 0;
5411 if (strncmp(optarg
, "mon:", 4) == 0) {
5412 default_monitor
= 0;
5415 case QEMU_OPTION_parallel
:
5416 add_device_config(DEV_PARALLEL
, optarg
);
5417 default_parallel
= 0;
5418 if (strncmp(optarg
, "mon:", 4) == 0) {
5419 default_monitor
= 0;
5422 case QEMU_OPTION_debugcon
:
5423 add_device_config(DEV_DEBUGCON
, optarg
);
5425 case QEMU_OPTION_loadvm
:
5428 case QEMU_OPTION_full_screen
:
5432 case QEMU_OPTION_no_frame
:
5435 case QEMU_OPTION_alt_grab
:
5438 case QEMU_OPTION_ctrl_grab
:
5441 case QEMU_OPTION_no_quit
:
5444 case QEMU_OPTION_sdl
:
5445 display_type
= DT_SDL
;
5448 case QEMU_OPTION_pidfile
:
5452 case QEMU_OPTION_win2k_hack
:
5453 win2k_install_hack
= 1;
5455 case QEMU_OPTION_rtc_td_hack
:
5458 case QEMU_OPTION_acpitable
:
5459 if(acpi_table_add(optarg
) < 0) {
5460 fprintf(stderr
, "Wrong acpi table provided\n");
5464 case QEMU_OPTION_smbios
:
5465 if(smbios_entry_add(optarg
) < 0) {
5466 fprintf(stderr
, "Wrong smbios provided\n");
5472 case QEMU_OPTION_enable_kvm
:
5476 case QEMU_OPTION_usb
:
5479 case QEMU_OPTION_usbdevice
:
5481 add_device_config(DEV_USB
, optarg
);
5483 case QEMU_OPTION_device
:
5484 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
5488 case QEMU_OPTION_smp
:
5491 fprintf(stderr
, "Invalid number of CPUs\n");
5494 if (max_cpus
< smp_cpus
) {
5495 fprintf(stderr
, "maxcpus must be equal to or greater than "
5499 if (max_cpus
> 255) {
5500 fprintf(stderr
, "Unsupported number of maxcpus\n");
5504 case QEMU_OPTION_vnc
:
5505 display_type
= DT_VNC
;
5506 vnc_display
= optarg
;
5509 case QEMU_OPTION_no_acpi
:
5512 case QEMU_OPTION_no_hpet
:
5515 case QEMU_OPTION_balloon
:
5516 if (balloon_parse(optarg
) < 0) {
5517 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5522 case QEMU_OPTION_no_reboot
:
5525 case QEMU_OPTION_no_shutdown
:
5528 case QEMU_OPTION_show_cursor
:
5531 case QEMU_OPTION_uuid
:
5532 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5533 fprintf(stderr
, "Fail to parse UUID string."
5534 " Wrong format.\n");
5539 case QEMU_OPTION_daemonize
:
5543 case QEMU_OPTION_option_rom
:
5544 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5545 fprintf(stderr
, "Too many option ROMs\n");
5548 option_rom
[nb_option_roms
] = optarg
;
5551 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5552 case QEMU_OPTION_semihosting
:
5553 semihosting_enabled
= 1;
5556 case QEMU_OPTION_name
:
5557 qemu_name
= qemu_strdup(optarg
);
5559 char *p
= strchr(qemu_name
, ',');
5562 if (strncmp(p
, "process=", 8)) {
5563 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5571 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5572 case QEMU_OPTION_prom_env
:
5573 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5574 fprintf(stderr
, "Too many prom variables\n");
5577 prom_envs
[nb_prom_envs
] = optarg
;
5582 case QEMU_OPTION_old_param
:
5586 case QEMU_OPTION_clock
:
5587 configure_alarms(optarg
);
5589 case QEMU_OPTION_startdate
:
5590 configure_rtc_date_offset(optarg
, 1);
5592 case QEMU_OPTION_rtc
:
5593 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
5595 fprintf(stderr
, "parse error: %s\n", optarg
);
5598 configure_rtc(opts
);
5600 case QEMU_OPTION_tb_size
:
5601 tb_size
= strtol(optarg
, NULL
, 0);
5605 case QEMU_OPTION_icount
:
5607 if (strcmp(optarg
, "auto") == 0) {
5608 icount_time_shift
= -1;
5610 icount_time_shift
= strtol(optarg
, NULL
, 0);
5613 case QEMU_OPTION_incoming
:
5616 case QEMU_OPTION_nodefaults
:
5618 default_parallel
= 0;
5619 default_virtcon
= 0;
5620 default_monitor
= 0;
5628 case QEMU_OPTION_chroot
:
5629 chroot_dir
= optarg
;
5631 case QEMU_OPTION_runas
:
5636 case QEMU_OPTION_xen_domid
:
5637 xen_domid
= atoi(optarg
);
5639 case QEMU_OPTION_xen_create
:
5640 xen_mode
= XEN_CREATE
;
5642 case QEMU_OPTION_xen_attach
:
5643 xen_mode
= XEN_ATTACH
;
5646 case QEMU_OPTION_readconfig
:
5649 fp
= fopen(optarg
, "r");
5651 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5654 if (qemu_config_parse(fp
, optarg
) != 0) {
5660 case QEMU_OPTION_writeconfig
:
5663 if (strcmp(optarg
, "-") == 0) {
5666 fp
= fopen(optarg
, "w");
5668 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5672 qemu_config_write(fp
);
5681 /* If no data_dir is specified then try to find it relative to the
5684 data_dir
= find_datadir(argv
[0]);
5686 /* If all else fails use the install patch specified when building. */
5688 data_dir
= CONFIG_QEMU_SHAREDIR
;
5692 * Default to max_cpus = smp_cpus, in case the user doesn't
5693 * specify a max_cpus value.
5696 max_cpus
= smp_cpus
;
5698 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5699 if (smp_cpus
> machine
->max_cpus
) {
5700 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5701 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5706 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5707 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5709 if (machine
->no_serial
) {
5712 if (machine
->no_parallel
) {
5713 default_parallel
= 0;
5715 if (!machine
->use_virtcon
) {
5716 default_virtcon
= 0;
5718 if (machine
->no_vga
) {
5721 if (machine
->no_floppy
) {
5724 if (machine
->no_cdrom
) {
5727 if (machine
->no_sdcard
) {
5731 if (display_type
== DT_NOGRAPHIC
) {
5732 if (default_parallel
)
5733 add_device_config(DEV_PARALLEL
, "null");
5734 if (default_serial
&& default_monitor
) {
5735 add_device_config(DEV_SERIAL
, "mon:stdio");
5736 } else if (default_virtcon
&& default_monitor
) {
5737 add_device_config(DEV_VIRTCON
, "mon:stdio");
5740 add_device_config(DEV_SERIAL
, "stdio");
5741 if (default_virtcon
)
5742 add_device_config(DEV_VIRTCON
, "stdio");
5743 if (default_monitor
)
5744 monitor_parse("stdio", "readline");
5748 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5749 if (default_parallel
)
5750 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5751 if (default_monitor
)
5752 monitor_parse("vc:80Cx24C", "readline");
5753 if (default_virtcon
)
5754 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5757 vga_interface_type
= VGA_CIRRUS
;
5759 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5766 if (pipe(fds
) == -1)
5777 len
= read(fds
[0], &status
, 1);
5778 if (len
== -1 && (errno
== EINTR
))
5783 else if (status
== 1) {
5784 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5792 qemu_set_cloexec(fds
[1]);
5804 signal(SIGTSTP
, SIG_IGN
);
5805 signal(SIGTTOU
, SIG_IGN
);
5806 signal(SIGTTIN
, SIG_IGN
);
5810 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5814 if (write(fds
[1], &status
, 1) != 1) {
5815 perror("daemonize. Writing to pipe\n");
5819 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5823 if (kvm_enabled()) {
5826 ret
= kvm_init(smp_cpus
);
5828 fprintf(stderr
, "failed to initialize KVM\n");
5833 if (qemu_init_main_loop()) {
5834 fprintf(stderr
, "qemu_init_main_loop failed\n");
5837 linux_boot
= (kernel_filename
!= NULL
);
5839 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5840 fprintf(stderr
, "-append only allowed with -kernel option\n");
5844 if (!linux_boot
&& initrd_filename
!= NULL
) {
5845 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5850 /* Win32 doesn't support line-buffering and requires size >= 2 */
5851 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5854 if (init_timer_alarm() < 0) {
5855 fprintf(stderr
, "could not initialize alarm timer\n");
5858 if (use_icount
&& icount_time_shift
< 0) {
5860 /* 125MIPS seems a reasonable initial guess at the guest speed.
5861 It will be corrected fairly quickly anyway. */
5862 icount_time_shift
= 3;
5863 init_icount_adjust();
5870 if (net_init_clients() < 0) {
5874 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5875 net_set_boot_mask(net_boot
);
5877 /* init the bluetooth world */
5878 if (foreach_device_config(DEV_BT
, bt_parse
))
5881 /* init the memory */
5883 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5885 /* init the dynamic translator */
5886 cpu_exec_init_all(tb_size
* 1024 * 1024);
5888 bdrv_init_with_whitelist();
5892 if (default_cdrom
) {
5893 /* we always create the cdrom drive, even if no disk is there */
5894 drive_add(NULL
, CDROM_ALIAS
);
5897 if (default_floppy
) {
5898 /* we always create at least one floppy */
5899 drive_add(NULL
, FD_ALIAS
, 0);
5902 if (default_sdcard
) {
5903 /* we always create one sd slot, even if no card is in it */
5904 drive_add(NULL
, SD_ALIAS
);
5907 /* open the virtual block devices */
5909 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5910 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5913 vmstate_register(0, &vmstate_timers
,&timers_state
);
5914 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5917 if (nb_numa_nodes
> 0) {
5920 if (nb_numa_nodes
> smp_cpus
) {
5921 nb_numa_nodes
= smp_cpus
;
5924 /* If no memory size if given for any node, assume the default case
5925 * and distribute the available memory equally across all nodes
5927 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5928 if (node_mem
[i
] != 0)
5931 if (i
== nb_numa_nodes
) {
5932 uint64_t usedmem
= 0;
5934 /* On Linux, the each node's border has to be 8MB aligned,
5935 * the final node gets the rest.
5937 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5938 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5939 usedmem
+= node_mem
[i
];
5941 node_mem
[i
] = ram_size
- usedmem
;
5944 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5945 if (node_cpumask
[i
] != 0)
5948 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5949 * must cope with this anyway, because there are BIOSes out there in
5950 * real machines which also use this scheme.
5952 if (i
== nb_numa_nodes
) {
5953 for (i
= 0; i
< smp_cpus
; i
++) {
5954 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5959 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5961 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5963 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5965 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5968 module_call_init(MODULE_INIT_DEVICE
);
5970 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5974 i
= select_watchdog(watchdog
);
5976 exit (i
== 1 ? 1 : 0);
5979 if (machine
->compat_props
) {
5980 qdev_prop_register_global_list(machine
->compat_props
);
5984 machine
->init(ram_size
, boot_devices
,
5985 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5987 cpu_synchronize_all_post_init();
5990 /* must be after terminal init, SDL library changes signal handlers */
5994 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5995 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5996 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
6002 current_machine
= machine
;
6004 /* init USB devices */
6006 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
6010 /* init generic devices */
6011 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
6014 net_check_clients();
6016 /* just use the first displaystate for the moment */
6017 ds
= get_displaystate();
6019 if (display_type
== DT_DEFAULT
) {
6020 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6021 display_type
= DT_SDL
;
6023 display_type
= DT_VNC
;
6024 vnc_display
= "localhost:0,to=99";
6030 switch (display_type
) {
6033 #if defined(CONFIG_CURSES)
6035 curses_display_init(ds
, full_screen
);
6038 #if defined(CONFIG_SDL)
6040 sdl_display_init(ds
, full_screen
, no_frame
);
6042 #elif defined(CONFIG_COCOA)
6044 cocoa_display_init(ds
, full_screen
);
6048 vnc_display_init(ds
);
6049 if (vnc_display_open(ds
, vnc_display
) < 0)
6052 if (show_vnc_port
) {
6053 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6061 dcl
= ds
->listeners
;
6062 while (dcl
!= NULL
) {
6063 if (dcl
->dpy_refresh
!= NULL
) {
6064 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6065 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6070 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6071 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6072 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6075 text_consoles_set_display(ds
);
6077 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
6080 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6081 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6086 qdev_machine_creation_done();
6088 if (rom_load_all() != 0) {
6089 fprintf(stderr
, "rom loading failed\n");
6093 qemu_system_reset();
6095 if (load_vmstate(loadvm
) < 0) {
6101 qemu_start_incoming_migration(incoming
);
6102 } else if (autostart
) {
6112 len
= write(fds
[1], &status
, 1);
6113 if (len
== -1 && (errno
== EINTR
))
6120 perror("not able to chdir to /");
6123 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
6129 pwd
= getpwnam(run_as
);
6131 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6137 if (chroot(chroot_dir
) < 0) {
6138 fprintf(stderr
, "chroot failed\n");
6142 perror("not able to chdir to /");
6148 if (setgid(pwd
->pw_gid
) < 0) {
6149 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6152 if (setuid(pwd
->pw_uid
) < 0) {
6153 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6156 if (setuid(0) != -1) {
6157 fprintf(stderr
, "Dropping privileges failed\n");