]> git.ipfire.org Git - thirdparty/qemu.git/blame - accel/kvm/kvm-all.c
kvm: extract kvm_log_clear_one_slot
[thirdparty/qemu.git] / accel / kvm / kvm-all.c
CommitLineData
05330448
AL
1/*
2 * QEMU KVM support
3 *
4 * Copyright IBM, Corp. 2008
5832d1f2 5 * Red Hat, Inc. 2008
05330448
AL
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
5832d1f2 9 * Glauber Costa <gcosta@redhat.com>
05330448
AL
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15
d38ea87a 16#include "qemu/osdep.h"
05330448 17#include <sys/ioctl.h>
05330448
AL
18
19#include <linux/kvm.h>
20
1de7afc9
PB
21#include "qemu/atomic.h"
22#include "qemu/option.h"
23#include "qemu/config-file.h"
4b3cfe72 24#include "qemu/error-report.h"
556969e9 25#include "qapi/error.h"
a2cb15b0 26#include "hw/pci/msi.h"
d1f6af6a 27#include "hw/pci/msix.h"
d426d9fb 28#include "hw/s390x/adapter.h"
022c62cb 29#include "exec/gdbstub.h"
8571ed35 30#include "sysemu/kvm_int.h"
54d31236 31#include "sysemu/runstate.h"
d2528bdc 32#include "sysemu/cpus.h"
46517dd4 33#include "sysemu/sysemu.h"
1de7afc9 34#include "qemu/bswap.h"
022c62cb 35#include "exec/memory.h"
747afd5b 36#include "exec/ram_addr.h"
022c62cb 37#include "exec/address-spaces.h"
1de7afc9 38#include "qemu/event_notifier.h"
db725815 39#include "qemu/main-loop.h"
92229a57 40#include "trace.h"
197e3524 41#include "hw/irq.h"
b20e3780 42#include "sysemu/sev.h"
f5948942 43#include "sysemu/balloon.h"
05330448 44
135a129a
AK
45#include "hw/boards.h"
46
d2f2b8a7
SH
47/* This check must be after config-host.h is included */
48#ifdef CONFIG_EVENTFD
49#include <sys/eventfd.h>
50#endif
51
bc92e4e9
AJ
52/* KVM uses PAGE_SIZE in its definition of KVM_COALESCED_MMIO_MAX. We
53 * need to use the real host PAGE_SIZE, as that's what KVM will use.
54 */
55#define PAGE_SIZE getpagesize()
f65ed4c1 56
05330448
AL
57//#define DEBUG_KVM
58
59#ifdef DEBUG_KVM
8c0d577e 60#define DPRINTF(fmt, ...) \
05330448
AL
61 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
62#else
8c0d577e 63#define DPRINTF(fmt, ...) \
05330448
AL
64 do { } while (0)
65#endif
66
04fa27f5
JK
67#define KVM_MSI_HASHTAB_SIZE 256
68
4c055ab5
GZ
69struct KVMParkedVcpu {
70 unsigned long vcpu_id;
71 int kvm_fd;
72 QLIST_ENTRY(KVMParkedVcpu) node;
73};
74
9d1c35df 75struct KVMState
05330448 76{
fc02086b
EH
77 AccelState parent_obj;
78
fb541ca5 79 int nr_slots;
05330448
AL
80 int fd;
81 int vmfd;
f65ed4c1 82 int coalesced_mmio;
e6d34aee 83 int coalesced_pio;
62a2744c 84 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
1cae88b9 85 bool coalesced_flush_in_progress;
a0fb002c 86 int vcpu_events;
b0b1d690 87 int robust_singlestep;
ff44f1a3 88 int debugregs;
e22a25c9 89#ifdef KVM_CAP_SET_GUEST_DEBUG
b58deb34 90 QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
e22a25c9 91#endif
ebbfef2f 92 int max_nested_state_len;
d2f2b8a7 93 int many_ioeventfds;
3ab73842 94 int intx_set_mask;
62dd4eda 95 bool sync_mmu;
ff4aa114 96 bool manual_dirty_log_protect;
92e4b519
DG
97 /* The man page (and posix) say ioctl numbers are signed int, but
98 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
99 * unsigned, and treating them as signed here can break things */
e333cd69 100 unsigned irq_set_ioctl;
aed6efb9 101 unsigned int sigmask_len;
197e3524 102 GHashTable *gsimap;
84b058d7
JK
103#ifdef KVM_CAP_IRQ_ROUTING
104 struct kvm_irq_routing *irq_routes;
105 int nr_allocated_irq_routes;
8269fb70 106 unsigned long *used_gsi_bitmap;
4e2e4e63 107 unsigned int gsi_count;
b58deb34 108 QTAILQ_HEAD(, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE];
84b058d7 109#endif
7bbda04c 110 KVMMemoryListener memory_listener;
4c055ab5 111 QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
b20e3780
BS
112
113 /* memory encryption */
114 void *memcrypt_handle;
54e89539 115 int (*memcrypt_encrypt_data)(void *handle, uint8_t *ptr, uint64_t len);
8072aae3
AK
116
117 /* For "info mtree -f" to tell if an MR is registered in KVM */
118 int nr_as;
119 struct KVMAs {
120 KVMMemoryListener *ml;
121 AddressSpace *as;
122 } *as;
9d1c35df 123};
05330448 124
6a7af8cb 125KVMState *kvm_state;
3d4b2649 126bool kvm_kernel_irqchip;
15eafc2e 127bool kvm_split_irqchip;
7ae26bd4 128bool kvm_async_interrupts_allowed;
215e79c0 129bool kvm_halt_in_kernel_allowed;
69e03ae6 130bool kvm_eventfds_allowed;
cc7e0ddf 131bool kvm_irqfds_allowed;
f41389ae 132bool kvm_resamplefds_allowed;
614e41bc 133bool kvm_msi_via_irqfd_allowed;
f3e1bed8 134bool kvm_gsi_routing_allowed;
76fe21de 135bool kvm_gsi_direct_mapping;
13eed94e 136bool kvm_allowed;
df9c8b75 137bool kvm_readonly_mem_allowed;
d0a073a1 138bool kvm_vm_attributes_allowed;
50bf31b9 139bool kvm_direct_msi_allowed;
35108223 140bool kvm_ioeventfd_any_length_allowed;
767a554a 141bool kvm_msi_use_devid;
cf0f7cf9 142static bool kvm_immediate_exit;
05330448 143
94a8d39a
JK
144static const KVMCapabilityInfo kvm_required_capabilites[] = {
145 KVM_CAP_INFO(USER_MEMORY),
146 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
89de4b91 147 KVM_CAP_INFO(JOIN_MEMORY_REGIONS_WORKS),
94a8d39a
JK
148 KVM_CAP_LAST_INFO
149};
150
36adac49
PX
151#define kvm_slots_lock(kml) qemu_mutex_lock(&(kml)->slots_lock)
152#define kvm_slots_unlock(kml) qemu_mutex_unlock(&(kml)->slots_lock)
153
44f2e6c1
BR
154int kvm_get_max_memslots(void)
155{
156 KVMState *s = KVM_STATE(current_machine->accelerator);
157
158 return s->nr_slots;
159}
160
b20e3780
BS
161bool kvm_memcrypt_enabled(void)
162{
163 if (kvm_state && kvm_state->memcrypt_handle) {
164 return true;
165 }
166
167 return false;
168}
169
54e89539
BS
170int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len)
171{
172 if (kvm_state->memcrypt_handle &&
173 kvm_state->memcrypt_encrypt_data) {
174 return kvm_state->memcrypt_encrypt_data(kvm_state->memcrypt_handle,
175 ptr, len);
176 }
177
178 return 1;
179}
180
36adac49 181/* Called with KVMMemoryListener.slots_lock held */
7bbda04c 182static KVMSlot *kvm_get_free_slot(KVMMemoryListener *kml)
05330448 183{
7bbda04c 184 KVMState *s = kvm_state;
05330448
AL
185 int i;
186
fb541ca5 187 for (i = 0; i < s->nr_slots; i++) {
7bbda04c
PB
188 if (kml->slots[i].memory_size == 0) {
189 return &kml->slots[i];
a426e122 190 }
05330448
AL
191 }
192
b8865591
IM
193 return NULL;
194}
195
196bool kvm_has_free_slot(MachineState *ms)
197{
7bbda04c 198 KVMState *s = KVM_STATE(ms->accelerator);
36adac49
PX
199 bool result;
200 KVMMemoryListener *kml = &s->memory_listener;
201
202 kvm_slots_lock(kml);
203 result = !!kvm_get_free_slot(kml);
204 kvm_slots_unlock(kml);
7bbda04c 205
36adac49 206 return result;
b8865591
IM
207}
208
36adac49 209/* Called with KVMMemoryListener.slots_lock held */
7bbda04c 210static KVMSlot *kvm_alloc_slot(KVMMemoryListener *kml)
b8865591 211{
7bbda04c 212 KVMSlot *slot = kvm_get_free_slot(kml);
b8865591
IM
213
214 if (slot) {
215 return slot;
216 }
217
d3f8d37f
AL
218 fprintf(stderr, "%s: no free slot available\n", __func__);
219 abort();
220}
221
7bbda04c 222static KVMSlot *kvm_lookup_matching_slot(KVMMemoryListener *kml,
a8170e5e 223 hwaddr start_addr,
2747e716 224 hwaddr size)
d3f8d37f 225{
7bbda04c 226 KVMState *s = kvm_state;
d3f8d37f
AL
227 int i;
228
fb541ca5 229 for (i = 0; i < s->nr_slots; i++) {
7bbda04c 230 KVMSlot *mem = &kml->slots[i];
d3f8d37f 231
2747e716 232 if (start_addr == mem->start_addr && size == mem->memory_size) {
d3f8d37f
AL
233 return mem;
234 }
235 }
236
05330448
AL
237 return NULL;
238}
239
5ea69c2e
DH
240/*
241 * Calculate and align the start address and the size of the section.
242 * Return the size. If the size is 0, the aligned section is empty.
243 */
244static hwaddr kvm_align_section(MemoryRegionSection *section,
245 hwaddr *start)
246{
247 hwaddr size = int128_get64(section->size);
a6ffc423 248 hwaddr delta, aligned;
5ea69c2e
DH
249
250 /* kvm works in page size chunks, but the function may be called
251 with sub-page size and unaligned start address. Pad the start
252 address to next and truncate size to previous page boundary. */
a6ffc423
DH
253 aligned = ROUND_UP(section->offset_within_address_space,
254 qemu_real_host_page_size);
255 delta = aligned - section->offset_within_address_space;
256 *start = aligned;
5ea69c2e
DH
257 if (delta > size) {
258 return 0;
259 }
5ea69c2e 260
a6ffc423 261 return (size - delta) & qemu_real_host_page_mask;
5ea69c2e
DH
262}
263
9f213ed9 264int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
a8170e5e 265 hwaddr *phys_addr)
983dfc3b 266{
7bbda04c 267 KVMMemoryListener *kml = &s->memory_listener;
36adac49 268 int i, ret = 0;
983dfc3b 269
36adac49 270 kvm_slots_lock(kml);
fb541ca5 271 for (i = 0; i < s->nr_slots; i++) {
7bbda04c 272 KVMSlot *mem = &kml->slots[i];
983dfc3b 273
9f213ed9
AK
274 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
275 *phys_addr = mem->start_addr + (ram - mem->ram);
36adac49
PX
276 ret = 1;
277 break;
983dfc3b
HY
278 }
279 }
36adac49 280 kvm_slots_unlock(kml);
983dfc3b 281
36adac49 282 return ret;
983dfc3b
HY
283}
284
6c090d4a 285static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, bool new)
5832d1f2 286{
7bbda04c 287 KVMState *s = kvm_state;
5832d1f2 288 struct kvm_userspace_memory_region mem;
fe29141b 289 int ret;
5832d1f2 290
38bfe691 291 mem.slot = slot->slot | (kml->as_id << 16);
5832d1f2 292 mem.guest_phys_addr = slot->start_addr;
9f213ed9 293 mem.userspace_addr = (unsigned long)slot->ram;
5832d1f2 294 mem.flags = slot->flags;
651eb0f4 295
6c090d4a 296 if (slot->memory_size && !new && (mem.flags ^ slot->old_flags) & KVM_MEM_READONLY) {
235e8982
JJ
297 /* Set the slot size to 0 before setting the slot to the desired
298 * value. This is needed based on KVM commit 75d61fbc. */
299 mem.memory_size = 0;
300 kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
301 }
302 mem.memory_size = slot->memory_size;
fe29141b 303 ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
6c090d4a 304 slot->old_flags = mem.flags;
fe29141b
AK
305 trace_kvm_set_user_memory(mem.slot, mem.flags, mem.guest_phys_addr,
306 mem.memory_size, mem.userspace_addr, ret);
307 return ret;
5832d1f2
AL
308}
309
4c055ab5
GZ
310int kvm_destroy_vcpu(CPUState *cpu)
311{
312 KVMState *s = kvm_state;
313 long mmap_size;
314 struct KVMParkedVcpu *vcpu = NULL;
315 int ret = 0;
316
317 DPRINTF("kvm_destroy_vcpu\n");
318
b1115c99
LA
319 ret = kvm_arch_destroy_vcpu(cpu);
320 if (ret < 0) {
321 goto err;
322 }
323
4c055ab5
GZ
324 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
325 if (mmap_size < 0) {
326 ret = mmap_size;
327 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
328 goto err;
329 }
330
331 ret = munmap(cpu->kvm_run, mmap_size);
332 if (ret < 0) {
333 goto err;
334 }
335
336 vcpu = g_malloc0(sizeof(*vcpu));
337 vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
338 vcpu->kvm_fd = cpu->kvm_fd;
339 QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
340err:
341 return ret;
342}
343
344static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
345{
346 struct KVMParkedVcpu *cpu;
347
348 QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
349 if (cpu->vcpu_id == vcpu_id) {
350 int kvm_fd;
351
352 QLIST_REMOVE(cpu, node);
353 kvm_fd = cpu->kvm_fd;
354 g_free(cpu);
355 return kvm_fd;
356 }
357 }
358
359 return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
360}
361
504134d2 362int kvm_init_vcpu(CPUState *cpu)
05330448
AL
363{
364 KVMState *s = kvm_state;
365 long mmap_size;
366 int ret;
367
8c0d577e 368 DPRINTF("kvm_init_vcpu\n");
05330448 369
4c055ab5 370 ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
05330448 371 if (ret < 0) {
8c0d577e 372 DPRINTF("kvm_create_vcpu failed\n");
05330448
AL
373 goto err;
374 }
375
8737c51c 376 cpu->kvm_fd = ret;
a60f24b5 377 cpu->kvm_state = s;
99f31832 378 cpu->vcpu_dirty = true;
05330448
AL
379
380 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
381 if (mmap_size < 0) {
748a680b 382 ret = mmap_size;
8c0d577e 383 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
05330448
AL
384 goto err;
385 }
386
f7575c96 387 cpu->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
8737c51c 388 cpu->kvm_fd, 0);
f7575c96 389 if (cpu->kvm_run == MAP_FAILED) {
05330448 390 ret = -errno;
8c0d577e 391 DPRINTF("mmap'ing vcpu state failed\n");
05330448
AL
392 goto err;
393 }
394
a426e122
JK
395 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
396 s->coalesced_mmio_ring =
f7575c96 397 (void *)cpu->kvm_run + s->coalesced_mmio * PAGE_SIZE;
a426e122 398 }
62a2744c 399
20d695a9 400 ret = kvm_arch_init_vcpu(cpu);
05330448
AL
401err:
402 return ret;
403}
404
5832d1f2
AL
405/*
406 * dirty pages logging control
407 */
25254bbc 408
d6ff5cbc 409static int kvm_mem_flags(MemoryRegion *mr)
25254bbc 410{
d6ff5cbc 411 bool readonly = mr->readonly || memory_region_is_romd(mr);
235e8982 412 int flags = 0;
d6ff5cbc
AJ
413
414 if (memory_region_get_dirty_log_mask(mr) != 0) {
415 flags |= KVM_MEM_LOG_DIRTY_PAGES;
416 }
235e8982
JJ
417 if (readonly && kvm_readonly_mem_allowed) {
418 flags |= KVM_MEM_READONLY;
419 }
420 return flags;
25254bbc
MT
421}
422
36adac49 423/* Called with KVMMemoryListener.slots_lock held */
7bbda04c
PB
424static int kvm_slot_update_flags(KVMMemoryListener *kml, KVMSlot *mem,
425 MemoryRegion *mr)
5832d1f2 426{
d6ff5cbc 427 mem->flags = kvm_mem_flags(mr);
5832d1f2 428
4495d6a7 429 /* If nothing changed effectively, no need to issue ioctl */
6c090d4a 430 if (mem->flags == mem->old_flags) {
25254bbc 431 return 0;
4495d6a7
JK
432 }
433
6c090d4a 434 return kvm_set_user_memory_region(kml, mem, false);
5832d1f2
AL
435}
436
7bbda04c
PB
437static int kvm_section_update_flags(KVMMemoryListener *kml,
438 MemoryRegionSection *section)
25254bbc 439{
343562e8
DH
440 hwaddr start_addr, size;
441 KVMSlot *mem;
36adac49 442 int ret = 0;
25254bbc 443
343562e8
DH
444 size = kvm_align_section(section, &start_addr);
445 if (!size) {
ea8cb1a8 446 return 0;
25254bbc 447 }
343562e8 448
36adac49
PX
449 kvm_slots_lock(kml);
450
343562e8
DH
451 mem = kvm_lookup_matching_slot(kml, start_addr, size);
452 if (!mem) {
e377e87c 453 /* We don't have a slot if we want to trap every access. */
36adac49 454 goto out;
343562e8
DH
455 }
456
36adac49
PX
457 ret = kvm_slot_update_flags(kml, mem, section->mr);
458
459out:
460 kvm_slots_unlock(kml);
461 return ret;
25254bbc
MT
462}
463
a01672d3 464static void kvm_log_start(MemoryListener *listener,
b2dfd71c
PB
465 MemoryRegionSection *section,
466 int old, int new)
5832d1f2 467{
7bbda04c 468 KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
a01672d3
AK
469 int r;
470
b2dfd71c
PB
471 if (old != 0) {
472 return;
473 }
474
7bbda04c 475 r = kvm_section_update_flags(kml, section);
a01672d3
AK
476 if (r < 0) {
477 abort();
478 }
5832d1f2
AL
479}
480
a01672d3 481static void kvm_log_stop(MemoryListener *listener,
b2dfd71c
PB
482 MemoryRegionSection *section,
483 int old, int new)
5832d1f2 484{
7bbda04c 485 KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
a01672d3
AK
486 int r;
487
b2dfd71c
PB
488 if (new != 0) {
489 return;
490 }
491
7bbda04c 492 r = kvm_section_update_flags(kml, section);
a01672d3
AK
493 if (r < 0) {
494 abort();
495 }
5832d1f2
AL
496}
497
8369e01c 498/* get kvm's dirty pages bitmap and update qemu's */
ffcde12f
AK
499static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
500 unsigned long *bitmap)
96c1606b 501{
8e41fb63
FZ
502 ram_addr_t start = section->offset_within_region +
503 memory_region_get_ram_addr(section->mr);
5ff7fb77
JQ
504 ram_addr_t pages = int128_get64(section->size) / getpagesize();
505
506 cpu_physical_memory_set_dirty_lebitmap(bitmap, start, pages);
8369e01c 507 return 0;
96c1606b
AG
508}
509
8369e01c
MT
510#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
511
5832d1f2 512/**
4a12a11a 513 * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space
5832d1f2 514 *
4a12a11a
PX
515 * This function will first try to fetch dirty bitmap from the kernel,
516 * and then updates qemu's dirty bitmap.
517 *
36adac49
PX
518 * NOTE: caller must be with kml->slots_lock held.
519 *
4a12a11a
PX
520 * @kml: the KVM memory listener object
521 * @section: the memory section to sync the dirty bitmap with
5832d1f2 522 */
7bbda04c
PB
523static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml,
524 MemoryRegionSection *section)
5832d1f2
AL
525{
526 KVMState *s = kvm_state;
714f78c5 527 struct kvm_dirty_log d = {};
151f7749 528 KVMSlot *mem;
67548f09 529 hwaddr start_addr, size;
36adac49 530 int ret = 0;
67548f09
DH
531
532 size = kvm_align_section(section, &start_addr);
533 if (size) {
534 mem = kvm_lookup_matching_slot(kml, start_addr, size);
535 if (!mem) {
e377e87c 536 /* We don't have a slot if we want to trap every access. */
36adac49 537 goto out;
151f7749 538 }
5832d1f2 539
51b0c606
MT
540 /* XXX bad kernel interface alert
541 * For dirty bitmap, kernel allocates array of size aligned to
542 * bits-per-long. But for case when the kernel is 64bits and
543 * the userspace is 32bits, userspace can't align to the same
544 * bits-per-long, since sizeof(long) is different between kernel
545 * and user space. This way, userspace will provide buffer which
546 * may be 4 bytes less than the kernel will use, resulting in
547 * userspace memory corruption (which is not detectable by valgrind
548 * too, in most cases).
549 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
cb8d4c8f 550 * a hope that sizeof(long) won't become >8 any time soon.
51b0c606
MT
551 */
552 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
553 /*HOST_LONG_BITS*/ 64) / 8;
9f4bf4ba
PX
554 if (!mem->dirty_bmap) {
555 /* Allocate on the first log_sync, once and for all */
556 mem->dirty_bmap = g_malloc0(size);
557 }
5832d1f2 558
9f4bf4ba 559 d.dirty_bitmap = mem->dirty_bmap;
38bfe691 560 d.slot = mem->slot | (kml->as_id << 16);
50212d63 561 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
8c0d577e 562 DPRINTF("ioctl failed %d\n", errno);
36adac49
PX
563 ret = -1;
564 goto out;
151f7749 565 }
5832d1f2 566
ffcde12f 567 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
5832d1f2 568 }
36adac49
PX
569out:
570 return ret;
5832d1f2
AL
571}
572
ff4aa114
PX
573/* Alignment requirement for KVM_CLEAR_DIRTY_LOG - 64 pages */
574#define KVM_CLEAR_LOG_SHIFT 6
575#define KVM_CLEAR_LOG_ALIGN (qemu_real_host_page_size << KVM_CLEAR_LOG_SHIFT)
576#define KVM_CLEAR_LOG_MASK (-KVM_CLEAR_LOG_ALIGN)
577
4222147d
PB
578static int kvm_log_clear_one_slot(KVMSlot *mem, int as_id, uint64_t start,
579 uint64_t size)
ff4aa114
PX
580{
581 KVMState *s = kvm_state;
4222147d 582 uint64_t end, bmap_start, start_delta, bmap_npages;
ff4aa114 583 struct kvm_clear_dirty_log d;
ff4aa114 584 unsigned long *bmap_clear = NULL, psize = qemu_real_host_page_size;
4222147d 585 int ret;
ff4aa114
PX
586
587 /*
588 * We need to extend either the start or the size or both to
589 * satisfy the KVM interface requirement. Firstly, do the start
590 * page alignment on 64 host pages
591 */
592 bmap_start = (start - mem->start_addr) & KVM_CLEAR_LOG_MASK;
593 start_delta = start - mem->start_addr - bmap_start;
594 bmap_start /= psize;
595
596 /*
597 * The kernel interface has restriction on the size too, that either:
598 *
599 * (1) the size is 64 host pages aligned (just like the start), or
600 * (2) the size fills up until the end of the KVM memslot.
601 */
602 bmap_npages = DIV_ROUND_UP(size + start_delta, KVM_CLEAR_LOG_ALIGN)
603 << KVM_CLEAR_LOG_SHIFT;
604 end = mem->memory_size / psize;
605 if (bmap_npages > end - bmap_start) {
606 bmap_npages = end - bmap_start;
607 }
608 start_delta /= psize;
609
610 /*
611 * Prepare the bitmap to clear dirty bits. Here we must guarantee
612 * that we won't clear any unknown dirty bits otherwise we might
613 * accidentally clear some set bits which are not yet synced from
614 * the kernel into QEMU's bitmap, then we'll lose track of the
615 * guest modifications upon those pages (which can directly lead
616 * to guest data loss or panic after migration).
617 *
618 * Layout of the KVMSlot.dirty_bmap:
619 *
620 * |<-------- bmap_npages -----------..>|
621 * [1]
622 * start_delta size
623 * |----------------|-------------|------------------|------------|
624 * ^ ^ ^ ^
625 * | | | |
626 * start bmap_start (start) end
627 * of memslot of memslot
628 *
629 * [1] bmap_npages can be aligned to either 64 pages or the end of slot
630 */
631
632 assert(bmap_start % BITS_PER_LONG == 0);
633 /* We should never do log_clear before log_sync */
634 assert(mem->dirty_bmap);
635 if (start_delta) {
636 /* Slow path - we need to manipulate a temp bitmap */
637 bmap_clear = bitmap_new(bmap_npages);
638 bitmap_copy_with_src_offset(bmap_clear, mem->dirty_bmap,
639 bmap_start, start_delta + size / psize);
640 /*
641 * We need to fill the holes at start because that was not
642 * specified by the caller and we extended the bitmap only for
643 * 64 pages alignment
644 */
645 bitmap_clear(bmap_clear, 0, start_delta);
646 d.dirty_bitmap = bmap_clear;
647 } else {
648 /* Fast path - start address aligns well with BITS_PER_LONG */
649 d.dirty_bitmap = mem->dirty_bmap + BIT_WORD(bmap_start);
650 }
651
652 d.first_page = bmap_start;
653 /* It should never overflow. If it happens, say something */
654 assert(bmap_npages <= UINT32_MAX);
655 d.num_pages = bmap_npages;
4222147d 656 d.slot = mem->slot | (as_id << 16);
ff4aa114
PX
657
658 if (kvm_vm_ioctl(s, KVM_CLEAR_DIRTY_LOG, &d) == -1) {
659 ret = -errno;
660 error_report("%s: KVM_CLEAR_DIRTY_LOG failed, slot=%d, "
661 "start=0x%"PRIx64", size=0x%"PRIx32", errno=%d",
662 __func__, d.slot, (uint64_t)d.first_page,
663 (uint32_t)d.num_pages, ret);
664 } else {
665 ret = 0;
666 trace_kvm_clear_dirty_log(d.slot, d.first_page, d.num_pages);
667 }
668
669 /*
670 * After we have updated the remote dirty bitmap, we update the
671 * cached bitmap as well for the memslot, then if another user
672 * clears the same region we know we shouldn't clear it again on
673 * the remote otherwise it's data loss as well.
674 */
675 bitmap_clear(mem->dirty_bmap, bmap_start + start_delta,
676 size / psize);
677 /* This handles the NULL case well */
678 g_free(bmap_clear);
4222147d
PB
679 return ret;
680}
681
682
683/**
684 * kvm_physical_log_clear - Clear the kernel's dirty bitmap for range
685 *
686 * NOTE: this will be a no-op if we haven't enabled manual dirty log
687 * protection in the host kernel because in that case this operation
688 * will be done within log_sync().
689 *
690 * @kml: the kvm memory listener
691 * @section: the memory range to clear dirty bitmap
692 */
693static int kvm_physical_log_clear(KVMMemoryListener *kml,
694 MemoryRegionSection *section)
695{
696 KVMState *s = kvm_state;
697 uint64_t start, size;
698 KVMSlot *mem = NULL;
699 int ret, i;
700
701 if (!s->manual_dirty_log_protect) {
702 /* No need to do explicit clear */
703 return 0;
704 }
705
706 start = section->offset_within_address_space;
707 size = int128_get64(section->size);
708
709 if (!size) {
710 /* Nothing more we can do... */
711 return 0;
712 }
713
714 kvm_slots_lock(kml);
715
716 /* Find any possible slot that covers the section */
717 for (i = 0; i < s->nr_slots; i++) {
718 mem = &kml->slots[i];
719 if (mem->start_addr <= start &&
720 start + size <= mem->start_addr + mem->memory_size) {
721 break;
722 }
723 }
724
725 /*
726 * We should always find one memslot until this point, otherwise
727 * there could be something wrong from the upper layer
728 */
729 assert(mem && i != s->nr_slots);
730 ret = kvm_log_clear_one_slot(mem, kml->as_id, start, size);
ff4aa114
PX
731
732 kvm_slots_unlock(kml);
733
734 return ret;
735}
736
95d2994a
AK
737static void kvm_coalesce_mmio_region(MemoryListener *listener,
738 MemoryRegionSection *secion,
a8170e5e 739 hwaddr start, hwaddr size)
f65ed4c1 740{
f65ed4c1
AL
741 KVMState *s = kvm_state;
742
743 if (s->coalesced_mmio) {
744 struct kvm_coalesced_mmio_zone zone;
745
746 zone.addr = start;
747 zone.size = size;
7e680753 748 zone.pad = 0;
f65ed4c1 749
95d2994a 750 (void)kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
f65ed4c1 751 }
f65ed4c1
AL
752}
753
95d2994a
AK
754static void kvm_uncoalesce_mmio_region(MemoryListener *listener,
755 MemoryRegionSection *secion,
a8170e5e 756 hwaddr start, hwaddr size)
f65ed4c1 757{
f65ed4c1
AL
758 KVMState *s = kvm_state;
759
760 if (s->coalesced_mmio) {
761 struct kvm_coalesced_mmio_zone zone;
762
763 zone.addr = start;
764 zone.size = size;
7e680753 765 zone.pad = 0;
f65ed4c1 766
95d2994a 767 (void)kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
f65ed4c1 768 }
f65ed4c1
AL
769}
770
e6d34aee
PH
771static void kvm_coalesce_pio_add(MemoryListener *listener,
772 MemoryRegionSection *section,
773 hwaddr start, hwaddr size)
774{
775 KVMState *s = kvm_state;
776
777 if (s->coalesced_pio) {
778 struct kvm_coalesced_mmio_zone zone;
779
780 zone.addr = start;
781 zone.size = size;
782 zone.pio = 1;
783
784 (void)kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
785 }
786}
787
788static void kvm_coalesce_pio_del(MemoryListener *listener,
789 MemoryRegionSection *section,
790 hwaddr start, hwaddr size)
791{
792 KVMState *s = kvm_state;
793
794 if (s->coalesced_pio) {
795 struct kvm_coalesced_mmio_zone zone;
796
797 zone.addr = start;
798 zone.size = size;
799 zone.pio = 1;
800
801 (void)kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
802 }
803}
804
805static MemoryListener kvm_coalesced_pio_listener = {
806 .coalesced_io_add = kvm_coalesce_pio_add,
807 .coalesced_io_del = kvm_coalesce_pio_del,
808};
809
ad7b8b33
AL
810int kvm_check_extension(KVMState *s, unsigned int extension)
811{
812 int ret;
813
814 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
815 if (ret < 0) {
816 ret = 0;
817 }
818
819 return ret;
820}
821
7d0a07fa
AG
822int kvm_vm_check_extension(KVMState *s, unsigned int extension)
823{
824 int ret;
825
826 ret = kvm_vm_ioctl(s, KVM_CHECK_EXTENSION, extension);
827 if (ret < 0) {
828 /* VM wide version not implemented, use global one instead */
829 ret = kvm_check_extension(s, extension);
830 }
831
832 return ret;
833}
834
b680c5ba
GK
835static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size)
836{
837#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
838 /* The kernel expects ioeventfd values in HOST_WORDS_BIGENDIAN
839 * endianness, but the memory core hands them in target endianness.
840 * For example, PPC is always treated as big-endian even if running
841 * on KVM and on PPC64LE. Correct here.
842 */
843 switch (size) {
844 case 2:
845 val = bswap16(val);
846 break;
847 case 4:
848 val = bswap32(val);
849 break;
850 }
851#endif
852 return val;
853}
854
584f2be7 855static int kvm_set_ioeventfd_mmio(int fd, hwaddr addr, uint32_t val,
41cb62c2 856 bool assign, uint32_t size, bool datamatch)
500ffd4a
MT
857{
858 int ret;
03a96b83
TH
859 struct kvm_ioeventfd iofd = {
860 .datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0,
861 .addr = addr,
862 .len = size,
863 .flags = 0,
864 .fd = fd,
865 };
500ffd4a 866
876d16cd
DDAG
867 trace_kvm_set_ioeventfd_mmio(fd, (uint64_t)addr, val, assign, size,
868 datamatch);
500ffd4a
MT
869 if (!kvm_enabled()) {
870 return -ENOSYS;
871 }
872
41cb62c2
MT
873 if (datamatch) {
874 iofd.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH;
875 }
500ffd4a
MT
876 if (!assign) {
877 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
878 }
879
880 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
881
882 if (ret < 0) {
883 return -errno;
884 }
885
886 return 0;
887}
888
44c3f8f7 889static int kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint16_t val,
41cb62c2 890 bool assign, uint32_t size, bool datamatch)
500ffd4a
MT
891{
892 struct kvm_ioeventfd kick = {
b680c5ba 893 .datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0,
500ffd4a 894 .addr = addr,
41cb62c2 895 .flags = KVM_IOEVENTFD_FLAG_PIO,
44c3f8f7 896 .len = size,
500ffd4a
MT
897 .fd = fd,
898 };
899 int r;
876d16cd 900 trace_kvm_set_ioeventfd_pio(fd, addr, val, assign, size, datamatch);
500ffd4a
MT
901 if (!kvm_enabled()) {
902 return -ENOSYS;
903 }
41cb62c2
MT
904 if (datamatch) {
905 kick.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH;
906 }
500ffd4a
MT
907 if (!assign) {
908 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
909 }
910 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
911 if (r < 0) {
912 return r;
913 }
914 return 0;
915}
916
917
d2f2b8a7
SH
918static int kvm_check_many_ioeventfds(void)
919{
d0dcac83
SH
920 /* Userspace can use ioeventfd for io notification. This requires a host
921 * that supports eventfd(2) and an I/O thread; since eventfd does not
922 * support SIGIO it cannot interrupt the vcpu.
923 *
924 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
d2f2b8a7
SH
925 * can avoid creating too many ioeventfds.
926 */
12d4536f 927#if defined(CONFIG_EVENTFD)
d2f2b8a7
SH
928 int ioeventfds[7];
929 int i, ret = 0;
930 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
931 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
932 if (ioeventfds[i] < 0) {
933 break;
934 }
41cb62c2 935 ret = kvm_set_ioeventfd_pio(ioeventfds[i], 0, i, true, 2, true);
d2f2b8a7
SH
936 if (ret < 0) {
937 close(ioeventfds[i]);
938 break;
939 }
940 }
941
942 /* Decide whether many devices are supported or not */
943 ret = i == ARRAY_SIZE(ioeventfds);
944
945 while (i-- > 0) {
41cb62c2 946 kvm_set_ioeventfd_pio(ioeventfds[i], 0, i, false, 2, true);
d2f2b8a7
SH
947 close(ioeventfds[i]);
948 }
949 return ret;
950#else
951 return 0;
952#endif
953}
954
94a8d39a
JK
955static const KVMCapabilityInfo *
956kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
957{
958 while (list->name) {
959 if (!kvm_check_extension(s, list->value)) {
960 return list;
961 }
962 list++;
963 }
964 return NULL;
965}
966
7bbda04c
PB
967static void kvm_set_phys_mem(KVMMemoryListener *kml,
968 MemoryRegionSection *section, bool add)
46dbef6a 969{
f357f564 970 KVMSlot *mem;
46dbef6a 971 int err;
a01672d3 972 MemoryRegion *mr = section->mr;
235e8982 973 bool writeable = !mr->readonly && !mr->rom_device;
5ea69c2e
DH
974 hwaddr start_addr, size;
975 void *ram;
46dbef6a 976
a01672d3 977 if (!memory_region_is_ram(mr)) {
235e8982
JJ
978 if (writeable || !kvm_readonly_mem_allowed) {
979 return;
980 } else if (!mr->romd_mode) {
981 /* If the memory device is not in romd_mode, then we actually want
982 * to remove the kvm memory slot so all accesses will trap. */
983 add = false;
984 }
9f213ed9
AK
985 }
986
5ea69c2e
DH
987 size = kvm_align_section(section, &start_addr);
988 if (!size) {
989 return;
990 }
991
bbfd3017 992 /* use aligned delta to align the ram address */
5ea69c2e 993 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region +
bbfd3017 994 (start_addr - section->offset_within_address_space);
a01672d3 995
36adac49
PX
996 kvm_slots_lock(kml);
997
f357f564 998 if (!add) {
90ed4bcc 999 mem = kvm_lookup_matching_slot(kml, start_addr, size);
46dbef6a 1000 if (!mem) {
36adac49 1001 goto out;
46dbef6a 1002 }
1bfbac4e 1003 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
7bbda04c 1004 kvm_physical_sync_dirty_bitmap(kml, section);
3fbffb62
AK
1005 }
1006
f357f564 1007 /* unregister the slot */
9f4bf4ba
PX
1008 g_free(mem->dirty_bmap);
1009 mem->dirty_bmap = NULL;
46dbef6a 1010 mem->memory_size = 0;
6c090d4a
SZ
1011 mem->flags = 0;
1012 err = kvm_set_user_memory_region(kml, mem, false);
46dbef6a 1013 if (err) {
1c4fdaba 1014 fprintf(stderr, "%s: error unregistering slot: %s\n",
46dbef6a
MT
1015 __func__, strerror(-err));
1016 abort();
1017 }
36adac49 1018 goto out;
46dbef6a
MT
1019 }
1020
f357f564 1021 /* register the new slot */
7bbda04c 1022 mem = kvm_alloc_slot(kml);
46dbef6a
MT
1023 mem->memory_size = size;
1024 mem->start_addr = start_addr;
9f213ed9 1025 mem->ram = ram;
d6ff5cbc 1026 mem->flags = kvm_mem_flags(mr);
46dbef6a 1027
6c090d4a 1028 err = kvm_set_user_memory_region(kml, mem, true);
46dbef6a
MT
1029 if (err) {
1030 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
1031 strerror(-err));
1032 abort();
1033 }
36adac49
PX
1034
1035out:
1036 kvm_slots_unlock(kml);
46dbef6a
MT
1037}
1038
a01672d3
AK
1039static void kvm_region_add(MemoryListener *listener,
1040 MemoryRegionSection *section)
1041{
7bbda04c
PB
1042 KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
1043
dfde4e6e 1044 memory_region_ref(section->mr);
7bbda04c 1045 kvm_set_phys_mem(kml, section, true);
a01672d3
AK
1046}
1047
1048static void kvm_region_del(MemoryListener *listener,
1049 MemoryRegionSection *section)
1050{
7bbda04c
PB
1051 KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
1052
1053 kvm_set_phys_mem(kml, section, false);
dfde4e6e 1054 memory_region_unref(section->mr);
a01672d3
AK
1055}
1056
1057static void kvm_log_sync(MemoryListener *listener,
1058 MemoryRegionSection *section)
7b8f3b78 1059{
7bbda04c 1060 KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
a01672d3
AK
1061 int r;
1062
36adac49 1063 kvm_slots_lock(kml);
7bbda04c 1064 r = kvm_physical_sync_dirty_bitmap(kml, section);
36adac49 1065 kvm_slots_unlock(kml);
a01672d3
AK
1066 if (r < 0) {
1067 abort();
1068 }
7b8f3b78
MT
1069}
1070
ff4aa114
PX
1071static void kvm_log_clear(MemoryListener *listener,
1072 MemoryRegionSection *section)
1073{
1074 KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
1075 int r;
1076
1077 r = kvm_physical_log_clear(kml, section);
1078 if (r < 0) {
1079 error_report_once("%s: kvm log clear failed: mr=%s "
1080 "offset=%"HWADDR_PRIx" size=%"PRIx64, __func__,
1081 section->mr->name, section->offset_within_region,
1082 int128_get64(section->size));
1083 abort();
1084 }
1085}
1086
d22b096e
AK
1087static void kvm_mem_ioeventfd_add(MemoryListener *listener,
1088 MemoryRegionSection *section,
1089 bool match_data, uint64_t data,
1090 EventNotifier *e)
1091{
1092 int fd = event_notifier_get_fd(e);
80a1ea37
AK
1093 int r;
1094
4b8f1c88 1095 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
052e87b0
PB
1096 data, true, int128_get64(section->size),
1097 match_data);
80a1ea37 1098 if (r < 0) {
e346bcbf
YK
1099 fprintf(stderr, "%s: error adding ioeventfd: %s (%d)\n",
1100 __func__, strerror(-r), -r);
80a1ea37
AK
1101 abort();
1102 }
1103}
1104
d22b096e
AK
1105static void kvm_mem_ioeventfd_del(MemoryListener *listener,
1106 MemoryRegionSection *section,
1107 bool match_data, uint64_t data,
1108 EventNotifier *e)
80a1ea37 1109{
d22b096e 1110 int fd = event_notifier_get_fd(e);
80a1ea37
AK
1111 int r;
1112
4b8f1c88 1113 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
052e87b0
PB
1114 data, false, int128_get64(section->size),
1115 match_data);
80a1ea37 1116 if (r < 0) {
e346bcbf
YK
1117 fprintf(stderr, "%s: error deleting ioeventfd: %s (%d)\n",
1118 __func__, strerror(-r), -r);
80a1ea37
AK
1119 abort();
1120 }
1121}
1122
d22b096e
AK
1123static void kvm_io_ioeventfd_add(MemoryListener *listener,
1124 MemoryRegionSection *section,
1125 bool match_data, uint64_t data,
1126 EventNotifier *e)
80a1ea37 1127{
d22b096e 1128 int fd = event_notifier_get_fd(e);
80a1ea37
AK
1129 int r;
1130
44c3f8f7 1131 r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space,
052e87b0
PB
1132 data, true, int128_get64(section->size),
1133 match_data);
80a1ea37 1134 if (r < 0) {
e346bcbf
YK
1135 fprintf(stderr, "%s: error adding ioeventfd: %s (%d)\n",
1136 __func__, strerror(-r), -r);
80a1ea37
AK
1137 abort();
1138 }
1139}
1140
d22b096e
AK
1141static void kvm_io_ioeventfd_del(MemoryListener *listener,
1142 MemoryRegionSection *section,
1143 bool match_data, uint64_t data,
1144 EventNotifier *e)
80a1ea37
AK
1145
1146{
d22b096e 1147 int fd = event_notifier_get_fd(e);
80a1ea37
AK
1148 int r;
1149
44c3f8f7 1150 r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space,
052e87b0
PB
1151 data, false, int128_get64(section->size),
1152 match_data);
80a1ea37 1153 if (r < 0) {
e346bcbf
YK
1154 fprintf(stderr, "%s: error deleting ioeventfd: %s (%d)\n",
1155 __func__, strerror(-r), -r);
80a1ea37
AK
1156 abort();
1157 }
1158}
1159
38bfe691
PB
1160void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
1161 AddressSpace *as, int as_id)
7bbda04c
PB
1162{
1163 int i;
1164
36adac49 1165 qemu_mutex_init(&kml->slots_lock);
7bbda04c 1166 kml->slots = g_malloc0(s->nr_slots * sizeof(KVMSlot));
38bfe691 1167 kml->as_id = as_id;
7bbda04c
PB
1168
1169 for (i = 0; i < s->nr_slots; i++) {
1170 kml->slots[i].slot = i;
1171 }
1172
1173 kml->listener.region_add = kvm_region_add;
1174 kml->listener.region_del = kvm_region_del;
1175 kml->listener.log_start = kvm_log_start;
1176 kml->listener.log_stop = kvm_log_stop;
1177 kml->listener.log_sync = kvm_log_sync;
ff4aa114 1178 kml->listener.log_clear = kvm_log_clear;
7bbda04c
PB
1179 kml->listener.priority = 10;
1180
1181 memory_listener_register(&kml->listener, as);
8072aae3
AK
1182
1183 for (i = 0; i < s->nr_as; ++i) {
1184 if (!s->as[i].as) {
1185 s->as[i].as = as;
1186 s->as[i].ml = kml;
1187 break;
1188 }
1189 }
7bbda04c 1190}
d22b096e
AK
1191
1192static MemoryListener kvm_io_listener = {
d22b096e
AK
1193 .eventfd_add = kvm_io_ioeventfd_add,
1194 .eventfd_del = kvm_io_ioeventfd_del,
72e22d2f 1195 .priority = 10,
7b8f3b78
MT
1196};
1197
3889c3fa 1198int kvm_set_irq(KVMState *s, int irq, int level)
84b058d7
JK
1199{
1200 struct kvm_irq_level event;
1201 int ret;
1202
7ae26bd4 1203 assert(kvm_async_interrupts_enabled());
84b058d7
JK
1204
1205 event.level = level;
1206 event.irq = irq;
e333cd69 1207 ret = kvm_vm_ioctl(s, s->irq_set_ioctl, &event);
84b058d7 1208 if (ret < 0) {
3889c3fa 1209 perror("kvm_set_irq");
84b058d7
JK
1210 abort();
1211 }
1212
e333cd69 1213 return (s->irq_set_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
84b058d7
JK
1214}
1215
1216#ifdef KVM_CAP_IRQ_ROUTING
d3d3bef0
JK
1217typedef struct KVMMSIRoute {
1218 struct kvm_irq_routing_entry kroute;
1219 QTAILQ_ENTRY(KVMMSIRoute) entry;
1220} KVMMSIRoute;
1221
84b058d7
JK
1222static void set_gsi(KVMState *s, unsigned int gsi)
1223{
8269fb70 1224 set_bit(gsi, s->used_gsi_bitmap);
84b058d7
JK
1225}
1226
04fa27f5
JK
1227static void clear_gsi(KVMState *s, unsigned int gsi)
1228{
8269fb70 1229 clear_bit(gsi, s->used_gsi_bitmap);
04fa27f5
JK
1230}
1231
7b774593 1232void kvm_init_irq_routing(KVMState *s)
84b058d7 1233{
04fa27f5 1234 int gsi_count, i;
84b058d7 1235
00008418 1236 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
84b058d7 1237 if (gsi_count > 0) {
84b058d7 1238 /* Round up so we can search ints using ffs */
8269fb70 1239 s->used_gsi_bitmap = bitmap_new(gsi_count);
4e2e4e63 1240 s->gsi_count = gsi_count;
84b058d7
JK
1241 }
1242
1243 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
1244 s->nr_allocated_irq_routes = 0;
1245
50bf31b9 1246 if (!kvm_direct_msi_allowed) {
4a3adebb
JK
1247 for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) {
1248 QTAILQ_INIT(&s->msi_hashtab[i]);
1249 }
04fa27f5
JK
1250 }
1251
84b058d7
JK
1252 kvm_arch_init_irq_routing(s);
1253}
1254
cb925cf9 1255void kvm_irqchip_commit_routes(KVMState *s)
e7b20308
JK
1256{
1257 int ret;
1258
7005f7f8
PX
1259 if (kvm_gsi_direct_mapping()) {
1260 return;
1261 }
1262
1263 if (!kvm_gsi_routing_enabled()) {
1264 return;
1265 }
1266
e7b20308 1267 s->irq_routes->flags = 0;
54a6c11b 1268 trace_kvm_irqchip_commit_routes();
e7b20308
JK
1269 ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
1270 assert(ret == 0);
1271}
1272
84b058d7
JK
1273static void kvm_add_routing_entry(KVMState *s,
1274 struct kvm_irq_routing_entry *entry)
1275{
1276 struct kvm_irq_routing_entry *new;
1277 int n, size;
1278
1279 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
1280 n = s->nr_allocated_irq_routes * 2;
1281 if (n < 64) {
1282 n = 64;
1283 }
1284 size = sizeof(struct kvm_irq_routing);
1285 size += n * sizeof(*new);
1286 s->irq_routes = g_realloc(s->irq_routes, size);
1287 s->nr_allocated_irq_routes = n;
1288 }
1289 n = s->irq_routes->nr++;
1290 new = &s->irq_routes->entries[n];
0fbc2074
MT
1291
1292 *new = *entry;
84b058d7
JK
1293
1294 set_gsi(s, entry->gsi);
1295}
1296
cc57407e
JK
1297static int kvm_update_routing_entry(KVMState *s,
1298 struct kvm_irq_routing_entry *new_entry)
1299{
1300 struct kvm_irq_routing_entry *entry;
1301 int n;
1302
1303 for (n = 0; n < s->irq_routes->nr; n++) {
1304 entry = &s->irq_routes->entries[n];
1305 if (entry->gsi != new_entry->gsi) {
1306 continue;
1307 }
1308
40509f7f
MT
1309 if(!memcmp(entry, new_entry, sizeof *entry)) {
1310 return 0;
1311 }
1312
0fbc2074 1313 *entry = *new_entry;
cc57407e 1314
cc57407e
JK
1315 return 0;
1316 }
1317
1318 return -ESRCH;
1319}
1320
1df186df 1321void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin)
84b058d7 1322{
0fbc2074 1323 struct kvm_irq_routing_entry e = {};
84b058d7 1324
4e2e4e63
JK
1325 assert(pin < s->gsi_count);
1326
84b058d7
JK
1327 e.gsi = irq;
1328 e.type = KVM_IRQ_ROUTING_IRQCHIP;
1329 e.flags = 0;
1330 e.u.irqchip.irqchip = irqchip;
1331 e.u.irqchip.pin = pin;
1332 kvm_add_routing_entry(s, &e);
1333}
1334
1e2aa8be 1335void kvm_irqchip_release_virq(KVMState *s, int virq)
04fa27f5
JK
1336{
1337 struct kvm_irq_routing_entry *e;
1338 int i;
1339
76fe21de
AK
1340 if (kvm_gsi_direct_mapping()) {
1341 return;
1342 }
1343
04fa27f5
JK
1344 for (i = 0; i < s->irq_routes->nr; i++) {
1345 e = &s->irq_routes->entries[i];
1346 if (e->gsi == virq) {
1347 s->irq_routes->nr--;
1348 *e = s->irq_routes->entries[s->irq_routes->nr];
1349 }
1350 }
1351 clear_gsi(s, virq);
38d87493 1352 kvm_arch_release_virq_post(virq);
9ba35d0b 1353 trace_kvm_irqchip_release_virq(virq);
04fa27f5
JK
1354}
1355
1356static unsigned int kvm_hash_msi(uint32_t data)
1357{
1358 /* This is optimized for IA32 MSI layout. However, no other arch shall
1359 * repeat the mistake of not providing a direct MSI injection API. */
1360 return data & 0xff;
1361}
1362
1363static void kvm_flush_dynamic_msi_routes(KVMState *s)
1364{
1365 KVMMSIRoute *route, *next;
1366 unsigned int hash;
1367
1368 for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) {
1369 QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) {
1370 kvm_irqchip_release_virq(s, route->kroute.gsi);
1371 QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry);
1372 g_free(route);
1373 }
1374 }
1375}
1376
1377static int kvm_irqchip_get_virq(KVMState *s)
1378{
8269fb70 1379 int next_virq;
04fa27f5 1380
bdf02631
WM
1381 /*
1382 * PIC and IOAPIC share the first 16 GSI numbers, thus the available
1383 * GSI numbers are more than the number of IRQ route. Allocating a GSI
1384 * number can succeed even though a new route entry cannot be added.
1385 * When this happens, flush dynamic MSI entries to free IRQ route entries.
1386 */
50bf31b9 1387 if (!kvm_direct_msi_allowed && s->irq_routes->nr == s->gsi_count) {
bdf02631
WM
1388 kvm_flush_dynamic_msi_routes(s);
1389 }
1390
04fa27f5 1391 /* Return the lowest unused GSI in the bitmap */
8269fb70
WY
1392 next_virq = find_first_zero_bit(s->used_gsi_bitmap, s->gsi_count);
1393 if (next_virq >= s->gsi_count) {
1394 return -ENOSPC;
1395 } else {
1396 return next_virq;
04fa27f5 1397 }
04fa27f5
JK
1398}
1399
1400static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
1401{
1402 unsigned int hash = kvm_hash_msi(msg.data);
1403 KVMMSIRoute *route;
1404
1405 QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) {
1406 if (route->kroute.u.msi.address_lo == (uint32_t)msg.address &&
1407 route->kroute.u.msi.address_hi == (msg.address >> 32) &&
d07cc1f1 1408 route->kroute.u.msi.data == le32_to_cpu(msg.data)) {
04fa27f5
JK
1409 return route;
1410 }
1411 }
1412 return NULL;
1413}
1414
1415int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1416{
4a3adebb 1417 struct kvm_msi msi;
04fa27f5
JK
1418 KVMMSIRoute *route;
1419
50bf31b9 1420 if (kvm_direct_msi_allowed) {
4a3adebb
JK
1421 msi.address_lo = (uint32_t)msg.address;
1422 msi.address_hi = msg.address >> 32;
d07cc1f1 1423 msi.data = le32_to_cpu(msg.data);
4a3adebb
JK
1424 msi.flags = 0;
1425 memset(msi.pad, 0, sizeof(msi.pad));
1426
1427 return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
1428 }
1429
04fa27f5
JK
1430 route = kvm_lookup_msi_route(s, msg);
1431 if (!route) {
e7b20308 1432 int virq;
04fa27f5
JK
1433
1434 virq = kvm_irqchip_get_virq(s);
1435 if (virq < 0) {
1436 return virq;
1437 }
1438
0fbc2074 1439 route = g_malloc0(sizeof(KVMMSIRoute));
04fa27f5
JK
1440 route->kroute.gsi = virq;
1441 route->kroute.type = KVM_IRQ_ROUTING_MSI;
1442 route->kroute.flags = 0;
1443 route->kroute.u.msi.address_lo = (uint32_t)msg.address;
1444 route->kroute.u.msi.address_hi = msg.address >> 32;
d07cc1f1 1445 route->kroute.u.msi.data = le32_to_cpu(msg.data);
04fa27f5
JK
1446
1447 kvm_add_routing_entry(s, &route->kroute);
cb925cf9 1448 kvm_irqchip_commit_routes(s);
04fa27f5
JK
1449
1450 QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route,
1451 entry);
04fa27f5
JK
1452 }
1453
1454 assert(route->kroute.type == KVM_IRQ_ROUTING_MSI);
1455
3889c3fa 1456 return kvm_set_irq(s, route->kroute.gsi, 1);
04fa27f5
JK
1457}
1458
d1f6af6a 1459int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev)
92b4e489 1460{
0fbc2074 1461 struct kvm_irq_routing_entry kroute = {};
92b4e489 1462 int virq;
d1f6af6a
PX
1463 MSIMessage msg = {0, 0};
1464
88c725c7 1465 if (pci_available && dev) {
e1d4fb2d 1466 msg = pci_get_msi_message(dev, vector);
d1f6af6a 1467 }
92b4e489 1468
76fe21de 1469 if (kvm_gsi_direct_mapping()) {
1850b6b7 1470 return kvm_arch_msi_data_to_gsi(msg.data);
76fe21de
AK
1471 }
1472
f3e1bed8 1473 if (!kvm_gsi_routing_enabled()) {
92b4e489
JK
1474 return -ENOSYS;
1475 }
1476
1477 virq = kvm_irqchip_get_virq(s);
1478 if (virq < 0) {
1479 return virq;
1480 }
1481
1482 kroute.gsi = virq;
1483 kroute.type = KVM_IRQ_ROUTING_MSI;
1484 kroute.flags = 0;
1485 kroute.u.msi.address_lo = (uint32_t)msg.address;
1486 kroute.u.msi.address_hi = msg.address >> 32;
d07cc1f1 1487 kroute.u.msi.data = le32_to_cpu(msg.data);
88c725c7 1488 if (pci_available && kvm_msi_devid_required()) {
767a554a
PF
1489 kroute.flags = KVM_MSI_VALID_DEVID;
1490 kroute.u.msi.devid = pci_requester_id(dev);
1491 }
dc9f06ca 1492 if (kvm_arch_fixup_msi_route(&kroute, msg.address, msg.data, dev)) {
9e03a040
FB
1493 kvm_irqchip_release_virq(s, virq);
1494 return -EINVAL;
1495 }
92b4e489 1496
9ba35d0b
PX
1497 trace_kvm_irqchip_add_msi_route(dev ? dev->name : (char *)"N/A",
1498 vector, virq);
54a6c11b 1499
92b4e489 1500 kvm_add_routing_entry(s, &kroute);
38d87493 1501 kvm_arch_add_msi_route_post(&kroute, vector, dev);
cb925cf9 1502 kvm_irqchip_commit_routes(s);
92b4e489
JK
1503
1504 return virq;
1505}
1506
dc9f06ca
PF
1507int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg,
1508 PCIDevice *dev)
cc57407e 1509{
0fbc2074 1510 struct kvm_irq_routing_entry kroute = {};
cc57407e 1511
76fe21de
AK
1512 if (kvm_gsi_direct_mapping()) {
1513 return 0;
1514 }
1515
cc57407e
JK
1516 if (!kvm_irqchip_in_kernel()) {
1517 return -ENOSYS;
1518 }
1519
1520 kroute.gsi = virq;
1521 kroute.type = KVM_IRQ_ROUTING_MSI;
1522 kroute.flags = 0;
1523 kroute.u.msi.address_lo = (uint32_t)msg.address;
1524 kroute.u.msi.address_hi = msg.address >> 32;
d07cc1f1 1525 kroute.u.msi.data = le32_to_cpu(msg.data);
88c725c7 1526 if (pci_available && kvm_msi_devid_required()) {
767a554a
PF
1527 kroute.flags = KVM_MSI_VALID_DEVID;
1528 kroute.u.msi.devid = pci_requester_id(dev);
1529 }
dc9f06ca 1530 if (kvm_arch_fixup_msi_route(&kroute, msg.address, msg.data, dev)) {
9e03a040
FB
1531 return -EINVAL;
1532 }
cc57407e 1533
54a6c11b
PX
1534 trace_kvm_irqchip_update_msi_route(virq);
1535
cc57407e
JK
1536 return kvm_update_routing_entry(s, &kroute);
1537}
1538
ca916d37
VM
1539static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int rfd, int virq,
1540 bool assign)
39853bbc
JK
1541{
1542 struct kvm_irqfd irqfd = {
1543 .fd = fd,
1544 .gsi = virq,
1545 .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN,
1546 };
1547
ca916d37
VM
1548 if (rfd != -1) {
1549 irqfd.flags |= KVM_IRQFD_FLAG_RESAMPLE;
1550 irqfd.resamplefd = rfd;
1551 }
1552
cc7e0ddf 1553 if (!kvm_irqfds_enabled()) {
39853bbc
JK
1554 return -ENOSYS;
1555 }
1556
1557 return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd);
1558}
1559
d426d9fb
CH
1560int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter)
1561{
e9af2fef 1562 struct kvm_irq_routing_entry kroute = {};
d426d9fb
CH
1563 int virq;
1564
1565 if (!kvm_gsi_routing_enabled()) {
1566 return -ENOSYS;
1567 }
1568
1569 virq = kvm_irqchip_get_virq(s);
1570 if (virq < 0) {
1571 return virq;
1572 }
1573
1574 kroute.gsi = virq;
1575 kroute.type = KVM_IRQ_ROUTING_S390_ADAPTER;
1576 kroute.flags = 0;
1577 kroute.u.adapter.summary_addr = adapter->summary_addr;
1578 kroute.u.adapter.ind_addr = adapter->ind_addr;
1579 kroute.u.adapter.summary_offset = adapter->summary_offset;
1580 kroute.u.adapter.ind_offset = adapter->ind_offset;
1581 kroute.u.adapter.adapter_id = adapter->adapter_id;
1582
1583 kvm_add_routing_entry(s, &kroute);
d426d9fb
CH
1584
1585 return virq;
1586}
1587
977a8d9c
AS
1588int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint)
1589{
1590 struct kvm_irq_routing_entry kroute = {};
1591 int virq;
1592
1593 if (!kvm_gsi_routing_enabled()) {
1594 return -ENOSYS;
1595 }
1596 if (!kvm_check_extension(s, KVM_CAP_HYPERV_SYNIC)) {
1597 return -ENOSYS;
1598 }
1599 virq = kvm_irqchip_get_virq(s);
1600 if (virq < 0) {
1601 return virq;
1602 }
1603
1604 kroute.gsi = virq;
1605 kroute.type = KVM_IRQ_ROUTING_HV_SINT;
1606 kroute.flags = 0;
1607 kroute.u.hv_sint.vcpu = vcpu;
1608 kroute.u.hv_sint.sint = sint;
1609
1610 kvm_add_routing_entry(s, &kroute);
1611 kvm_irqchip_commit_routes(s);
1612
1613 return virq;
1614}
1615
84b058d7
JK
1616#else /* !KVM_CAP_IRQ_ROUTING */
1617
7b774593 1618void kvm_init_irq_routing(KVMState *s)
84b058d7
JK
1619{
1620}
04fa27f5 1621
d3d3bef0
JK
1622void kvm_irqchip_release_virq(KVMState *s, int virq)
1623{
1624}
1625
04fa27f5
JK
1626int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1627{
1628 abort();
1629}
92b4e489 1630
d1f6af6a 1631int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev)
92b4e489 1632{
df410675 1633 return -ENOSYS;
92b4e489 1634}
39853bbc 1635
d426d9fb
CH
1636int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter)
1637{
1638 return -ENOSYS;
1639}
1640
977a8d9c
AS
1641int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint)
1642{
1643 return -ENOSYS;
1644}
1645
39853bbc
JK
1646static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1647{
1648 abort();
1649}
dabe3143
MT
1650
1651int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg)
1652{
1653 return -ENOSYS;
1654}
84b058d7
JK
1655#endif /* !KVM_CAP_IRQ_ROUTING */
1656
1c9b71a7
EA
1657int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
1658 EventNotifier *rn, int virq)
39853bbc 1659{
ca916d37
VM
1660 return kvm_irqchip_assign_irqfd(s, event_notifier_get_fd(n),
1661 rn ? event_notifier_get_fd(rn) : -1, virq, true);
39853bbc
JK
1662}
1663
1c9b71a7
EA
1664int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
1665 int virq)
15b2bd18 1666{
ca916d37
VM
1667 return kvm_irqchip_assign_irqfd(s, event_notifier_get_fd(n), -1, virq,
1668 false);
15b2bd18
PB
1669}
1670
197e3524
EA
1671int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n,
1672 EventNotifier *rn, qemu_irq irq)
1673{
1674 gpointer key, gsi;
1675 gboolean found = g_hash_table_lookup_extended(s->gsimap, irq, &key, &gsi);
1676
1677 if (!found) {
1678 return -ENXIO;
1679 }
1680 return kvm_irqchip_add_irqfd_notifier_gsi(s, n, rn, GPOINTER_TO_INT(gsi));
1681}
1682
1683int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n,
1684 qemu_irq irq)
1685{
1686 gpointer key, gsi;
1687 gboolean found = g_hash_table_lookup_extended(s->gsimap, irq, &key, &gsi);
1688
1689 if (!found) {
1690 return -ENXIO;
1691 }
1692 return kvm_irqchip_remove_irqfd_notifier_gsi(s, n, GPOINTER_TO_INT(gsi));
1693}
1694
1695void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
1696{
1697 g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
1698}
1699
8db4936b 1700static void kvm_irqchip_create(MachineState *machine, KVMState *s)
84b058d7 1701{
84b058d7
JK
1702 int ret;
1703
8db4936b
PB
1704 if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
1705 ;
1706 } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
1707 ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
1708 if (ret < 0) {
1709 fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
1710 exit(1);
1711 }
1712 } else {
1713 return;
84b058d7
JK
1714 }
1715
d6032e06
CD
1716 /* First probe and see if there's a arch-specific hook to create the
1717 * in-kernel irqchip for us */
15eafc2e 1718 ret = kvm_arch_irqchip_create(machine, s);
8db4936b 1719 if (ret == 0) {
15eafc2e
PB
1720 if (machine_kernel_irqchip_split(machine)) {
1721 perror("Split IRQ chip mode not supported.");
1722 exit(1);
1723 } else {
1724 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
1725 }
8db4936b
PB
1726 }
1727 if (ret < 0) {
1728 fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret));
1729 exit(1);
84b058d7
JK
1730 }
1731
3d4b2649 1732 kvm_kernel_irqchip = true;
7ae26bd4
PM
1733 /* If we have an in-kernel IRQ chip then we must have asynchronous
1734 * interrupt delivery (though the reverse is not necessarily true)
1735 */
1736 kvm_async_interrupts_allowed = true;
215e79c0 1737 kvm_halt_in_kernel_allowed = true;
84b058d7
JK
1738
1739 kvm_init_irq_routing(s);
1740
197e3524 1741 s->gsimap = g_hash_table_new(g_direct_hash, g_direct_equal);
84b058d7
JK
1742}
1743
670436ce
AJ
1744/* Find number of supported CPUs using the recommended
1745 * procedure from the kernel API documentation to cope with
1746 * older kernels that may be missing capabilities.
1747 */
1748static int kvm_recommended_vcpus(KVMState *s)
3ed444e9 1749{
11748ba7 1750 int ret = kvm_vm_check_extension(s, KVM_CAP_NR_VCPUS);
670436ce
AJ
1751 return (ret) ? ret : 4;
1752}
3ed444e9 1753
670436ce
AJ
1754static int kvm_max_vcpus(KVMState *s)
1755{
1756 int ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
1757 return (ret) ? ret : kvm_recommended_vcpus(s);
3ed444e9
DH
1758}
1759
f31e3266
GK
1760static int kvm_max_vcpu_id(KVMState *s)
1761{
1762 int ret = kvm_check_extension(s, KVM_CAP_MAX_VCPU_ID);
1763 return (ret) ? ret : kvm_max_vcpus(s);
1764}
1765
41264b38
GK
1766bool kvm_vcpu_id_is_valid(int vcpu_id)
1767{
1768 KVMState *s = KVM_STATE(current_machine->accelerator);
f31e3266 1769 return vcpu_id >= 0 && vcpu_id < kvm_max_vcpu_id(s);
41264b38
GK
1770}
1771
f6a1ef64 1772static int kvm_init(MachineState *ms)
05330448 1773{
f6a1ef64 1774 MachineClass *mc = MACHINE_GET_CLASS(ms);
168ccc11
JK
1775 static const char upgrade_note[] =
1776 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1777 "(see http://sourceforge.net/projects/kvm).\n";
670436ce
AJ
1778 struct {
1779 const char *name;
1780 int num;
1781 } num_cpus[] = {
5cc8767d
LX
1782 { "SMP", ms->smp.cpus },
1783 { "hotpluggable", ms->smp.max_cpus },
670436ce
AJ
1784 { NULL, }
1785 }, *nc = num_cpus;
1786 int soft_vcpus_limit, hard_vcpus_limit;
05330448 1787 KVMState *s;
94a8d39a 1788 const KVMCapabilityInfo *missing_cap;
05330448 1789 int ret;
7bbda04c 1790 int type = 0;
135a129a 1791 const char *kvm_type;
05330448 1792
fc02086b 1793 s = KVM_STATE(ms->accelerator);
05330448 1794
3145fcb6
DG
1795 /*
1796 * On systems where the kernel can support different base page
1797 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1798 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1799 * page size for the system though.
1800 */
1801 assert(TARGET_PAGE_SIZE <= getpagesize());
1802
aed6efb9
JH
1803 s->sigmask_len = 8;
1804
e22a25c9 1805#ifdef KVM_CAP_SET_GUEST_DEBUG
72cf2d4f 1806 QTAILQ_INIT(&s->kvm_sw_breakpoints);
e22a25c9 1807#endif
4c055ab5 1808 QLIST_INIT(&s->kvm_parked_vcpus);
05330448 1809 s->vmfd = -1;
40ff6d7e 1810 s->fd = qemu_open("/dev/kvm", O_RDWR);
05330448
AL
1811 if (s->fd == -1) {
1812 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1813 ret = -errno;
1814 goto err;
1815 }
1816
1817 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1818 if (ret < KVM_API_VERSION) {
0e1dac6c 1819 if (ret >= 0) {
05330448 1820 ret = -EINVAL;
a426e122 1821 }
05330448
AL
1822 fprintf(stderr, "kvm version too old\n");
1823 goto err;
1824 }
1825
1826 if (ret > KVM_API_VERSION) {
1827 ret = -EINVAL;
1828 fprintf(stderr, "kvm version not supported\n");
1829 goto err;
1830 }
1831
cf0f7cf9 1832 kvm_immediate_exit = kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT);
fb541ca5
AW
1833 s->nr_slots = kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1834
1835 /* If unspecified, use the default value */
1836 if (!s->nr_slots) {
1837 s->nr_slots = 32;
1838 }
1839
8072aae3
AK
1840 s->nr_as = kvm_check_extension(s, KVM_CAP_MULTI_ADDRESS_SPACE);
1841 if (s->nr_as <= 1) {
1842 s->nr_as = 1;
1843 }
1844 s->as = g_new0(struct KVMAs, s->nr_as);
1845
135a129a 1846 kvm_type = qemu_opt_get(qemu_get_machine_opts(), "kvm-type");
f1e29879 1847 if (mc->kvm_type) {
dc0ca80e 1848 type = mc->kvm_type(ms, kvm_type);
135a129a 1849 } else if (kvm_type) {
0e1dac6c 1850 ret = -EINVAL;
135a129a
AK
1851 fprintf(stderr, "Invalid argument kvm-type=%s\n", kvm_type);
1852 goto err;
1853 }
1854
94ccff13 1855 do {
135a129a 1856 ret = kvm_ioctl(s, KVM_CREATE_VM, type);
94ccff13
TK
1857 } while (ret == -EINTR);
1858
1859 if (ret < 0) {
521f438e 1860 fprintf(stderr, "ioctl(KVM_CREATE_VM) failed: %d %s\n", -ret,
94ccff13
TK
1861 strerror(-ret));
1862
0104dcac 1863#ifdef TARGET_S390X
2c80e996
CH
1864 if (ret == -EINVAL) {
1865 fprintf(stderr,
1866 "Host kernel setup problem detected. Please verify:\n");
1867 fprintf(stderr, "- for kernels supporting the switch_amode or"
1868 " user_mode parameters, whether\n");
1869 fprintf(stderr,
1870 " user space is running in primary address space\n");
1871 fprintf(stderr,
1872 "- for kernels supporting the vm.allocate_pgste sysctl, "
1873 "whether it is enabled\n");
1874 }
0104dcac 1875#endif
05330448 1876 goto err;
0104dcac 1877 }
05330448 1878
94ccff13 1879 s->vmfd = ret;
11748ba7
GK
1880
1881 /* check the vcpu limits */
1882 soft_vcpus_limit = kvm_recommended_vcpus(s);
1883 hard_vcpus_limit = kvm_max_vcpus(s);
1884
1885 while (nc->name) {
1886 if (nc->num > soft_vcpus_limit) {
1887 warn_report("Number of %s cpus requested (%d) exceeds "
1888 "the recommended cpus supported by KVM (%d)",
1889 nc->name, nc->num, soft_vcpus_limit);
1890
1891 if (nc->num > hard_vcpus_limit) {
1892 fprintf(stderr, "Number of %s cpus requested (%d) exceeds "
1893 "the maximum cpus supported by KVM (%d)\n",
1894 nc->name, nc->num, hard_vcpus_limit);
1895 exit(1);
1896 }
1897 }
1898 nc++;
1899 }
1900
94a8d39a
JK
1901 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1902 if (!missing_cap) {
1903 missing_cap =
1904 kvm_check_extension_list(s, kvm_arch_required_capabilities);
05330448 1905 }
94a8d39a 1906 if (missing_cap) {
ad7b8b33 1907 ret = -EINVAL;
94a8d39a
JK
1908 fprintf(stderr, "kvm does not support %s\n%s",
1909 missing_cap->name, upgrade_note);
d85dc283
AL
1910 goto err;
1911 }
1912
ad7b8b33 1913 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
e6d34aee
PH
1914 s->coalesced_pio = s->coalesced_mmio &&
1915 kvm_check_extension(s, KVM_CAP_COALESCED_PIO);
f65ed4c1 1916
ff4aa114
PX
1917 s->manual_dirty_log_protect =
1918 kvm_check_extension(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
1919 if (s->manual_dirty_log_protect) {
1920 ret = kvm_vm_enable_cap(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, 0, 1);
1921 if (ret) {
1922 warn_report("Trying to enable KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 "
1923 "but failed. Falling back to the legacy mode. ");
1924 s->manual_dirty_log_protect = false;
1925 }
1926 }
1927
a0fb002c
JK
1928#ifdef KVM_CAP_VCPU_EVENTS
1929 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1930#endif
1931
b0b1d690
JK
1932 s->robust_singlestep =
1933 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
b0b1d690 1934
ff44f1a3
JK
1935#ifdef KVM_CAP_DEBUGREGS
1936 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1937#endif
1938
ebbfef2f
LA
1939 s->max_nested_state_len = kvm_check_extension(s, KVM_CAP_NESTED_STATE);
1940
d3d3bef0 1941#ifdef KVM_CAP_IRQ_ROUTING
50bf31b9 1942 kvm_direct_msi_allowed = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);
d3d3bef0 1943#endif
4a3adebb 1944
3ab73842
JK
1945 s->intx_set_mask = kvm_check_extension(s, KVM_CAP_PCI_2_3);
1946
e333cd69 1947 s->irq_set_ioctl = KVM_IRQ_LINE;
8732fbd2 1948 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
e333cd69 1949 s->irq_set_ioctl = KVM_IRQ_LINE_STATUS;
8732fbd2
PM
1950 }
1951
df9c8b75
JJ
1952 kvm_readonly_mem_allowed =
1953 (kvm_check_extension(s, KVM_CAP_READONLY_MEM) > 0);
df9c8b75 1954
69e03ae6
NN
1955 kvm_eventfds_allowed =
1956 (kvm_check_extension(s, KVM_CAP_IOEVENTFD) > 0);
1957
f41389ae
EA
1958 kvm_irqfds_allowed =
1959 (kvm_check_extension(s, KVM_CAP_IRQFD) > 0);
1960
1961 kvm_resamplefds_allowed =
1962 (kvm_check_extension(s, KVM_CAP_IRQFD_RESAMPLE) > 0);
1963
d0a073a1
DD
1964 kvm_vm_attributes_allowed =
1965 (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES) > 0);
1966
35108223
JW
1967 kvm_ioeventfd_any_length_allowed =
1968 (kvm_check_extension(s, KVM_CAP_IOEVENTFD_ANY_LENGTH) > 0);
1969
d870cfde
GA
1970 kvm_state = s;
1971
b20e3780
BS
1972 /*
1973 * if memory encryption object is specified then initialize the memory
1974 * encryption context.
1975 */
1976 if (ms->memory_encryption) {
1977 kvm_state->memcrypt_handle = sev_guest_init(ms->memory_encryption);
1978 if (!kvm_state->memcrypt_handle) {
1979 ret = -1;
1980 goto err;
1981 }
54e89539
BS
1982
1983 kvm_state->memcrypt_encrypt_data = sev_encrypt_data;
b20e3780
BS
1984 }
1985
b16565b3 1986 ret = kvm_arch_init(ms, s);
a426e122 1987 if (ret < 0) {
05330448 1988 goto err;
a426e122 1989 }
05330448 1990
8db4936b
PB
1991 if (machine_kernel_irqchip_allowed(ms)) {
1992 kvm_irqchip_create(ms, s);
84b058d7
JK
1993 }
1994
8c56c1a5
PF
1995 if (kvm_eventfds_allowed) {
1996 s->memory_listener.listener.eventfd_add = kvm_mem_ioeventfd_add;
1997 s->memory_listener.listener.eventfd_del = kvm_mem_ioeventfd_del;
1998 }
e6d34aee
PH
1999 s->memory_listener.listener.coalesced_io_add = kvm_coalesce_mmio_region;
2000 s->memory_listener.listener.coalesced_io_del = kvm_uncoalesce_mmio_region;
7bbda04c
PB
2001
2002 kvm_memory_listener_register(s, &s->memory_listener,
38bfe691 2003 &address_space_memory, 0);
7bbda04c
PB
2004 memory_listener_register(&kvm_io_listener,
2005 &address_space_io);
e6d34aee
PH
2006 memory_listener_register(&kvm_coalesced_pio_listener,
2007 &address_space_io);
05330448 2008
d2f2b8a7
SH
2009 s->many_ioeventfds = kvm_check_many_ioeventfds();
2010
62dd4eda 2011 s->sync_mmu = !!kvm_vm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
f5948942
AW
2012 if (!s->sync_mmu) {
2013 qemu_balloon_inhibit(true);
2014 }
62dd4eda 2015
05330448
AL
2016 return 0;
2017
2018err:
0e1dac6c 2019 assert(ret < 0);
6d1cc321
SW
2020 if (s->vmfd >= 0) {
2021 close(s->vmfd);
2022 }
2023 if (s->fd != -1) {
2024 close(s->fd);
05330448 2025 }
7bbda04c 2026 g_free(s->memory_listener.slots);
05330448
AL
2027
2028 return ret;
2029}
2030
aed6efb9
JH
2031void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len)
2032{
2033 s->sigmask_len = sigmask_len;
2034}
2035
4c663752
PB
2036static void kvm_handle_io(uint16_t port, MemTxAttrs attrs, void *data, int direction,
2037 int size, uint32_t count)
05330448
AL
2038{
2039 int i;
2040 uint8_t *ptr = data;
2041
2042 for (i = 0; i < count; i++) {
4c663752 2043 address_space_rw(&address_space_io, port, attrs,
5c9eb028 2044 ptr, size,
354678c5 2045 direction == KVM_EXIT_IO_OUT);
05330448
AL
2046 ptr += size;
2047 }
05330448
AL
2048}
2049
5326ab55 2050static int kvm_handle_internal_error(CPUState *cpu, struct kvm_run *run)
7c80eef8 2051{
977c7b6d
RK
2052 fprintf(stderr, "KVM internal error. Suberror: %d\n",
2053 run->internal.suberror);
2054
7c80eef8
MT
2055 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
2056 int i;
2057
7c80eef8
MT
2058 for (i = 0; i < run->internal.ndata; ++i) {
2059 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
2060 i, (uint64_t)run->internal.data[i]);
2061 }
2062 }
7c80eef8
MT
2063 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
2064 fprintf(stderr, "emulation failure\n");
20d695a9 2065 if (!kvm_arch_stop_on_emulation_error(cpu)) {
90c84c56 2066 cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
d73cd8f4 2067 return EXCP_INTERRUPT;
a426e122 2068 }
7c80eef8
MT
2069 }
2070 /* FIXME: Should trigger a qmp message to let management know
2071 * something went wrong.
2072 */
73aaec4a 2073 return -1;
7c80eef8 2074}
7c80eef8 2075
62a2744c 2076void kvm_flush_coalesced_mmio_buffer(void)
f65ed4c1 2077{
f65ed4c1 2078 KVMState *s = kvm_state;
1cae88b9
AK
2079
2080 if (s->coalesced_flush_in_progress) {
2081 return;
2082 }
2083
2084 s->coalesced_flush_in_progress = true;
2085
62a2744c
SY
2086 if (s->coalesced_mmio_ring) {
2087 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
f65ed4c1
AL
2088 while (ring->first != ring->last) {
2089 struct kvm_coalesced_mmio *ent;
2090
2091 ent = &ring->coalesced_mmio[ring->first];
2092
e6d34aee
PH
2093 if (ent->pio == 1) {
2094 address_space_rw(&address_space_io, ent->phys_addr,
2095 MEMTXATTRS_UNSPECIFIED, ent->data,
2096 ent->len, true);
2097 } else {
2098 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
2099 }
85199474 2100 smp_wmb();
f65ed4c1
AL
2101 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
2102 }
2103 }
1cae88b9
AK
2104
2105 s->coalesced_flush_in_progress = false;
f65ed4c1
AL
2106}
2107
14e6fe12 2108static void do_kvm_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
4c0960c0 2109{
99f31832 2110 if (!cpu->vcpu_dirty) {
20d695a9 2111 kvm_arch_get_registers(cpu);
99f31832 2112 cpu->vcpu_dirty = true;
4c0960c0
AK
2113 }
2114}
2115
dd1750d7 2116void kvm_cpu_synchronize_state(CPUState *cpu)
2705d56a 2117{
99f31832 2118 if (!cpu->vcpu_dirty) {
14e6fe12 2119 run_on_cpu(cpu, do_kvm_cpu_synchronize_state, RUN_ON_CPU_NULL);
a426e122 2120 }
2705d56a
JK
2121}
2122
14e6fe12 2123static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
ea375f9a 2124{
20d695a9 2125 kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE);
99f31832 2126 cpu->vcpu_dirty = false;
ea375f9a
JK
2127}
2128
c8e2085d
DH
2129void kvm_cpu_synchronize_post_reset(CPUState *cpu)
2130{
14e6fe12 2131 run_on_cpu(cpu, do_kvm_cpu_synchronize_post_reset, RUN_ON_CPU_NULL);
c8e2085d
DH
2132}
2133
14e6fe12 2134static void do_kvm_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
ea375f9a 2135{
20d695a9 2136 kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE);
99f31832 2137 cpu->vcpu_dirty = false;
ea375f9a
JK
2138}
2139
c8e2085d
DH
2140void kvm_cpu_synchronize_post_init(CPUState *cpu)
2141{
14e6fe12 2142 run_on_cpu(cpu, do_kvm_cpu_synchronize_post_init, RUN_ON_CPU_NULL);
c8e2085d
DH
2143}
2144
75e972da
DG
2145static void do_kvm_cpu_synchronize_pre_loadvm(CPUState *cpu, run_on_cpu_data arg)
2146{
99f31832 2147 cpu->vcpu_dirty = true;
75e972da
DG
2148}
2149
2150void kvm_cpu_synchronize_pre_loadvm(CPUState *cpu)
2151{
2152 run_on_cpu(cpu, do_kvm_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL);
2153}
2154
2ae41db2
PB
2155#ifdef KVM_HAVE_MCE_INJECTION
2156static __thread void *pending_sigbus_addr;
2157static __thread int pending_sigbus_code;
2158static __thread bool have_sigbus_pending;
2159#endif
2160
cf0f7cf9
PB
2161static void kvm_cpu_kick(CPUState *cpu)
2162{
2163 atomic_set(&cpu->kvm_run->immediate_exit, 1);
2164}
2165
2166static void kvm_cpu_kick_self(void)
2167{
2168 if (kvm_immediate_exit) {
2169 kvm_cpu_kick(current_cpu);
2170 } else {
2171 qemu_cpu_kick_self();
2172 }
2173}
2174
18268b60
PB
2175static void kvm_eat_signals(CPUState *cpu)
2176{
2177 struct timespec ts = { 0, 0 };
2178 siginfo_t siginfo;
2179 sigset_t waitset;
2180 sigset_t chkset;
2181 int r;
2182
cf0f7cf9
PB
2183 if (kvm_immediate_exit) {
2184 atomic_set(&cpu->kvm_run->immediate_exit, 0);
2185 /* Write kvm_run->immediate_exit before the cpu->exit_request
2186 * write in kvm_cpu_exec.
2187 */
2188 smp_wmb();
2189 return;
2190 }
2191
18268b60
PB
2192 sigemptyset(&waitset);
2193 sigaddset(&waitset, SIG_IPI);
2194
2195 do {
2196 r = sigtimedwait(&waitset, &siginfo, &ts);
2197 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
2198 perror("sigtimedwait");
2199 exit(1);
2200 }
2201
2202 r = sigpending(&chkset);
2203 if (r == -1) {
2204 perror("sigpending");
2205 exit(1);
2206 }
2207 } while (sigismember(&chkset, SIG_IPI));
2208}
2209
1458c363 2210int kvm_cpu_exec(CPUState *cpu)
05330448 2211{
f7575c96 2212 struct kvm_run *run = cpu->kvm_run;
7cbb533f 2213 int ret, run_ret;
05330448 2214
8c0d577e 2215 DPRINTF("kvm_cpu_exec()\n");
05330448 2216
20d695a9 2217 if (kvm_arch_process_async_events(cpu)) {
c5c6679d 2218 atomic_set(&cpu->exit_request, 0);
6792a57b 2219 return EXCP_HLT;
9ccfac9e 2220 }
0af691d7 2221
4b8523ee 2222 qemu_mutex_unlock_iothread();
1d78a3c3 2223 cpu_exec_start(cpu);
4b8523ee 2224
9ccfac9e 2225 do {
4c663752
PB
2226 MemTxAttrs attrs;
2227
99f31832 2228 if (cpu->vcpu_dirty) {
20d695a9 2229 kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE);
99f31832 2230 cpu->vcpu_dirty = false;
4c0960c0
AK
2231 }
2232
20d695a9 2233 kvm_arch_pre_run(cpu, run);
c5c6679d 2234 if (atomic_read(&cpu->exit_request)) {
9ccfac9e
JK
2235 DPRINTF("interrupt exit requested\n");
2236 /*
2237 * KVM requires us to reenter the kernel after IO exits to complete
2238 * instruction emulation. This self-signal will ensure that we
2239 * leave ASAP again.
2240 */
cf0f7cf9 2241 kvm_cpu_kick_self();
9ccfac9e 2242 }
9ccfac9e 2243
cf0f7cf9
PB
2244 /* Read cpu->exit_request before KVM_RUN reads run->immediate_exit.
2245 * Matching barrier in kvm_eat_signals.
2246 */
2247 smp_rmb();
2248
1bc22652 2249 run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
9ccfac9e 2250
4c663752 2251 attrs = kvm_arch_post_run(cpu, run);
05330448 2252
2ae41db2
PB
2253#ifdef KVM_HAVE_MCE_INJECTION
2254 if (unlikely(have_sigbus_pending)) {
2255 qemu_mutex_lock_iothread();
2256 kvm_arch_on_sigbus_vcpu(cpu, pending_sigbus_code,
2257 pending_sigbus_addr);
2258 have_sigbus_pending = false;
2259 qemu_mutex_unlock_iothread();
2260 }
2261#endif
2262
7cbb533f 2263 if (run_ret < 0) {
dc77d341
JK
2264 if (run_ret == -EINTR || run_ret == -EAGAIN) {
2265 DPRINTF("io window exit\n");
18268b60 2266 kvm_eat_signals(cpu);
d73cd8f4 2267 ret = EXCP_INTERRUPT;
dc77d341
JK
2268 break;
2269 }
7b011fbc
ME
2270 fprintf(stderr, "error: kvm run failed %s\n",
2271 strerror(-run_ret));
dae02ba5
LV
2272#ifdef TARGET_PPC
2273 if (run_ret == -EBUSY) {
2274 fprintf(stderr,
2275 "This is probably because your SMT is enabled.\n"
2276 "VCPU can only run on primary threads with all "
2277 "secondary threads offline.\n");
2278 }
2279#endif
a85e130e
PB
2280 ret = -1;
2281 break;
05330448
AL
2282 }
2283
b76ac80a 2284 trace_kvm_run_exit(cpu->cpu_index, run->exit_reason);
05330448
AL
2285 switch (run->exit_reason) {
2286 case KVM_EXIT_IO:
8c0d577e 2287 DPRINTF("handle_io\n");
80b7d2ef 2288 /* Called outside BQL */
4c663752 2289 kvm_handle_io(run->io.port, attrs,
b30e93e9
JK
2290 (uint8_t *)run + run->io.data_offset,
2291 run->io.direction,
2292 run->io.size,
2293 run->io.count);
d73cd8f4 2294 ret = 0;
05330448
AL
2295 break;
2296 case KVM_EXIT_MMIO:
8c0d577e 2297 DPRINTF("handle_mmio\n");
de7ea885 2298 /* Called outside BQL */
4c663752
PB
2299 address_space_rw(&address_space_memory,
2300 run->mmio.phys_addr, attrs,
2301 run->mmio.data,
2302 run->mmio.len,
2303 run->mmio.is_write);
d73cd8f4 2304 ret = 0;
05330448
AL
2305 break;
2306 case KVM_EXIT_IRQ_WINDOW_OPEN:
8c0d577e 2307 DPRINTF("irq_window_open\n");
d73cd8f4 2308 ret = EXCP_INTERRUPT;
05330448
AL
2309 break;
2310 case KVM_EXIT_SHUTDOWN:
8c0d577e 2311 DPRINTF("shutdown\n");
cf83f140 2312 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
d73cd8f4 2313 ret = EXCP_INTERRUPT;
05330448
AL
2314 break;
2315 case KVM_EXIT_UNKNOWN:
bb44e0d1
JK
2316 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
2317 (uint64_t)run->hw.hardware_exit_reason);
73aaec4a 2318 ret = -1;
05330448 2319 break;
7c80eef8 2320 case KVM_EXIT_INTERNAL_ERROR:
5326ab55 2321 ret = kvm_handle_internal_error(cpu, run);
7c80eef8 2322 break;
99040447
PS
2323 case KVM_EXIT_SYSTEM_EVENT:
2324 switch (run->system_event.type) {
2325 case KVM_SYSTEM_EVENT_SHUTDOWN:
cf83f140 2326 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
99040447
PS
2327 ret = EXCP_INTERRUPT;
2328 break;
2329 case KVM_SYSTEM_EVENT_RESET:
cf83f140 2330 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
99040447
PS
2331 ret = EXCP_INTERRUPT;
2332 break;
7c207b90 2333 case KVM_SYSTEM_EVENT_CRASH:
d187e08d 2334 kvm_cpu_synchronize_state(cpu);
7c207b90 2335 qemu_mutex_lock_iothread();
c86f106b 2336 qemu_system_guest_panicked(cpu_get_crash_info(cpu));
7c207b90
AS
2337 qemu_mutex_unlock_iothread();
2338 ret = 0;
2339 break;
99040447
PS
2340 default:
2341 DPRINTF("kvm_arch_handle_exit\n");
2342 ret = kvm_arch_handle_exit(cpu, run);
2343 break;
2344 }
2345 break;
05330448 2346 default:
8c0d577e 2347 DPRINTF("kvm_arch_handle_exit\n");
20d695a9 2348 ret = kvm_arch_handle_exit(cpu, run);
05330448
AL
2349 break;
2350 }
d73cd8f4 2351 } while (ret == 0);
05330448 2352
1d78a3c3 2353 cpu_exec_end(cpu);
4b8523ee
JK
2354 qemu_mutex_lock_iothread();
2355
73aaec4a 2356 if (ret < 0) {
90c84c56 2357 cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
0461d5a6 2358 vm_stop(RUN_STATE_INTERNAL_ERROR);
becfc390
AL
2359 }
2360
c5c6679d 2361 atomic_set(&cpu->exit_request, 0);
05330448
AL
2362 return ret;
2363}
2364
984b5181 2365int kvm_ioctl(KVMState *s, int type, ...)
05330448
AL
2366{
2367 int ret;
984b5181
AL
2368 void *arg;
2369 va_list ap;
05330448 2370
984b5181
AL
2371 va_start(ap, type);
2372 arg = va_arg(ap, void *);
2373 va_end(ap);
2374
9c775729 2375 trace_kvm_ioctl(type, arg);
984b5181 2376 ret = ioctl(s->fd, type, arg);
a426e122 2377 if (ret == -1) {
05330448 2378 ret = -errno;
a426e122 2379 }
05330448
AL
2380 return ret;
2381}
2382
984b5181 2383int kvm_vm_ioctl(KVMState *s, int type, ...)
05330448
AL
2384{
2385 int ret;
984b5181
AL
2386 void *arg;
2387 va_list ap;
2388
2389 va_start(ap, type);
2390 arg = va_arg(ap, void *);
2391 va_end(ap);
05330448 2392
9c775729 2393 trace_kvm_vm_ioctl(type, arg);
984b5181 2394 ret = ioctl(s->vmfd, type, arg);
a426e122 2395 if (ret == -1) {
05330448 2396 ret = -errno;
a426e122 2397 }
05330448
AL
2398 return ret;
2399}
2400
1bc22652 2401int kvm_vcpu_ioctl(CPUState *cpu, int type, ...)
05330448
AL
2402{
2403 int ret;
984b5181
AL
2404 void *arg;
2405 va_list ap;
2406
2407 va_start(ap, type);
2408 arg = va_arg(ap, void *);
2409 va_end(ap);
05330448 2410
9c775729 2411 trace_kvm_vcpu_ioctl(cpu->cpu_index, type, arg);
8737c51c 2412 ret = ioctl(cpu->kvm_fd, type, arg);
a426e122 2413 if (ret == -1) {
05330448 2414 ret = -errno;
a426e122 2415 }
05330448
AL
2416 return ret;
2417}
bd322087 2418
0a6a7cca
CD
2419int kvm_device_ioctl(int fd, int type, ...)
2420{
2421 int ret;
2422 void *arg;
2423 va_list ap;
2424
2425 va_start(ap, type);
2426 arg = va_arg(ap, void *);
2427 va_end(ap);
2428
2429 trace_kvm_device_ioctl(fd, type, arg);
2430 ret = ioctl(fd, type, arg);
2431 if (ret == -1) {
2432 ret = -errno;
2433 }
2434 return ret;
2435}
2436
d0a073a1
DD
2437int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr)
2438{
2439 int ret;
2440 struct kvm_device_attr attribute = {
2441 .group = group,
2442 .attr = attr,
2443 };
2444
2445 if (!kvm_vm_attributes_allowed) {
2446 return 0;
2447 }
2448
2449 ret = kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attribute);
2450 /* kvm returns 0 on success for HAS_DEVICE_ATTR */
2451 return ret ? 0 : 1;
2452}
2453
4b3cfe72
PF
2454int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr)
2455{
2456 struct kvm_device_attr attribute = {
2457 .group = group,
2458 .attr = attr,
2459 .flags = 0,
2460 };
2461
2462 return kvm_device_ioctl(dev_fd, KVM_HAS_DEVICE_ATTR, &attribute) ? 0 : 1;
2463}
2464
556969e9
EA
2465int kvm_device_access(int fd, int group, uint64_t attr,
2466 void *val, bool write, Error **errp)
4b3cfe72
PF
2467{
2468 struct kvm_device_attr kvmattr;
2469 int err;
2470
2471 kvmattr.flags = 0;
2472 kvmattr.group = group;
2473 kvmattr.attr = attr;
2474 kvmattr.addr = (uintptr_t)val;
2475
2476 err = kvm_device_ioctl(fd,
2477 write ? KVM_SET_DEVICE_ATTR : KVM_GET_DEVICE_ATTR,
2478 &kvmattr);
2479 if (err < 0) {
556969e9
EA
2480 error_setg_errno(errp, -err,
2481 "KVM_%s_DEVICE_ATTR failed: Group %d "
2482 "attr 0x%016" PRIx64,
2483 write ? "SET" : "GET", group, attr);
4b3cfe72 2484 }
556969e9 2485 return err;
4b3cfe72
PF
2486}
2487
62dd4eda 2488bool kvm_has_sync_mmu(void)
bd322087 2489{
62dd4eda 2490 return kvm_state->sync_mmu;
bd322087 2491}
e22a25c9 2492
a0fb002c
JK
2493int kvm_has_vcpu_events(void)
2494{
2495 return kvm_state->vcpu_events;
2496}
2497
b0b1d690
JK
2498int kvm_has_robust_singlestep(void)
2499{
2500 return kvm_state->robust_singlestep;
2501}
2502
ff44f1a3
JK
2503int kvm_has_debugregs(void)
2504{
2505 return kvm_state->debugregs;
2506}
2507
ebbfef2f
LA
2508int kvm_max_nested_state_length(void)
2509{
2510 return kvm_state->max_nested_state_len;
2511}
2512
d2f2b8a7
SH
2513int kvm_has_many_ioeventfds(void)
2514{
2515 if (!kvm_enabled()) {
2516 return 0;
2517 }
2518 return kvm_state->many_ioeventfds;
2519}
2520
84b058d7
JK
2521int kvm_has_gsi_routing(void)
2522{
a9c5eb0d 2523#ifdef KVM_CAP_IRQ_ROUTING
84b058d7 2524 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
a9c5eb0d
AG
2525#else
2526 return false;
2527#endif
84b058d7
JK
2528}
2529
3ab73842
JK
2530int kvm_has_intx_set_mask(void)
2531{
2532 return kvm_state->intx_set_mask;
2533}
2534
5d721b78
AG
2535bool kvm_arm_supports_user_irq(void)
2536{
2537 return kvm_check_extension(kvm_state, KVM_CAP_ARM_USER_IRQ);
2538}
2539
e22a25c9 2540#ifdef KVM_CAP_SET_GUEST_DEBUG
a60f24b5 2541struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
e22a25c9
AL
2542 target_ulong pc)
2543{
2544 struct kvm_sw_breakpoint *bp;
2545
a60f24b5 2546 QTAILQ_FOREACH(bp, &cpu->kvm_state->kvm_sw_breakpoints, entry) {
a426e122 2547 if (bp->pc == pc) {
e22a25c9 2548 return bp;
a426e122 2549 }
e22a25c9
AL
2550 }
2551 return NULL;
2552}
2553
a60f24b5 2554int kvm_sw_breakpoints_active(CPUState *cpu)
e22a25c9 2555{
a60f24b5 2556 return !QTAILQ_EMPTY(&cpu->kvm_state->kvm_sw_breakpoints);
e22a25c9
AL
2557}
2558
452e4751
GC
2559struct kvm_set_guest_debug_data {
2560 struct kvm_guest_debug dbg;
452e4751
GC
2561 int err;
2562};
2563
14e6fe12 2564static void kvm_invoke_set_guest_debug(CPUState *cpu, run_on_cpu_data data)
452e4751 2565{
14e6fe12
PB
2566 struct kvm_set_guest_debug_data *dbg_data =
2567 (struct kvm_set_guest_debug_data *) data.host_ptr;
b3807725 2568
3c0ed2a3 2569 dbg_data->err = kvm_vcpu_ioctl(cpu, KVM_SET_GUEST_DEBUG,
a60f24b5 2570 &dbg_data->dbg);
452e4751
GC
2571}
2572
38e478ec 2573int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
e22a25c9 2574{
452e4751 2575 struct kvm_set_guest_debug_data data;
e22a25c9 2576
b0b1d690 2577 data.dbg.control = reinject_trap;
e22a25c9 2578
ed2803da 2579 if (cpu->singlestep_enabled) {
b0b1d690
JK
2580 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
2581 }
20d695a9 2582 kvm_arch_update_guest_debug(cpu, &data.dbg);
e22a25c9 2583
14e6fe12
PB
2584 run_on_cpu(cpu, kvm_invoke_set_guest_debug,
2585 RUN_ON_CPU_HOST_PTR(&data));
452e4751 2586 return data.err;
e22a25c9
AL
2587}
2588
62278814 2589int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
e22a25c9
AL
2590 target_ulong len, int type)
2591{
2592 struct kvm_sw_breakpoint *bp;
e22a25c9
AL
2593 int err;
2594
2595 if (type == GDB_BREAKPOINT_SW) {
80b7cd73 2596 bp = kvm_find_sw_breakpoint(cpu, addr);
e22a25c9
AL
2597 if (bp) {
2598 bp->use_count++;
2599 return 0;
2600 }
2601
7267c094 2602 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
e22a25c9
AL
2603 bp->pc = addr;
2604 bp->use_count = 1;
80b7cd73 2605 err = kvm_arch_insert_sw_breakpoint(cpu, bp);
e22a25c9 2606 if (err) {
7267c094 2607 g_free(bp);
e22a25c9
AL
2608 return err;
2609 }
2610
80b7cd73 2611 QTAILQ_INSERT_HEAD(&cpu->kvm_state->kvm_sw_breakpoints, bp, entry);
e22a25c9
AL
2612 } else {
2613 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
a426e122 2614 if (err) {
e22a25c9 2615 return err;
a426e122 2616 }
e22a25c9
AL
2617 }
2618
bdc44640 2619 CPU_FOREACH(cpu) {
38e478ec 2620 err = kvm_update_guest_debug(cpu, 0);
a426e122 2621 if (err) {
e22a25c9 2622 return err;
a426e122 2623 }
e22a25c9
AL
2624 }
2625 return 0;
2626}
2627
62278814 2628int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
e22a25c9
AL
2629 target_ulong len, int type)
2630{
2631 struct kvm_sw_breakpoint *bp;
e22a25c9
AL
2632 int err;
2633
2634 if (type == GDB_BREAKPOINT_SW) {
80b7cd73 2635 bp = kvm_find_sw_breakpoint(cpu, addr);
a426e122 2636 if (!bp) {
e22a25c9 2637 return -ENOENT;
a426e122 2638 }
e22a25c9
AL
2639
2640 if (bp->use_count > 1) {
2641 bp->use_count--;
2642 return 0;
2643 }
2644
80b7cd73 2645 err = kvm_arch_remove_sw_breakpoint(cpu, bp);
a426e122 2646 if (err) {
e22a25c9 2647 return err;
a426e122 2648 }
e22a25c9 2649
80b7cd73 2650 QTAILQ_REMOVE(&cpu->kvm_state->kvm_sw_breakpoints, bp, entry);
7267c094 2651 g_free(bp);
e22a25c9
AL
2652 } else {
2653 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
a426e122 2654 if (err) {
e22a25c9 2655 return err;
a426e122 2656 }
e22a25c9
AL
2657 }
2658
bdc44640 2659 CPU_FOREACH(cpu) {
38e478ec 2660 err = kvm_update_guest_debug(cpu, 0);
a426e122 2661 if (err) {
e22a25c9 2662 return err;
a426e122 2663 }
e22a25c9
AL
2664 }
2665 return 0;
2666}
2667
1d5791f4 2668void kvm_remove_all_breakpoints(CPUState *cpu)
e22a25c9
AL
2669{
2670 struct kvm_sw_breakpoint *bp, *next;
80b7cd73 2671 KVMState *s = cpu->kvm_state;
dc54e252 2672 CPUState *tmpcpu;
e22a25c9 2673
72cf2d4f 2674 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
80b7cd73 2675 if (kvm_arch_remove_sw_breakpoint(cpu, bp) != 0) {
e22a25c9 2676 /* Try harder to find a CPU that currently sees the breakpoint. */
dc54e252
CG
2677 CPU_FOREACH(tmpcpu) {
2678 if (kvm_arch_remove_sw_breakpoint(tmpcpu, bp) == 0) {
e22a25c9 2679 break;
a426e122 2680 }
e22a25c9
AL
2681 }
2682 }
78021d6d
JK
2683 QTAILQ_REMOVE(&s->kvm_sw_breakpoints, bp, entry);
2684 g_free(bp);
e22a25c9
AL
2685 }
2686 kvm_arch_remove_all_hw_breakpoints();
2687
bdc44640 2688 CPU_FOREACH(cpu) {
38e478ec 2689 kvm_update_guest_debug(cpu, 0);
a426e122 2690 }
e22a25c9
AL
2691}
2692
2693#else /* !KVM_CAP_SET_GUEST_DEBUG */
2694
38e478ec 2695int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
e22a25c9
AL
2696{
2697 return -EINVAL;
2698}
2699
62278814 2700int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
e22a25c9
AL
2701 target_ulong len, int type)
2702{
2703 return -EINVAL;
2704}
2705
62278814 2706int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
e22a25c9
AL
2707 target_ulong len, int type)
2708{
2709 return -EINVAL;
2710}
2711
1d5791f4 2712void kvm_remove_all_breakpoints(CPUState *cpu)
e22a25c9
AL
2713{
2714}
2715#endif /* !KVM_CAP_SET_GUEST_DEBUG */
cc84de95 2716
18268b60 2717static int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset)
cc84de95 2718{
aed6efb9 2719 KVMState *s = kvm_state;
cc84de95
MT
2720 struct kvm_signal_mask *sigmask;
2721 int r;
2722
7267c094 2723 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
cc84de95 2724
aed6efb9 2725 sigmask->len = s->sigmask_len;
cc84de95 2726 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1bc22652 2727 r = kvm_vcpu_ioctl(cpu, KVM_SET_SIGNAL_MASK, sigmask);
7267c094 2728 g_free(sigmask);
cc84de95
MT
2729
2730 return r;
2731}
4d39892c 2732
cf0f7cf9 2733static void kvm_ipi_signal(int sig)
18268b60 2734{
cf0f7cf9
PB
2735 if (current_cpu) {
2736 assert(kvm_immediate_exit);
2737 kvm_cpu_kick(current_cpu);
2738 }
18268b60
PB
2739}
2740
2741void kvm_init_cpu_signals(CPUState *cpu)
2742{
2743 int r;
2744 sigset_t set;
2745 struct sigaction sigact;
2746
2747 memset(&sigact, 0, sizeof(sigact));
cf0f7cf9 2748 sigact.sa_handler = kvm_ipi_signal;
18268b60
PB
2749 sigaction(SIG_IPI, &sigact, NULL);
2750
2751 pthread_sigmask(SIG_BLOCK, NULL, &set);
2752#if defined KVM_HAVE_MCE_INJECTION
2753 sigdelset(&set, SIGBUS);
2754 pthread_sigmask(SIG_SETMASK, &set, NULL);
2755#endif
2756 sigdelset(&set, SIG_IPI);
cf0f7cf9
PB
2757 if (kvm_immediate_exit) {
2758 r = pthread_sigmask(SIG_SETMASK, &set, NULL);
2759 } else {
2760 r = kvm_set_signal_mask(cpu, &set);
2761 }
18268b60
PB
2762 if (r) {
2763 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
2764 exit(1);
2765 }
2766}
2767
2ae41db2 2768/* Called asynchronously in VCPU thread. */
290adf38 2769int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
a1b87fe0 2770{
2ae41db2
PB
2771#ifdef KVM_HAVE_MCE_INJECTION
2772 if (have_sigbus_pending) {
2773 return 1;
2774 }
2775 have_sigbus_pending = true;
2776 pending_sigbus_addr = addr;
2777 pending_sigbus_code = code;
2778 atomic_set(&cpu->exit_request, 1);
2779 return 0;
2780#else
2781 return 1;
2782#endif
a1b87fe0
JK
2783}
2784
2ae41db2 2785/* Called synchronously (via signalfd) in main thread. */
a1b87fe0
JK
2786int kvm_on_sigbus(int code, void *addr)
2787{
2ae41db2 2788#ifdef KVM_HAVE_MCE_INJECTION
4d39892c
PB
2789 /* Action required MCE kills the process if SIGBUS is blocked. Because
2790 * that's what happens in the I/O thread, where we handle MCE via signalfd,
2791 * we can only get action optional here.
2792 */
2793 assert(code != BUS_MCEERR_AR);
2794 kvm_arch_on_sigbus_vcpu(first_cpu, code, addr);
2795 return 0;
2ae41db2
PB
2796#else
2797 return 1;
2798#endif
a1b87fe0 2799}
0a6a7cca
CD
2800
2801int kvm_create_device(KVMState *s, uint64_t type, bool test)
2802{
2803 int ret;
2804 struct kvm_create_device create_dev;
2805
2806 create_dev.type = type;
2807 create_dev.fd = -1;
2808 create_dev.flags = test ? KVM_CREATE_DEVICE_TEST : 0;
2809
2810 if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
2811 return -ENOTSUP;
2812 }
2813
2814 ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &create_dev);
2815 if (ret) {
2816 return ret;
2817 }
2818
2819 return test ? 0 : create_dev.fd;
2820}
ada4135f 2821
29039acf
PX
2822bool kvm_device_supported(int vmfd, uint64_t type)
2823{
2824 struct kvm_create_device create_dev = {
2825 .type = type,
2826 .fd = -1,
2827 .flags = KVM_CREATE_DEVICE_TEST,
2828 };
2829
2830 if (ioctl(vmfd, KVM_CHECK_EXTENSION, KVM_CAP_DEVICE_CTRL) <= 0) {
2831 return false;
2832 }
2833
2834 return (ioctl(vmfd, KVM_CREATE_DEVICE, &create_dev) >= 0);
2835}
2836
ada4135f
CH
2837int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source)
2838{
2839 struct kvm_one_reg reg;
2840 int r;
2841
2842 reg.id = id;
2843 reg.addr = (uintptr_t) source;
2844 r = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
2845 if (r) {
844a3d34 2846 trace_kvm_failed_reg_set(id, strerror(-r));
ada4135f
CH
2847 }
2848 return r;
2849}
2850
2851int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target)
2852{
2853 struct kvm_one_reg reg;
2854 int r;
2855
2856 reg.id = id;
2857 reg.addr = (uintptr_t) target;
2858 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
2859 if (r) {
844a3d34 2860 trace_kvm_failed_reg_get(id, strerror(-r));
ada4135f
CH
2861 }
2862 return r;
2863}
782c3f29 2864
8072aae3
AK
2865static bool kvm_accel_has_memory(MachineState *ms, AddressSpace *as,
2866 hwaddr start_addr, hwaddr size)
2867{
2868 KVMState *kvm = KVM_STATE(ms->accelerator);
2869 int i;
2870
2871 for (i = 0; i < kvm->nr_as; ++i) {
2872 if (kvm->as[i].as == as && kvm->as[i].ml) {
2873 return NULL != kvm_lookup_matching_slot(kvm->as[i].ml,
2874 start_addr, size);
2875 }
2876 }
2877
2878 return false;
2879}
2880
782c3f29
EH
2881static void kvm_accel_class_init(ObjectClass *oc, void *data)
2882{
2883 AccelClass *ac = ACCEL_CLASS(oc);
2884 ac->name = "KVM";
0d15da8e 2885 ac->init_machine = kvm_init;
8072aae3 2886 ac->has_memory = kvm_accel_has_memory;
782c3f29
EH
2887 ac->allowed = &kvm_allowed;
2888}
2889
2890static const TypeInfo kvm_accel_type = {
2891 .name = TYPE_KVM_ACCEL,
2892 .parent = TYPE_ACCEL,
2893 .class_init = kvm_accel_class_init,
fc02086b 2894 .instance_size = sizeof(KVMState),
782c3f29
EH
2895};
2896
2897static void kvm_type_init(void)
2898{
2899 type_register_static(&kvm_accel_type);
2900}
2901
2902type_init(kvm_type_init);