80
78
int many_ioeventfds;
83
static KVMState *kvm_state;
83
static const KVMCapabilityInfo kvm_required_capabilites[] = {
84
KVM_CAP_INFO(USER_MEMORY),
85
KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
85
89
static KVMSlot *kvm_alloc_slot(KVMState *s)
89
93
for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
90
/* KVM private memory slots */
93
if (s->slots[i].memory_size == 0)
94
if (s->slots[i].memory_size == 0) {
94
95
return &s->slots[i];
97
99
fprintf(stderr, "%s: no free slot available\n", __func__);
228
#ifdef KVM_CAP_COALESCED_MMIO
229
if (s->coalesced_mmio && !s->coalesced_mmio_ring)
230
s->coalesced_mmio_ring = (void *) env->kvm_run +
231
s->coalesced_mmio * PAGE_SIZE;
230
if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
231
s->coalesced_mmio_ring =
232
(void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
234
235
ret = kvm_arch_init_vcpu(env);
273
274
return kvm_set_user_memory_region(s, mem);
276
int kvm_log_start(target_phys_addr_t phys_addr, ram_addr_t size)
277
static int kvm_log_start(CPUPhysMemoryClient *client,
278
target_phys_addr_t phys_addr, ram_addr_t size)
278
return kvm_dirty_pages_log_change(phys_addr, size,
279
KVM_MEM_LOG_DIRTY_PAGES,
280
KVM_MEM_LOG_DIRTY_PAGES);
280
return kvm_dirty_pages_log_change(phys_addr, size, KVM_MEM_LOG_DIRTY_PAGES,
281
KVM_MEM_LOG_DIRTY_PAGES);
283
int kvm_log_stop(target_phys_addr_t phys_addr, ram_addr_t size)
284
static int kvm_log_stop(CPUPhysMemoryClient *client,
285
target_phys_addr_t phys_addr, ram_addr_t size)
285
return kvm_dirty_pages_log_change(phys_addr, size,
287
KVM_MEM_LOG_DIRTY_PAGES);
287
return kvm_dirty_pages_log_change(phys_addr, size, 0,
288
KVM_MEM_LOG_DIRTY_PAGES);
290
291
static int kvm_set_migration_log(int enable)
356
357
* @end_addr: end of logged region.
358
359
static int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
359
target_phys_addr_t end_addr)
360
target_phys_addr_t end_addr)
361
362
KVMState *s = kvm_state;
362
363
unsigned long size, allocated_size = 0;
450
447
static int kvm_check_many_ioeventfds(void)
452
/* Older kernels have a 6 device limit on the KVM io bus. Find out so we
449
/* Userspace can use ioeventfd for io notification. This requires a host
450
* that supports eventfd(2) and an I/O thread; since eventfd does not
451
* support SIGIO it cannot interrupt the vcpu.
453
* Older kernels have a 6 device limit on the KVM io bus. Find out so we
453
454
* can avoid creating too many ioeventfds.
455
#ifdef CONFIG_EVENTFD
456
#if defined(CONFIG_EVENTFD) && defined(CONFIG_IOTHREAD)
456
457
int ioeventfds[7];
458
459
for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
483
static void kvm_set_phys_mem(target_phys_addr_t start_addr,
485
ram_addr_t phys_offset)
484
static const KVMCapabilityInfo *
485
kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
488
if (!kvm_check_extension(s, list->value)) {
496
static void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
497
ram_addr_t phys_offset)
487
499
KVMState *s = kvm_state;
488
500
ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
591
603
/* in case the KVM bug workaround already "consumed" the new slot */
595
607
/* KVM does not need to know about this memory */
596
if (flags >= IO_MEM_UNASSIGNED)
608
if (flags >= IO_MEM_UNASSIGNED) {
599
611
mem = kvm_alloc_slot(s);
600
612
mem->memory_size = size;
601
613
mem->start_addr = start_addr;
613
625
static void kvm_client_set_memory(struct CPUPhysMemoryClient *client,
614
target_phys_addr_t start_addr,
616
ram_addr_t phys_offset)
626
target_phys_addr_t start_addr,
627
ram_addr_t size, ram_addr_t phys_offset)
618
kvm_set_phys_mem(start_addr, size, phys_offset);
629
kvm_set_phys_mem(start_addr, size, phys_offset);
621
632
static int kvm_client_sync_dirty_bitmap(struct CPUPhysMemoryClient *client,
622
target_phys_addr_t start_addr,
623
target_phys_addr_t end_addr)
633
target_phys_addr_t start_addr,
634
target_phys_addr_t end_addr)
625
return kvm_physical_sync_dirty_bitmap(start_addr, end_addr);
636
return kvm_physical_sync_dirty_bitmap(start_addr, end_addr);
628
639
static int kvm_client_migration_log(struct CPUPhysMemoryClient *client,
631
return kvm_set_migration_log(enable);
642
return kvm_set_migration_log(enable);
634
645
static CPUPhysMemoryClient kvm_cpu_phys_memory_client = {
635
.set_memory = kvm_client_set_memory,
636
.sync_dirty_bitmap = kvm_client_sync_dirty_bitmap,
637
.migration_log = kvm_client_migration_log,
646
.set_memory = kvm_client_set_memory,
647
.sync_dirty_bitmap = kvm_client_sync_dirty_bitmap,
648
.migration_log = kvm_client_migration_log,
649
.log_start = kvm_log_start,
650
.log_stop = kvm_log_stop,
640
int kvm_init(int smp_cpus)
642
655
static const char upgrade_note[] =
643
656
"Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
644
657
"(see http://sourceforge.net/projects/kvm).\n";
659
const KVMCapabilityInfo *missing_cap;
651
665
#ifdef KVM_CAP_SET_GUEST_DEBUG
652
666
QTAILQ_INIT(&s->kvm_sw_breakpoints);
654
for (i = 0; i < ARRAY_SIZE(s->slots); i++)
668
for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
655
669
s->slots[i].slot = i;
658
672
s->fd = qemu_open("/dev/kvm", O_RDWR);
659
673
if (s->fd == -1) {
688
/* initially, KVM allocated its own memory and we had to jump through
689
* hooks to make phys_ram_base point to this. Modern versions of KVM
690
* just use a user allocated buffer so we can use regular pages
691
* unmodified. Make sure we have a sufficiently modern version of KVM.
693
if (!kvm_check_extension(s, KVM_CAP_USER_MEMORY)) {
695
fprintf(stderr, "kvm does not support KVM_CAP_USER_MEMORY\n%s",
700
/* There was a nasty bug in < kvm-80 that prevents memory slots from being
701
* destroyed properly. Since we rely on this capability, refuse to work
702
* with any kernel without this capability. */
703
if (!kvm_check_extension(s, KVM_CAP_DESTROY_MEMORY_REGION_WORKS)) {
707
"KVM kernel module broken (DESTROY_MEMORY_REGION).\n%s",
712
s->coalesced_mmio = 0;
713
#ifdef KVM_CAP_COALESCED_MMIO
703
missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
706
kvm_check_extension_list(s, kvm_arch_required_capabilities);
710
fprintf(stderr, "kvm does not support %s\n%s",
711
missing_cap->name, upgrade_note);
714
715
s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
715
s->coalesced_mmio_ring = NULL;
718
717
s->broken_set_mem_region = 1;
719
718
#ifdef KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
720
ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
719
ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
722
721
s->broken_set_mem_region = 0;
775
static int kvm_handle_io(uint16_t port, void *data, int direction, int size,
777
static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
779
781
uint8_t *ptr = data;
814
814
#ifdef KVM_CAP_INTERNAL_ERROR_DATA
815
static void kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
815
static int kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
817
fprintf(stderr, "KVM internal error.");
818
818
if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
821
fprintf(stderr, "KVM internal error. Suberror: %d\n",
822
run->internal.suberror);
821
fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
824
822
for (i = 0; i < run->internal.ndata; ++i) {
825
823
fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
826
824
i, (uint64_t)run->internal.data[i]);
827
fprintf(stderr, "\n");
829
cpu_dump_state(env, stderr, fprintf, 0);
830
829
if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
831
830
fprintf(stderr, "emulation failure\n");
832
if (!kvm_arch_stop_on_emulation_error(env))
831
if (!kvm_arch_stop_on_emulation_error(env)) {
832
cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
835
836
/* FIXME: Should trigger a qmp message to let management know
836
837
* something went wrong.
842
843
void kvm_flush_coalesced_mmio_buffer(void)
844
#ifdef KVM_CAP_COALESCED_MMIO
845
845
KVMState *s = kvm_state;
846
846
if (s->coalesced_mmio_ring) {
847
847
struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
871
870
void kvm_cpu_synchronize_state(CPUState *env)
873
if (!env->kvm_vcpu_dirty)
872
if (!env->kvm_vcpu_dirty) {
874
873
run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
877
877
void kvm_cpu_synchronize_post_reset(CPUState *env)
894
894
DPRINTF("kvm_cpu_exec()\n");
896
if (kvm_arch_process_irqchip_events(env)) {
897
env->exit_request = 0;
901
cpu_single_env = env;
897
#ifndef CONFIG_IOTHREAD
898
if (env->exit_request) {
899
DPRINTF("interrupt exit requested\n");
905
if (kvm_arch_process_irqchip_events(env)) {
910
904
if (env->kvm_vcpu_dirty) {
911
905
kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
912
906
env->kvm_vcpu_dirty = 0;
915
909
kvm_arch_pre_run(env, run);
910
if (env->exit_request) {
911
DPRINTF("interrupt exit requested\n");
913
* KVM requires us to reenter the kernel after IO exits to complete
914
* instruction emulation. This self-signal will ensure that we
917
qemu_cpu_kick_self();
916
919
cpu_single_env = NULL;
917
920
qemu_mutex_unlock_iothread();
918
922
ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
919
924
qemu_mutex_lock_iothread();
920
925
cpu_single_env = env;
921
926
kvm_arch_post_run(env, run);
928
kvm_flush_coalesced_mmio_buffer();
923
930
if (ret == -EINTR || ret == -EAGAIN) {
925
931
DPRINTF("io window exit\n");
935
kvm_flush_coalesced_mmio_buffer();
937
941
ret = 0; /* exit loop */
938
942
switch (run->exit_reason) {
939
943
case KVM_EXIT_IO:
940
944
DPRINTF("handle_io\n");
941
ret = kvm_handle_io(run->io.port,
942
(uint8_t *)run + run->io.data_offset,
945
kvm_handle_io(run->io.port,
946
(uint8_t *)run + run->io.data_offset,
947
952
case KVM_EXIT_MMIO:
948
953
DPRINTF("handle_mmio\n");
958
963
case KVM_EXIT_SHUTDOWN:
959
964
DPRINTF("shutdown\n");
960
965
qemu_system_reset_request();
963
967
case KVM_EXIT_UNKNOWN:
964
DPRINTF("kvm_exit_unknown\n");
966
case KVM_EXIT_FAIL_ENTRY:
967
DPRINTF("kvm_exit_fail_entry\n");
969
case KVM_EXIT_EXCEPTION:
970
DPRINTF("kvm_exit_exception\n");
968
fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
969
(uint64_t)run->hw.hardware_exit_reason);
972
972
#ifdef KVM_CAP_INTERNAL_ERROR_DATA
973
973
case KVM_EXIT_INTERNAL_ERROR:
974
kvm_handle_internal_error(env, run);
974
ret = kvm_handle_internal_error(env, run);
977
977
case KVM_EXIT_DEBUG:
978
978
DPRINTF("kvm_exit_debug\n");
979
979
#ifdef KVM_CAP_SET_GUEST_DEBUG
980
980
if (kvm_arch_debug(&run->debug.arch)) {
981
env->exception_index = EXCP_DEBUG;
984
984
/* re-enter, this exception was guest-internal */
1047
1051
ret = ioctl(env->kvm_fd, type, arg);
1054
1058
int kvm_has_sync_mmu(void)
1056
#ifdef KVM_CAP_SYNC_MMU
1057
KVMState *s = kvm_state;
1059
return kvm_check_extension(s, KVM_CAP_SYNC_MMU);
1060
return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1065
1063
int kvm_has_vcpu_events(void)
1219
1222
err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1223
1227
QTAILQ_REMOVE(¤t_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1226
1230
err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1231
1236
for (env = first_cpu; env != NULL; env = env->next_cpu) {
1232
1237
err = kvm_update_guest_debug(env, 0);
1246
1252
if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1247
1253
/* Try harder to find a CPU that currently sees the breakpoint. */
1248
1254
for (env = first_cpu; env != NULL; env = env->next_cpu) {
1249
if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
1255
if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1254
1261
kvm_arch_remove_all_hw_breakpoints();
1256
for (env = first_cpu; env != NULL; env = env->next_cpu)
1263
for (env = first_cpu; env != NULL; env = env->next_cpu) {
1257
1264
kvm_update_guest_debug(env, 0);
1260
1268
#else /* !KVM_CAP_SET_GUEST_DEBUG */
1354
if (!kvm_enabled()) {
1346
1355
return -ENOSYS;
1348
1358
kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1349
1360
r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1354
1366
return -ENOSYS;
1370
int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr)
1372
return kvm_arch_on_sigbus_vcpu(env, code, addr);
1375
int kvm_on_sigbus(int code, void *addr)
1377
return kvm_arch_on_sigbus(code, addr);