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