blob: 15bc42fcdbbec577bc57d9822198c786f982ee6d [file] [log] [blame]
aliguori05330442008-11-05 16:29:27 +00001/*
2 * QEMU KVM support
3 *
4 * Copyright IBM, Corp. 2008
aliguori5832d1f2008-11-24 19:36:26 +00005 * Red Hat, Inc. 2008
aliguori05330442008-11-05 16:29:27 +00006 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
aliguori5832d1f2008-11-24 19:36:26 +00009 * Glauber Costa <gcosta@redhat.com>
aliguori05330442008-11-05 16:29:27 +000010 *
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
16#include <sys/types.h>
17#include <sys/ioctl.h>
18#include <sys/mman.h>
aliguori984b5182008-11-13 19:21:00 +000019#include <stdarg.h>
aliguori05330442008-11-05 16:29:27 +000020
21#include <linux/kvm.h>
22
23#include "qemu-common.h"
Marcelo Tosatti85199472010-02-22 13:57:54 -030024#include "qemu-barrier.h"
aliguori05330442008-11-05 16:29:27 +000025#include "sysemu.h"
Jan Kiszkad33a1812009-05-02 00:29:37 +020026#include "hw/hw.h"
aliguorie22a25c2009-03-12 20:12:48 +000027#include "gdbstub.h"
aliguori05330442008-11-05 16:29:27 +000028#include "kvm.h"
Marcelo Tosatti8369e012010-04-23 14:04:14 -030029#include "bswap.h"
Avi Kivitya01672d2011-12-18 14:06:05 +020030#include "memory.h"
Avi Kivity80a1ea32012-02-08 16:39:06 +020031#include "exec-memory.h"
aliguori05330442008-11-05 16:29:27 +000032
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +020033/* This check must be after config-host.h is included */
34#ifdef CONFIG_EVENTFD
35#include <sys/eventfd.h>
36#endif
37
aliguorif65ed4c2008-12-09 20:09:57 +000038/* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
39#define PAGE_SIZE TARGET_PAGE_SIZE
40
aliguori05330442008-11-05 16:29:27 +000041//#define DEBUG_KVM
42
43#ifdef DEBUG_KVM
Blue Swirl8c0d5772010-04-18 14:22:14 +000044#define DPRINTF(fmt, ...) \
aliguori05330442008-11-05 16:29:27 +000045 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46#else
Blue Swirl8c0d5772010-04-18 14:22:14 +000047#define DPRINTF(fmt, ...) \
aliguori05330442008-11-05 16:29:27 +000048 do { } while (0)
49#endif
50
aliguori34fc6432008-11-19 17:41:58 +000051typedef struct KVMSlot
52{
Anthony Liguoric227f092009-10-01 16:12:16 -050053 target_phys_addr_t start_addr;
54 ram_addr_t memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +020055 void *ram;
aliguori34fc6432008-11-19 17:41:58 +000056 int slot;
57 int flags;
58} KVMSlot;
aliguori05330442008-11-05 16:29:27 +000059
aliguori5832d1f2008-11-24 19:36:26 +000060typedef struct kvm_dirty_log KVMDirtyLog;
61
aliguori05330442008-11-05 16:29:27 +000062struct KVMState
63{
64 KVMSlot slots[32];
65 int fd;
66 int vmfd;
aliguorif65ed4c2008-12-09 20:09:57 +000067 int coalesced_mmio;
Sheng Yang62a27442010-01-26 19:21:16 +080068 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
Avi Kivity1cae88b2011-10-18 19:43:12 +020069 bool coalesced_flush_in_progress;
Jan Kiszkae69917e2009-05-01 20:42:15 +020070 int broken_set_mem_region;
Jan Kiszka4495d6a2009-05-01 20:52:46 +020071 int migration_log;
Jan Kiszkaa0fb0022009-11-25 00:33:03 +010072 int vcpu_events;
Jan Kiszkab0b1d692010-03-01 19:10:29 +010073 int robust_singlestep;
Jan Kiszkaff44f1a2010-03-12 15:20:49 +010074 int debugregs;
aliguorie22a25c2009-03-12 20:12:48 +000075#ifdef KVM_CAP_SET_GUEST_DEBUG
76 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
77#endif
Glauber Costa6f725c12009-07-21 12:26:58 -030078 int irqchip_in_kernel;
79 int pit_in_kernel;
Sheng Yangf1665b22010-06-17 17:53:07 +080080 int xsave, xcrs;
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +020081 int many_ioeventfds;
Jan Kiszka84b058d2011-10-15 11:49:47 +020082 int irqchip_inject_ioctl;
83#ifdef KVM_CAP_IRQ_ROUTING
84 struct kvm_irq_routing *irq_routes;
85 int nr_allocated_irq_routes;
86 uint32_t *used_gsi_bitmap;
87 unsigned int max_gsi;
88#endif
aliguori05330442008-11-05 16:29:27 +000089};
90
Jan Kiszka6a7af8c2011-02-07 12:19:25 +010091KVMState *kvm_state;
aliguori05330442008-11-05 16:29:27 +000092
Jan Kiszka94a8d392011-01-21 21:48:17 +010093static const KVMCapabilityInfo kvm_required_capabilites[] = {
94 KVM_CAP_INFO(USER_MEMORY),
95 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
96 KVM_CAP_LAST_INFO
97};
98
aliguori05330442008-11-05 16:29:27 +000099static KVMSlot *kvm_alloc_slot(KVMState *s)
100{
101 int i;
102
103 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
Jan Kiszkaa426e122011-01-04 09:32:13 +0100104 if (s->slots[i].memory_size == 0) {
aliguori05330442008-11-05 16:29:27 +0000105 return &s->slots[i];
Jan Kiszkaa426e122011-01-04 09:32:13 +0100106 }
aliguori05330442008-11-05 16:29:27 +0000107 }
108
aliguorid3f8d372009-04-17 14:26:29 +0000109 fprintf(stderr, "%s: no free slot available\n", __func__);
110 abort();
111}
112
113static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
Anthony Liguoric227f092009-10-01 16:12:16 -0500114 target_phys_addr_t start_addr,
115 target_phys_addr_t end_addr)
aliguorid3f8d372009-04-17 14:26:29 +0000116{
117 int i;
118
119 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
120 KVMSlot *mem = &s->slots[i];
121
122 if (start_addr == mem->start_addr &&
123 end_addr == mem->start_addr + mem->memory_size) {
124 return mem;
125 }
126 }
127
aliguori05330442008-11-05 16:29:27 +0000128 return NULL;
129}
130
aliguori6152e2a2009-04-17 14:26:33 +0000131/*
132 * Find overlapping slot with lowest start address
133 */
134static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
Anthony Liguoric227f092009-10-01 16:12:16 -0500135 target_phys_addr_t start_addr,
136 target_phys_addr_t end_addr)
aliguori05330442008-11-05 16:29:27 +0000137{
aliguori6152e2a2009-04-17 14:26:33 +0000138 KVMSlot *found = NULL;
aliguori05330442008-11-05 16:29:27 +0000139 int i;
140
141 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
142 KVMSlot *mem = &s->slots[i];
143
aliguori6152e2a2009-04-17 14:26:33 +0000144 if (mem->memory_size == 0 ||
145 (found && found->start_addr < mem->start_addr)) {
146 continue;
147 }
148
149 if (end_addr > mem->start_addr &&
150 start_addr < mem->start_addr + mem->memory_size) {
151 found = mem;
152 }
aliguori05330442008-11-05 16:29:27 +0000153 }
154
aliguori6152e2a2009-04-17 14:26:33 +0000155 return found;
aliguori05330442008-11-05 16:29:27 +0000156}
157
Avi Kivity9f213ed2011-12-15 19:55:26 +0200158int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
159 target_phys_addr_t *phys_addr)
Huang Ying983dfc32010-10-11 15:31:20 -0300160{
161 int i;
162
163 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
164 KVMSlot *mem = &s->slots[i];
165
Avi Kivity9f213ed2011-12-15 19:55:26 +0200166 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
167 *phys_addr = mem->start_addr + (ram - mem->ram);
Huang Ying983dfc32010-10-11 15:31:20 -0300168 return 1;
169 }
170 }
171
172 return 0;
173}
174
aliguori5832d1f2008-11-24 19:36:26 +0000175static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
176{
177 struct kvm_userspace_memory_region mem;
178
179 mem.slot = slot->slot;
180 mem.guest_phys_addr = slot->start_addr;
181 mem.memory_size = slot->memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200182 mem.userspace_addr = (unsigned long)slot->ram;
aliguori5832d1f2008-11-24 19:36:26 +0000183 mem.flags = slot->flags;
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200184 if (s->migration_log) {
185 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
186 }
aliguori5832d1f2008-11-24 19:36:26 +0000187 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
188}
189
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200190static void kvm_reset_vcpu(void *opaque)
191{
192 CPUState *env = opaque;
193
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100194 kvm_arch_reset_vcpu(env);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200195}
aliguori5832d1f2008-11-24 19:36:26 +0000196
Glauber Costa6f725c12009-07-21 12:26:58 -0300197int kvm_irqchip_in_kernel(void)
198{
199 return kvm_state->irqchip_in_kernel;
200}
201
202int kvm_pit_in_kernel(void)
203{
204 return kvm_state->pit_in_kernel;
205}
206
aliguori05330442008-11-05 16:29:27 +0000207int kvm_init_vcpu(CPUState *env)
208{
209 KVMState *s = kvm_state;
210 long mmap_size;
211 int ret;
212
Blue Swirl8c0d5772010-04-18 14:22:14 +0000213 DPRINTF("kvm_init_vcpu\n");
aliguori05330442008-11-05 16:29:27 +0000214
aliguori984b5182008-11-13 19:21:00 +0000215 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
aliguori05330442008-11-05 16:29:27 +0000216 if (ret < 0) {
Blue Swirl8c0d5772010-04-18 14:22:14 +0000217 DPRINTF("kvm_create_vcpu failed\n");
aliguori05330442008-11-05 16:29:27 +0000218 goto err;
219 }
220
221 env->kvm_fd = ret;
222 env->kvm_state = s;
Jan Kiszkad841b6c2011-03-15 12:26:20 +0100223 env->kvm_vcpu_dirty = 1;
aliguori05330442008-11-05 16:29:27 +0000224
225 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
226 if (mmap_size < 0) {
Jan Kiszka748a6802011-02-01 22:15:48 +0100227 ret = mmap_size;
Blue Swirl8c0d5772010-04-18 14:22:14 +0000228 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
aliguori05330442008-11-05 16:29:27 +0000229 goto err;
230 }
231
232 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
233 env->kvm_fd, 0);
234 if (env->kvm_run == MAP_FAILED) {
235 ret = -errno;
Blue Swirl8c0d5772010-04-18 14:22:14 +0000236 DPRINTF("mmap'ing vcpu state failed\n");
aliguori05330442008-11-05 16:29:27 +0000237 goto err;
238 }
239
Jan Kiszkaa426e122011-01-04 09:32:13 +0100240 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
241 s->coalesced_mmio_ring =
242 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
243 }
Sheng Yang62a27442010-01-26 19:21:16 +0800244
aliguori05330442008-11-05 16:29:27 +0000245 ret = kvm_arch_init_vcpu(env);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200246 if (ret == 0) {
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200247 qemu_register_reset(kvm_reset_vcpu, env);
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100248 kvm_arch_reset_vcpu(env);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200249 }
aliguori05330442008-11-05 16:29:27 +0000250err:
251 return ret;
252}
253
aliguori5832d1f2008-11-24 19:36:26 +0000254/*
255 * dirty pages logging control
256 */
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300257
258static int kvm_mem_flags(KVMState *s, bool log_dirty)
259{
260 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
261}
262
263static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
aliguori5832d1f2008-11-24 19:36:26 +0000264{
265 KVMState *s = kvm_state;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300266 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200267 int old_flags;
268
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200269 old_flags = mem->flags;
aliguori5832d1f2008-11-24 19:36:26 +0000270
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300271 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
aliguori5832d1f2008-11-24 19:36:26 +0000272 mem->flags = flags;
273
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200274 /* If nothing changed effectively, no need to issue ioctl */
275 if (s->migration_log) {
276 flags |= KVM_MEM_LOG_DIRTY_PAGES;
277 }
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300278
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200279 if (flags == old_flags) {
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300280 return 0;
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200281 }
282
aliguori5832d1f2008-11-24 19:36:26 +0000283 return kvm_set_user_memory_region(s, mem);
284}
285
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300286static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
287 ram_addr_t size, bool log_dirty)
288{
289 KVMState *s = kvm_state;
290 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
291
292 if (mem == NULL) {
293 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
294 TARGET_FMT_plx "\n", __func__, phys_addr,
295 (target_phys_addr_t)(phys_addr + size - 1));
296 return -EINVAL;
297 }
298 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
299}
300
Avi Kivitya01672d2011-12-18 14:06:05 +0200301static void kvm_log_start(MemoryListener *listener,
302 MemoryRegionSection *section)
aliguori5832d1f2008-11-24 19:36:26 +0000303{
Avi Kivitya01672d2011-12-18 14:06:05 +0200304 int r;
305
306 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
307 section->size, true);
308 if (r < 0) {
309 abort();
310 }
aliguori5832d1f2008-11-24 19:36:26 +0000311}
312
Avi Kivitya01672d2011-12-18 14:06:05 +0200313static void kvm_log_stop(MemoryListener *listener,
314 MemoryRegionSection *section)
aliguori5832d1f2008-11-24 19:36:26 +0000315{
Avi Kivitya01672d2011-12-18 14:06:05 +0200316 int r;
317
318 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
319 section->size, false);
320 if (r < 0) {
321 abort();
322 }
aliguori5832d1f2008-11-24 19:36:26 +0000323}
324
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200325static int kvm_set_migration_log(int enable)
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200326{
327 KVMState *s = kvm_state;
328 KVMSlot *mem;
329 int i, err;
330
331 s->migration_log = enable;
332
333 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
334 mem = &s->slots[i];
335
Alex Williamson70fedd72010-07-14 13:36:49 -0600336 if (!mem->memory_size) {
337 continue;
338 }
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200339 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
340 continue;
341 }
342 err = kvm_set_user_memory_region(s, mem);
343 if (err) {
344 return err;
345 }
346 }
347 return 0;
348}
349
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300350/* get kvm's dirty pages bitmap and update qemu's */
Avi Kivityffcde122011-12-19 13:18:13 +0200351static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
352 unsigned long *bitmap)
Alexander Graf96c16062009-07-27 12:49:56 +0200353{
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300354 unsigned int i, j;
Benjamin Herrenschmidtaa90fec2012-01-11 19:46:21 +0000355 unsigned long page_number, c;
356 target_phys_addr_t addr, addr1;
Avi Kivityffcde122011-12-19 13:18:13 +0200357 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300358
359 /*
360 * bitmap-traveling is faster than memory-traveling (for addr...)
361 * especially when most of the memory is not dirty.
362 */
363 for (i = 0; i < len; i++) {
364 if (bitmap[i] != 0) {
365 c = leul_to_cpu(bitmap[i]);
366 do {
367 j = ffsl(c) - 1;
368 c &= ~(1ul << j);
369 page_number = i * HOST_LONG_BITS + j;
370 addr1 = page_number * TARGET_PAGE_SIZE;
Avi Kivityffcde122011-12-19 13:18:13 +0200371 addr = section->offset_within_region + addr1;
Blue Swirlfd4aa972011-10-16 16:04:59 +0000372 memory_region_set_dirty(section->mr, addr, TARGET_PAGE_SIZE);
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300373 } while (c != 0);
374 }
375 }
376 return 0;
Alexander Graf96c16062009-07-27 12:49:56 +0200377}
378
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300379#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
380
aliguori5832d1f2008-11-24 19:36:26 +0000381/**
382 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
Blue Swirlfd4aa972011-10-16 16:04:59 +0000383 * This function updates qemu's dirty bitmap using
384 * memory_region_set_dirty(). This means all bits are set
385 * to dirty.
aliguori5832d1f2008-11-24 19:36:26 +0000386 *
aliguorid3f8d372009-04-17 14:26:29 +0000387 * @start_add: start of logged region.
aliguori5832d1f2008-11-24 19:36:26 +0000388 * @end_addr: end of logged region.
389 */
Avi Kivityffcde122011-12-19 13:18:13 +0200390static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
aliguori5832d1f2008-11-24 19:36:26 +0000391{
392 KVMState *s = kvm_state;
Jan Kiszka151f7742009-05-01 20:52:47 +0200393 unsigned long size, allocated_size = 0;
Jan Kiszka151f7742009-05-01 20:52:47 +0200394 KVMDirtyLog d;
395 KVMSlot *mem;
396 int ret = 0;
Avi Kivityffcde122011-12-19 13:18:13 +0200397 target_phys_addr_t start_addr = section->offset_within_address_space;
398 target_phys_addr_t end_addr = start_addr + section->size;
aliguori5832d1f2008-11-24 19:36:26 +0000399
Jan Kiszka151f7742009-05-01 20:52:47 +0200400 d.dirty_bitmap = NULL;
401 while (start_addr < end_addr) {
402 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
403 if (mem == NULL) {
404 break;
405 }
406
Michael Tokarev51b0c602011-04-26 20:13:49 +0400407 /* XXX bad kernel interface alert
408 * For dirty bitmap, kernel allocates array of size aligned to
409 * bits-per-long. But for case when the kernel is 64bits and
410 * the userspace is 32bits, userspace can't align to the same
411 * bits-per-long, since sizeof(long) is different between kernel
412 * and user space. This way, userspace will provide buffer which
413 * may be 4 bytes less than the kernel will use, resulting in
414 * userspace memory corruption (which is not detectable by valgrind
415 * too, in most cases).
416 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
417 * a hope that sizeof(long) wont become >8 any time soon.
418 */
419 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
420 /*HOST_LONG_BITS*/ 64) / 8;
Jan Kiszka151f7742009-05-01 20:52:47 +0200421 if (!d.dirty_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500422 d.dirty_bitmap = g_malloc(size);
Jan Kiszka151f7742009-05-01 20:52:47 +0200423 } else if (size > allocated_size) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500424 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
Jan Kiszka151f7742009-05-01 20:52:47 +0200425 }
426 allocated_size = size;
427 memset(d.dirty_bitmap, 0, allocated_size);
428
429 d.slot = mem->slot;
430
Anthony Liguori6e489f32009-07-27 15:23:59 -0500431 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
Blue Swirl8c0d5772010-04-18 14:22:14 +0000432 DPRINTF("ioctl failed %d\n", errno);
Jan Kiszka151f7742009-05-01 20:52:47 +0200433 ret = -1;
434 break;
435 }
436
Avi Kivityffcde122011-12-19 13:18:13 +0200437 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300438 start_addr = mem->start_addr + mem->memory_size;
aliguori5832d1f2008-11-24 19:36:26 +0000439 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500440 g_free(d.dirty_bitmap);
Jan Kiszka151f7742009-05-01 20:52:47 +0200441
442 return ret;
aliguori5832d1f2008-11-24 19:36:26 +0000443}
444
Anthony Liguoric227f092009-10-01 16:12:16 -0500445int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +0000446{
447 int ret = -ENOSYS;
aliguorif65ed4c2008-12-09 20:09:57 +0000448 KVMState *s = kvm_state;
449
450 if (s->coalesced_mmio) {
451 struct kvm_coalesced_mmio_zone zone;
452
453 zone.addr = start;
454 zone.size = size;
455
456 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
457 }
aliguorif65ed4c2008-12-09 20:09:57 +0000458
459 return ret;
460}
461
Anthony Liguoric227f092009-10-01 16:12:16 -0500462int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +0000463{
464 int ret = -ENOSYS;
aliguorif65ed4c2008-12-09 20:09:57 +0000465 KVMState *s = kvm_state;
466
467 if (s->coalesced_mmio) {
468 struct kvm_coalesced_mmio_zone zone;
469
470 zone.addr = start;
471 zone.size = size;
472
473 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
474 }
aliguorif65ed4c2008-12-09 20:09:57 +0000475
476 return ret;
477}
478
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500479int kvm_check_extension(KVMState *s, unsigned int extension)
480{
481 int ret;
482
483 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
484 if (ret < 0) {
485 ret = 0;
486 }
487
488 return ret;
489}
490
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200491static int kvm_check_many_ioeventfds(void)
492{
Stefan Hajnoczid0dcac82011-01-25 16:17:14 +0000493 /* Userspace can use ioeventfd for io notification. This requires a host
494 * that supports eventfd(2) and an I/O thread; since eventfd does not
495 * support SIGIO it cannot interrupt the vcpu.
496 *
497 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200498 * can avoid creating too many ioeventfds.
499 */
Anthony Liguori12d45362011-08-22 08:24:58 -0500500#if defined(CONFIG_EVENTFD)
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200501 int ioeventfds[7];
502 int i, ret = 0;
503 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
504 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
505 if (ioeventfds[i] < 0) {
506 break;
507 }
508 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
509 if (ret < 0) {
510 close(ioeventfds[i]);
511 break;
512 }
513 }
514
515 /* Decide whether many devices are supported or not */
516 ret = i == ARRAY_SIZE(ioeventfds);
517
518 while (i-- > 0) {
519 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
520 close(ioeventfds[i]);
521 }
522 return ret;
523#else
524 return 0;
525#endif
526}
527
Jan Kiszka94a8d392011-01-21 21:48:17 +0100528static const KVMCapabilityInfo *
529kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
530{
531 while (list->name) {
532 if (!kvm_check_extension(s, list->value)) {
533 return list;
534 }
535 list++;
536 }
537 return NULL;
538}
539
Avi Kivitya01672d2011-12-18 14:06:05 +0200540static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200541{
542 KVMState *s = kvm_state;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200543 KVMSlot *mem, old;
544 int err;
Avi Kivitya01672d2011-12-18 14:06:05 +0200545 MemoryRegion *mr = section->mr;
546 bool log_dirty = memory_region_is_logging(mr);
547 target_phys_addr_t start_addr = section->offset_within_address_space;
548 ram_addr_t size = section->size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200549 void *ram = NULL;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200550
Gleb Natapov14542fe2010-07-28 18:13:23 +0300551 /* kvm works in page size chunks, but the function may be called
552 with sub-page size and unaligned start address. */
553 size = TARGET_PAGE_ALIGN(size);
554 start_addr = TARGET_PAGE_ALIGN(start_addr);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200555
Avi Kivitya01672d2011-12-18 14:06:05 +0200556 if (!memory_region_is_ram(mr)) {
557 return;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200558 }
559
Avi Kivitya01672d2011-12-18 14:06:05 +0200560 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region;
561
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200562 while (1) {
563 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
564 if (!mem) {
565 break;
566 }
567
Avi Kivitya01672d2011-12-18 14:06:05 +0200568 if (add && start_addr >= mem->start_addr &&
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200569 (start_addr + size <= mem->start_addr + mem->memory_size) &&
Avi Kivity9f213ed2011-12-15 19:55:26 +0200570 (ram - start_addr == mem->ram - mem->start_addr)) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200571 /* The new slot fits into the existing one and comes with
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300572 * identical parameters - update flags and done. */
573 kvm_slot_dirty_pages_log_change(mem, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200574 return;
575 }
576
577 old = *mem;
578
Avi Kivity3fbffb62012-01-15 16:13:59 +0200579 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
580 kvm_physical_sync_dirty_bitmap(section);
581 }
582
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200583 /* unregister the overlapping slot */
584 mem->memory_size = 0;
585 err = kvm_set_user_memory_region(s, mem);
586 if (err) {
587 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
588 __func__, strerror(-err));
589 abort();
590 }
591
592 /* Workaround for older KVM versions: we can't join slots, even not by
593 * unregistering the previous ones and then registering the larger
594 * slot. We have to maintain the existing fragmentation. Sigh.
595 *
596 * This workaround assumes that the new slot starts at the same
597 * address as the first existing one. If not or if some overlapping
598 * slot comes around later, we will fail (not seen in practice so far)
599 * - and actually require a recent KVM version. */
600 if (s->broken_set_mem_region &&
Avi Kivitya01672d2011-12-18 14:06:05 +0200601 old.start_addr == start_addr && old.memory_size < size && add) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200602 mem = kvm_alloc_slot(s);
603 mem->memory_size = old.memory_size;
604 mem->start_addr = old.start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200605 mem->ram = old.ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300606 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200607
608 err = kvm_set_user_memory_region(s, mem);
609 if (err) {
610 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
611 strerror(-err));
612 abort();
613 }
614
615 start_addr += old.memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200616 ram += old.memory_size;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200617 size -= old.memory_size;
618 continue;
619 }
620
621 /* register prefix slot */
622 if (old.start_addr < start_addr) {
623 mem = kvm_alloc_slot(s);
624 mem->memory_size = start_addr - old.start_addr;
625 mem->start_addr = old.start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200626 mem->ram = old.ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300627 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200628
629 err = kvm_set_user_memory_region(s, mem);
630 if (err) {
631 fprintf(stderr, "%s: error registering prefix slot: %s\n",
632 __func__, strerror(-err));
Alexander Grafd4d68682011-04-16 10:15:11 +0200633#ifdef TARGET_PPC
634 fprintf(stderr, "%s: This is probably because your kernel's " \
635 "PAGE_SIZE is too big. Please try to use 4k " \
636 "PAGE_SIZE!\n", __func__);
637#endif
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200638 abort();
639 }
640 }
641
642 /* register suffix slot */
643 if (old.start_addr + old.memory_size > start_addr + size) {
644 ram_addr_t size_delta;
645
646 mem = kvm_alloc_slot(s);
647 mem->start_addr = start_addr + size;
648 size_delta = mem->start_addr - old.start_addr;
649 mem->memory_size = old.memory_size - size_delta;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200650 mem->ram = old.ram + size_delta;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300651 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200652
653 err = kvm_set_user_memory_region(s, mem);
654 if (err) {
655 fprintf(stderr, "%s: error registering suffix slot: %s\n",
656 __func__, strerror(-err));
657 abort();
658 }
659 }
660 }
661
662 /* in case the KVM bug workaround already "consumed" the new slot */
Jan Kiszkaa426e122011-01-04 09:32:13 +0100663 if (!size) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200664 return;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100665 }
Avi Kivitya01672d2011-12-18 14:06:05 +0200666 if (!add) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200667 return;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100668 }
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200669 mem = kvm_alloc_slot(s);
670 mem->memory_size = size;
671 mem->start_addr = start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200672 mem->ram = ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300673 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200674
675 err = kvm_set_user_memory_region(s, mem);
676 if (err) {
677 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
678 strerror(-err));
679 abort();
680 }
681}
682
Avi Kivitya01672d2011-12-18 14:06:05 +0200683static void kvm_region_add(MemoryListener *listener,
684 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200685{
Avi Kivitya01672d2011-12-18 14:06:05 +0200686 kvm_set_phys_mem(section, true);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200687}
688
Avi Kivitya01672d2011-12-18 14:06:05 +0200689static void kvm_region_del(MemoryListener *listener,
690 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200691{
Avi Kivitya01672d2011-12-18 14:06:05 +0200692 kvm_set_phys_mem(section, false);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200693}
694
Avi Kivitya01672d2011-12-18 14:06:05 +0200695static void kvm_log_sync(MemoryListener *listener,
696 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200697{
Avi Kivitya01672d2011-12-18 14:06:05 +0200698 int r;
699
Avi Kivityffcde122011-12-19 13:18:13 +0200700 r = kvm_physical_sync_dirty_bitmap(section);
Avi Kivitya01672d2011-12-18 14:06:05 +0200701 if (r < 0) {
702 abort();
703 }
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200704}
705
Avi Kivitya01672d2011-12-18 14:06:05 +0200706static void kvm_log_global_start(struct MemoryListener *listener)
707{
708 int r;
709
710 r = kvm_set_migration_log(1);
711 assert(r >= 0);
712}
713
714static void kvm_log_global_stop(struct MemoryListener *listener)
715{
716 int r;
717
718 r = kvm_set_migration_log(0);
719 assert(r >= 0);
720}
721
Avi Kivity80a1ea32012-02-08 16:39:06 +0200722static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
723 bool match_data, uint64_t data, int fd)
724{
725 int r;
726
727 assert(match_data && section->size == 4);
728
729 r = kvm_set_ioeventfd_mmio_long(fd, section->offset_within_address_space,
730 data, true);
731 if (r < 0) {
732 abort();
733 }
734}
735
736static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
737 bool match_data, uint64_t data, int fd)
738{
739 int r;
740
741 r = kvm_set_ioeventfd_mmio_long(fd, section->offset_within_address_space,
742 data, false);
743 if (r < 0) {
744 abort();
745 }
746}
747
748static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
749 bool match_data, uint64_t data, int fd)
750{
751 int r;
752
753 assert(match_data && section->size == 2);
754
755 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
756 data, true);
757 if (r < 0) {
758 abort();
759 }
760}
761
762static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
763 bool match_data, uint64_t data, int fd)
764
765{
766 int r;
767
768 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
769 data, false);
770 if (r < 0) {
771 abort();
772 }
773}
774
775static void kvm_eventfd_add(MemoryListener *listener,
776 MemoryRegionSection *section,
777 bool match_data, uint64_t data, int fd)
778{
779 if (section->address_space == get_system_memory()) {
780 kvm_mem_ioeventfd_add(section, match_data, data, fd);
781 } else {
782 kvm_io_ioeventfd_add(section, match_data, data, fd);
783 }
784}
785
786static void kvm_eventfd_del(MemoryListener *listener,
787 MemoryRegionSection *section,
788 bool match_data, uint64_t data, int fd)
789{
790 if (section->address_space == get_system_memory()) {
791 kvm_mem_ioeventfd_del(section, match_data, data, fd);
792 } else {
793 kvm_io_ioeventfd_del(section, match_data, data, fd);
794 }
795}
796
Avi Kivitya01672d2011-12-18 14:06:05 +0200797static MemoryListener kvm_memory_listener = {
798 .region_add = kvm_region_add,
799 .region_del = kvm_region_del,
Anthony PERARDe5896b12011-02-07 12:19:23 +0100800 .log_start = kvm_log_start,
801 .log_stop = kvm_log_stop,
Avi Kivitya01672d2011-12-18 14:06:05 +0200802 .log_sync = kvm_log_sync,
803 .log_global_start = kvm_log_global_start,
804 .log_global_stop = kvm_log_global_stop,
Avi Kivity80a1ea32012-02-08 16:39:06 +0200805 .eventfd_add = kvm_eventfd_add,
806 .eventfd_del = kvm_eventfd_del,
Avi Kivity72e22d22012-02-08 15:05:50 +0200807 .priority = 10,
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200808};
809
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +0200810static void kvm_handle_interrupt(CPUState *env, int mask)
811{
812 env->interrupt_request |= mask;
813
814 if (!qemu_cpu_is_self(env)) {
815 qemu_cpu_kick(env);
816 }
817}
818
Jan Kiszka84b058d2011-10-15 11:49:47 +0200819int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
820{
821 struct kvm_irq_level event;
822 int ret;
823
824 assert(s->irqchip_in_kernel);
825
826 event.level = level;
827 event.irq = irq;
828 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
829 if (ret < 0) {
830 perror("kvm_set_irqchip_line");
831 abort();
832 }
833
834 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
835}
836
837#ifdef KVM_CAP_IRQ_ROUTING
838static void set_gsi(KVMState *s, unsigned int gsi)
839{
840 assert(gsi < s->max_gsi);
841
842 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
843}
844
845static void kvm_init_irq_routing(KVMState *s)
846{
847 int gsi_count;
848
849 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
850 if (gsi_count > 0) {
851 unsigned int gsi_bits, i;
852
853 /* Round up so we can search ints using ffs */
854 gsi_bits = (gsi_count + 31) / 32;
855 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
856 s->max_gsi = gsi_bits;
857
858 /* Mark any over-allocated bits as already in use */
859 for (i = gsi_count; i < gsi_bits; i++) {
860 set_gsi(s, i);
861 }
862 }
863
864 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
865 s->nr_allocated_irq_routes = 0;
866
867 kvm_arch_init_irq_routing(s);
868}
869
870static void kvm_add_routing_entry(KVMState *s,
871 struct kvm_irq_routing_entry *entry)
872{
873 struct kvm_irq_routing_entry *new;
874 int n, size;
875
876 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
877 n = s->nr_allocated_irq_routes * 2;
878 if (n < 64) {
879 n = 64;
880 }
881 size = sizeof(struct kvm_irq_routing);
882 size += n * sizeof(*new);
883 s->irq_routes = g_realloc(s->irq_routes, size);
884 s->nr_allocated_irq_routes = n;
885 }
886 n = s->irq_routes->nr++;
887 new = &s->irq_routes->entries[n];
888 memset(new, 0, sizeof(*new));
889 new->gsi = entry->gsi;
890 new->type = entry->type;
891 new->flags = entry->flags;
892 new->u = entry->u;
893
894 set_gsi(s, entry->gsi);
895}
896
897void kvm_irqchip_add_route(KVMState *s, int irq, int irqchip, int pin)
898{
899 struct kvm_irq_routing_entry e;
900
901 e.gsi = irq;
902 e.type = KVM_IRQ_ROUTING_IRQCHIP;
903 e.flags = 0;
904 e.u.irqchip.irqchip = irqchip;
905 e.u.irqchip.pin = pin;
906 kvm_add_routing_entry(s, &e);
907}
908
909int kvm_irqchip_commit_routes(KVMState *s)
910{
911 s->irq_routes->flags = 0;
912 return kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
913}
914
915#else /* !KVM_CAP_IRQ_ROUTING */
916
917static void kvm_init_irq_routing(KVMState *s)
918{
919}
920#endif /* !KVM_CAP_IRQ_ROUTING */
921
922static int kvm_irqchip_create(KVMState *s)
923{
924 QemuOptsList *list = qemu_find_opts("machine");
925 int ret;
926
927 if (QTAILQ_EMPTY(&list->head) ||
928 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
929 "kernel_irqchip", false) ||
930 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
931 return 0;
932 }
933
934 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
935 if (ret < 0) {
936 fprintf(stderr, "Create kernel irqchip failed\n");
937 return ret;
938 }
939
940 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
941 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
942 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
943 }
944 s->irqchip_in_kernel = 1;
945
946 kvm_init_irq_routing(s);
947
948 return 0;
949}
950
Jan Kiszkacad1e282011-01-21 21:48:16 +0100951int kvm_init(void)
aliguori05330442008-11-05 16:29:27 +0000952{
Jan Kiszka168ccc12009-06-07 11:30:25 +0200953 static const char upgrade_note[] =
954 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
955 "(see http://sourceforge.net/projects/kvm).\n";
aliguori05330442008-11-05 16:29:27 +0000956 KVMState *s;
Jan Kiszka94a8d392011-01-21 21:48:17 +0100957 const KVMCapabilityInfo *missing_cap;
aliguori05330442008-11-05 16:29:27 +0000958 int ret;
959 int i;
960
Anthony Liguori7267c092011-08-20 22:09:37 -0500961 s = g_malloc0(sizeof(KVMState));
aliguori05330442008-11-05 16:29:27 +0000962
aliguorie22a25c2009-03-12 20:12:48 +0000963#ifdef KVM_CAP_SET_GUEST_DEBUG
Blue Swirl72cf2d42009-09-12 07:36:22 +0000964 QTAILQ_INIT(&s->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +0000965#endif
Jan Kiszkaa426e122011-01-04 09:32:13 +0100966 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
aliguori05330442008-11-05 16:29:27 +0000967 s->slots[i].slot = i;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100968 }
aliguori05330442008-11-05 16:29:27 +0000969 s->vmfd = -1;
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100970 s->fd = qemu_open("/dev/kvm", O_RDWR);
aliguori05330442008-11-05 16:29:27 +0000971 if (s->fd == -1) {
972 fprintf(stderr, "Could not access KVM kernel module: %m\n");
973 ret = -errno;
974 goto err;
975 }
976
977 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
978 if (ret < KVM_API_VERSION) {
Jan Kiszkaa426e122011-01-04 09:32:13 +0100979 if (ret > 0) {
aliguori05330442008-11-05 16:29:27 +0000980 ret = -EINVAL;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100981 }
aliguori05330442008-11-05 16:29:27 +0000982 fprintf(stderr, "kvm version too old\n");
983 goto err;
984 }
985
986 if (ret > KVM_API_VERSION) {
987 ret = -EINVAL;
988 fprintf(stderr, "kvm version not supported\n");
989 goto err;
990 }
991
992 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
Alexander Graf0104dca2010-04-01 18:42:37 +0200993 if (s->vmfd < 0) {
994#ifdef TARGET_S390X
995 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
996 "your host kernel command line\n");
997#endif
Xu He Jiedb9eae12011-10-27 10:15:13 +0800998 ret = s->vmfd;
aliguori05330442008-11-05 16:29:27 +0000999 goto err;
Alexander Graf0104dca2010-04-01 18:42:37 +02001000 }
aliguori05330442008-11-05 16:29:27 +00001001
Jan Kiszka94a8d392011-01-21 21:48:17 +01001002 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1003 if (!missing_cap) {
1004 missing_cap =
1005 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1006 }
1007 if (missing_cap) {
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001008 ret = -EINVAL;
Jan Kiszka94a8d392011-01-21 21:48:17 +01001009 fprintf(stderr, "kvm does not support %s\n%s",
1010 missing_cap->name, upgrade_note);
aliguori05330442008-11-05 16:29:27 +00001011 goto err;
1012 }
1013
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001014 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
aliguorif65ed4c2008-12-09 20:09:57 +00001015
Jan Kiszkae69917e2009-05-01 20:42:15 +02001016 s->broken_set_mem_region = 1;
Lai Jiangshan14a09512010-12-10 15:52:36 +08001017 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
Jan Kiszkae69917e2009-05-01 20:42:15 +02001018 if (ret > 0) {
1019 s->broken_set_mem_region = 0;
1020 }
Jan Kiszkae69917e2009-05-01 20:42:15 +02001021
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001022#ifdef KVM_CAP_VCPU_EVENTS
1023 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1024#endif
1025
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001026 s->robust_singlestep =
1027 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001028
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001029#ifdef KVM_CAP_DEBUGREGS
1030 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1031#endif
1032
Sheng Yangf1665b22010-06-17 17:53:07 +08001033#ifdef KVM_CAP_XSAVE
1034 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1035#endif
1036
Sheng Yangf1665b22010-06-17 17:53:07 +08001037#ifdef KVM_CAP_XCRS
1038 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1039#endif
1040
Jan Kiszkacad1e282011-01-21 21:48:16 +01001041 ret = kvm_arch_init(s);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001042 if (ret < 0) {
aliguori05330442008-11-05 16:29:27 +00001043 goto err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001044 }
aliguori05330442008-11-05 16:29:27 +00001045
Jan Kiszka84b058d2011-10-15 11:49:47 +02001046 ret = kvm_irqchip_create(s);
1047 if (ret < 0) {
1048 goto err;
1049 }
1050
aliguori05330442008-11-05 16:29:27 +00001051 kvm_state = s;
Avi Kivity7376e582012-02-08 21:05:17 +02001052 memory_listener_register(&kvm_memory_listener, NULL);
aliguori05330442008-11-05 16:29:27 +00001053
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001054 s->many_ioeventfds = kvm_check_many_ioeventfds();
1055
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +02001056 cpu_interrupt_handler = kvm_handle_interrupt;
1057
aliguori05330442008-11-05 16:29:27 +00001058 return 0;
1059
1060err:
1061 if (s) {
Xu He Jiedb9eae12011-10-27 10:15:13 +08001062 if (s->vmfd >= 0) {
aliguori05330442008-11-05 16:29:27 +00001063 close(s->vmfd);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001064 }
1065 if (s->fd != -1) {
aliguori05330442008-11-05 16:29:27 +00001066 close(s->fd);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001067 }
aliguori05330442008-11-05 16:29:27 +00001068 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001069 g_free(s);
aliguori05330442008-11-05 16:29:27 +00001070
1071 return ret;
1072}
1073
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001074static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1075 uint32_t count)
aliguori05330442008-11-05 16:29:27 +00001076{
1077 int i;
1078 uint8_t *ptr = data;
1079
1080 for (i = 0; i < count; i++) {
1081 if (direction == KVM_EXIT_IO_IN) {
1082 switch (size) {
1083 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001084 stb_p(ptr, cpu_inb(port));
aliguori05330442008-11-05 16:29:27 +00001085 break;
1086 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001087 stw_p(ptr, cpu_inw(port));
aliguori05330442008-11-05 16:29:27 +00001088 break;
1089 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001090 stl_p(ptr, cpu_inl(port));
aliguori05330442008-11-05 16:29:27 +00001091 break;
1092 }
1093 } else {
1094 switch (size) {
1095 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001096 cpu_outb(port, ldub_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001097 break;
1098 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001099 cpu_outw(port, lduw_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001100 break;
1101 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001102 cpu_outl(port, ldl_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001103 break;
1104 }
1105 }
1106
1107 ptr += size;
1108 }
aliguori05330442008-11-05 16:29:27 +00001109}
1110
Jan Kiszka73aaec42011-01-21 21:48:06 +01001111static int kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001112{
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001113 fprintf(stderr, "KVM internal error.");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001114 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1115 int i;
1116
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001117 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001118 for (i = 0; i < run->internal.ndata; ++i) {
1119 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1120 i, (uint64_t)run->internal.data[i]);
1121 }
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001122 } else {
1123 fprintf(stderr, "\n");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001124 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001125 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1126 fprintf(stderr, "emulation failure\n");
Jan Kiszkaa426e122011-01-04 09:32:13 +01001127 if (!kvm_arch_stop_on_emulation_error(env)) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001128 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001129 return EXCP_INTERRUPT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001130 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001131 }
1132 /* FIXME: Should trigger a qmp message to let management know
1133 * something went wrong.
1134 */
Jan Kiszka73aaec42011-01-21 21:48:06 +01001135 return -1;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001136}
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001137
Sheng Yang62a27442010-01-26 19:21:16 +08001138void kvm_flush_coalesced_mmio_buffer(void)
aliguorif65ed4c2008-12-09 20:09:57 +00001139{
aliguorif65ed4c2008-12-09 20:09:57 +00001140 KVMState *s = kvm_state;
Avi Kivity1cae88b2011-10-18 19:43:12 +02001141
1142 if (s->coalesced_flush_in_progress) {
1143 return;
1144 }
1145
1146 s->coalesced_flush_in_progress = true;
1147
Sheng Yang62a27442010-01-26 19:21:16 +08001148 if (s->coalesced_mmio_ring) {
1149 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
aliguorif65ed4c2008-12-09 20:09:57 +00001150 while (ring->first != ring->last) {
1151 struct kvm_coalesced_mmio *ent;
1152
1153 ent = &ring->coalesced_mmio[ring->first];
1154
1155 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
Marcelo Tosatti85199472010-02-22 13:57:54 -03001156 smp_wmb();
aliguorif65ed4c2008-12-09 20:09:57 +00001157 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1158 }
1159 }
Avi Kivity1cae88b2011-10-18 19:43:12 +02001160
1161 s->coalesced_flush_in_progress = false;
aliguorif65ed4c2008-12-09 20:09:57 +00001162}
1163
Jan Kiszka2705d562010-05-04 09:45:23 -03001164static void do_kvm_cpu_synchronize_state(void *_env)
Avi Kivity4c0960c2009-08-17 23:19:53 +03001165{
Jan Kiszka2705d562010-05-04 09:45:23 -03001166 CPUState *env = _env;
1167
Jan Kiszka9ded2742010-02-03 21:17:05 +01001168 if (!env->kvm_vcpu_dirty) {
Avi Kivity4c0960c2009-08-17 23:19:53 +03001169 kvm_arch_get_registers(env);
Jan Kiszka9ded2742010-02-03 21:17:05 +01001170 env->kvm_vcpu_dirty = 1;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001171 }
1172}
1173
Jan Kiszka2705d562010-05-04 09:45:23 -03001174void kvm_cpu_synchronize_state(CPUState *env)
1175{
Jan Kiszkaa426e122011-01-04 09:32:13 +01001176 if (!env->kvm_vcpu_dirty) {
Jan Kiszka2705d562010-05-04 09:45:23 -03001177 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001178 }
Jan Kiszka2705d562010-05-04 09:45:23 -03001179}
1180
Jan Kiszkaea375f92010-03-01 19:10:30 +01001181void kvm_cpu_synchronize_post_reset(CPUState *env)
1182{
1183 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1184 env->kvm_vcpu_dirty = 0;
1185}
1186
1187void kvm_cpu_synchronize_post_init(CPUState *env)
1188{
1189 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1190 env->kvm_vcpu_dirty = 0;
1191}
1192
aliguori05330442008-11-05 16:29:27 +00001193int kvm_cpu_exec(CPUState *env)
1194{
1195 struct kvm_run *run = env->kvm_run;
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001196 int ret, run_ret;
aliguori05330442008-11-05 16:29:27 +00001197
Blue Swirl8c0d5772010-04-18 14:22:14 +00001198 DPRINTF("kvm_cpu_exec()\n");
aliguori05330442008-11-05 16:29:27 +00001199
Jan Kiszka99036862011-03-02 08:56:13 +01001200 if (kvm_arch_process_async_events(env)) {
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001201 env->exit_request = 0;
Jan Kiszka6792a572011-02-07 12:19:18 +01001202 return EXCP_HLT;
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001203 }
1204
Jan Kiszka6792a572011-02-07 12:19:18 +01001205 cpu_single_env = env;
1206
aliguori05330442008-11-05 16:29:27 +00001207 do {
Jan Kiszka9ded2742010-02-03 21:17:05 +01001208 if (env->kvm_vcpu_dirty) {
Jan Kiszkaea375f92010-03-01 19:10:30 +01001209 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
Jan Kiszka9ded2742010-02-03 21:17:05 +01001210 env->kvm_vcpu_dirty = 0;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001211 }
1212
Jan Kiszka8c14c172009-05-30 10:01:45 +02001213 kvm_arch_pre_run(env, run);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001214 if (env->exit_request) {
1215 DPRINTF("interrupt exit requested\n");
1216 /*
1217 * KVM requires us to reenter the kernel after IO exits to complete
1218 * instruction emulation. This self-signal will ensure that we
1219 * leave ASAP again.
1220 */
1221 qemu_cpu_kick_self();
1222 }
Marcelo Tosatti273faf12010-05-04 09:45:19 -03001223 cpu_single_env = NULL;
Glauber Costad549db52009-10-07 16:38:03 -03001224 qemu_mutex_unlock_iothread();
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001225
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001226 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001227
Glauber Costad549db52009-10-07 16:38:03 -03001228 qemu_mutex_lock_iothread();
Marcelo Tosatti273faf12010-05-04 09:45:19 -03001229 cpu_single_env = env;
aliguori05330442008-11-05 16:29:27 +00001230 kvm_arch_post_run(env, run);
1231
Jan Kiszkab0c883b2011-01-21 21:48:19 +01001232 kvm_flush_coalesced_mmio_buffer();
1233
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001234 if (run_ret < 0) {
Jan Kiszkadc77d342011-03-15 12:26:26 +01001235 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1236 DPRINTF("io window exit\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001237 ret = EXCP_INTERRUPT;
Jan Kiszkadc77d342011-03-15 12:26:26 +01001238 break;
1239 }
Michael Ellerman7b011fb2011-12-16 11:20:20 +11001240 fprintf(stderr, "error: kvm run failed %s\n",
1241 strerror(-run_ret));
aliguori05330442008-11-05 16:29:27 +00001242 abort();
1243 }
1244
aliguori05330442008-11-05 16:29:27 +00001245 switch (run->exit_reason) {
1246 case KVM_EXIT_IO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001247 DPRINTF("handle_io\n");
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001248 kvm_handle_io(run->io.port,
1249 (uint8_t *)run + run->io.data_offset,
1250 run->io.direction,
1251 run->io.size,
1252 run->io.count);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001253 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001254 break;
1255 case KVM_EXIT_MMIO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001256 DPRINTF("handle_mmio\n");
aliguori05330442008-11-05 16:29:27 +00001257 cpu_physical_memory_rw(run->mmio.phys_addr,
1258 run->mmio.data,
1259 run->mmio.len,
1260 run->mmio.is_write);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001261 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001262 break;
1263 case KVM_EXIT_IRQ_WINDOW_OPEN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001264 DPRINTF("irq_window_open\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001265 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001266 break;
1267 case KVM_EXIT_SHUTDOWN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001268 DPRINTF("shutdown\n");
aliguori05330442008-11-05 16:29:27 +00001269 qemu_system_reset_request();
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001270 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001271 break;
1272 case KVM_EXIT_UNKNOWN:
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001273 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1274 (uint64_t)run->hw.hardware_exit_reason);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001275 ret = -1;
aliguori05330442008-11-05 16:29:27 +00001276 break;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001277 case KVM_EXIT_INTERNAL_ERROR:
Jan Kiszka73aaec42011-01-21 21:48:06 +01001278 ret = kvm_handle_internal_error(env, run);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001279 break;
aliguori05330442008-11-05 16:29:27 +00001280 default:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001281 DPRINTF("kvm_arch_handle_exit\n");
aliguori05330442008-11-05 16:29:27 +00001282 ret = kvm_arch_handle_exit(env, run);
1283 break;
1284 }
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001285 } while (ret == 0);
aliguori05330442008-11-05 16:29:27 +00001286
Jan Kiszka73aaec42011-01-21 21:48:06 +01001287 if (ret < 0) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001288 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001289 vm_stop(RUN_STATE_INTERNAL_ERROR);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001290 }
aliguoribecfc392008-11-10 15:55:14 +00001291
Jan Kiszka6792a572011-02-07 12:19:18 +01001292 env->exit_request = 0;
1293 cpu_single_env = NULL;
aliguori05330442008-11-05 16:29:27 +00001294 return ret;
1295}
1296
aliguori984b5182008-11-13 19:21:00 +00001297int kvm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001298{
1299 int ret;
aliguori984b5182008-11-13 19:21:00 +00001300 void *arg;
1301 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001302
aliguori984b5182008-11-13 19:21:00 +00001303 va_start(ap, type);
1304 arg = va_arg(ap, void *);
1305 va_end(ap);
1306
1307 ret = ioctl(s->fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001308 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001309 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001310 }
aliguori05330442008-11-05 16:29:27 +00001311 return ret;
1312}
1313
aliguori984b5182008-11-13 19:21:00 +00001314int kvm_vm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001315{
1316 int ret;
aliguori984b5182008-11-13 19:21:00 +00001317 void *arg;
1318 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001319
aliguori984b5182008-11-13 19:21:00 +00001320 va_start(ap, type);
1321 arg = va_arg(ap, void *);
1322 va_end(ap);
1323
1324 ret = ioctl(s->vmfd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001325 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001326 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001327 }
aliguori05330442008-11-05 16:29:27 +00001328 return ret;
1329}
1330
aliguori984b5182008-11-13 19:21:00 +00001331int kvm_vcpu_ioctl(CPUState *env, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001332{
1333 int ret;
aliguori984b5182008-11-13 19:21:00 +00001334 void *arg;
1335 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001336
aliguori984b5182008-11-13 19:21:00 +00001337 va_start(ap, type);
1338 arg = va_arg(ap, void *);
1339 va_end(ap);
1340
1341 ret = ioctl(env->kvm_fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001342 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001343 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001344 }
aliguori05330442008-11-05 16:29:27 +00001345 return ret;
1346}
aliguoribd322082008-12-04 20:33:06 +00001347
1348int kvm_has_sync_mmu(void)
1349{
Jan Kiszka94a8d392011-01-21 21:48:17 +01001350 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
aliguoribd322082008-12-04 20:33:06 +00001351}
aliguorie22a25c2009-03-12 20:12:48 +00001352
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001353int kvm_has_vcpu_events(void)
1354{
1355 return kvm_state->vcpu_events;
1356}
1357
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001358int kvm_has_robust_singlestep(void)
1359{
1360 return kvm_state->robust_singlestep;
1361}
1362
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001363int kvm_has_debugregs(void)
1364{
1365 return kvm_state->debugregs;
1366}
1367
Sheng Yangf1665b22010-06-17 17:53:07 +08001368int kvm_has_xsave(void)
1369{
1370 return kvm_state->xsave;
1371}
1372
1373int kvm_has_xcrs(void)
1374{
1375 return kvm_state->xcrs;
1376}
1377
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001378int kvm_has_many_ioeventfds(void)
1379{
1380 if (!kvm_enabled()) {
1381 return 0;
1382 }
1383 return kvm_state->many_ioeventfds;
1384}
1385
Jan Kiszka84b058d2011-10-15 11:49:47 +02001386int kvm_has_gsi_routing(void)
1387{
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001388#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszka84b058d2011-10-15 11:49:47 +02001389 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001390#else
1391 return false;
1392#endif
Jan Kiszka84b058d2011-10-15 11:49:47 +02001393}
1394
Jan Kiszka9b5b76d2011-10-15 14:08:26 +02001395int kvm_allows_irq0_override(void)
1396{
1397 return !kvm_enabled() || !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1398}
1399
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001400void kvm_setup_guest_memory(void *start, size_t size)
1401{
1402 if (!kvm_has_sync_mmu()) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001403 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001404
1405 if (ret) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001406 perror("qemu_madvise");
1407 fprintf(stderr,
1408 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001409 exit(1);
1410 }
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001411 }
1412}
1413
aliguorie22a25c2009-03-12 20:12:48 +00001414#ifdef KVM_CAP_SET_GUEST_DEBUG
1415struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1416 target_ulong pc)
1417{
1418 struct kvm_sw_breakpoint *bp;
1419
Blue Swirl72cf2d42009-09-12 07:36:22 +00001420 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001421 if (bp->pc == pc) {
aliguorie22a25c2009-03-12 20:12:48 +00001422 return bp;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001423 }
aliguorie22a25c2009-03-12 20:12:48 +00001424 }
1425 return NULL;
1426}
1427
1428int kvm_sw_breakpoints_active(CPUState *env)
1429{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001430 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +00001431}
1432
Glauber Costa452e4752009-07-16 17:55:28 -04001433struct kvm_set_guest_debug_data {
1434 struct kvm_guest_debug dbg;
1435 CPUState *env;
1436 int err;
1437};
1438
1439static void kvm_invoke_set_guest_debug(void *data)
1440{
1441 struct kvm_set_guest_debug_data *dbg_data = data;
Jan Kiszkab3807722009-09-17 20:05:58 +02001442 CPUState *env = dbg_data->env;
1443
Jan Kiszkab3807722009-09-17 20:05:58 +02001444 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001445}
1446
aliguorie22a25c2009-03-12 20:12:48 +00001447int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1448{
Glauber Costa452e4752009-07-16 17:55:28 -04001449 struct kvm_set_guest_debug_data data;
aliguorie22a25c2009-03-12 20:12:48 +00001450
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001451 data.dbg.control = reinject_trap;
aliguorie22a25c2009-03-12 20:12:48 +00001452
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001453 if (env->singlestep_enabled) {
1454 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1455 }
Glauber Costa452e4752009-07-16 17:55:28 -04001456 kvm_arch_update_guest_debug(env, &data.dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001457 data.env = env;
aliguorie22a25c2009-03-12 20:12:48 +00001458
Jan Kiszkabe41cbe2010-05-20 00:28:45 +02001459 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
Glauber Costa452e4752009-07-16 17:55:28 -04001460 return data.err;
aliguorie22a25c2009-03-12 20:12:48 +00001461}
1462
1463int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1464 target_ulong len, int type)
1465{
1466 struct kvm_sw_breakpoint *bp;
1467 CPUState *env;
1468 int err;
1469
1470 if (type == GDB_BREAKPOINT_SW) {
1471 bp = kvm_find_sw_breakpoint(current_env, addr);
1472 if (bp) {
1473 bp->use_count++;
1474 return 0;
1475 }
1476
Anthony Liguori7267c092011-08-20 22:09:37 -05001477 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
Jan Kiszkaa426e122011-01-04 09:32:13 +01001478 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001479 return -ENOMEM;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001480 }
aliguorie22a25c2009-03-12 20:12:48 +00001481
1482 bp->pc = addr;
1483 bp->use_count = 1;
1484 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1485 if (err) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001486 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001487 return err;
1488 }
1489
Blue Swirl72cf2d42009-09-12 07:36:22 +00001490 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
aliguorie22a25c2009-03-12 20:12:48 +00001491 bp, entry);
1492 } else {
1493 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001494 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001495 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001496 }
aliguorie22a25c2009-03-12 20:12:48 +00001497 }
1498
1499 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1500 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001501 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001502 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001503 }
aliguorie22a25c2009-03-12 20:12:48 +00001504 }
1505 return 0;
1506}
1507
1508int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1509 target_ulong len, int type)
1510{
1511 struct kvm_sw_breakpoint *bp;
1512 CPUState *env;
1513 int err;
1514
1515 if (type == GDB_BREAKPOINT_SW) {
1516 bp = kvm_find_sw_breakpoint(current_env, addr);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001517 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001518 return -ENOENT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001519 }
aliguorie22a25c2009-03-12 20:12:48 +00001520
1521 if (bp->use_count > 1) {
1522 bp->use_count--;
1523 return 0;
1524 }
1525
1526 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001527 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001528 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001529 }
aliguorie22a25c2009-03-12 20:12:48 +00001530
Blue Swirl72cf2d42009-09-12 07:36:22 +00001531 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001532 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001533 } else {
1534 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001535 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001536 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001537 }
aliguorie22a25c2009-03-12 20:12:48 +00001538 }
1539
1540 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1541 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001542 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001543 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001544 }
aliguorie22a25c2009-03-12 20:12:48 +00001545 }
1546 return 0;
1547}
1548
1549void kvm_remove_all_breakpoints(CPUState *current_env)
1550{
1551 struct kvm_sw_breakpoint *bp, *next;
1552 KVMState *s = current_env->kvm_state;
1553 CPUState *env;
1554
Blue Swirl72cf2d42009-09-12 07:36:22 +00001555 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
aliguorie22a25c2009-03-12 20:12:48 +00001556 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1557 /* Try harder to find a CPU that currently sees the breakpoint. */
1558 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001559 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
aliguorie22a25c2009-03-12 20:12:48 +00001560 break;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001561 }
aliguorie22a25c2009-03-12 20:12:48 +00001562 }
1563 }
1564 }
1565 kvm_arch_remove_all_hw_breakpoints();
1566
Jan Kiszkaa426e122011-01-04 09:32:13 +01001567 for (env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorie22a25c2009-03-12 20:12:48 +00001568 kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001569 }
aliguorie22a25c2009-03-12 20:12:48 +00001570}
1571
1572#else /* !KVM_CAP_SET_GUEST_DEBUG */
1573
1574int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1575{
1576 return -EINVAL;
1577}
1578
1579int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1580 target_ulong len, int type)
1581{
1582 return -EINVAL;
1583}
1584
1585int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1586 target_ulong len, int type)
1587{
1588 return -EINVAL;
1589}
1590
1591void kvm_remove_all_breakpoints(CPUState *current_env)
1592{
1593}
1594#endif /* !KVM_CAP_SET_GUEST_DEBUG */
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001595
1596int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset)
1597{
1598 struct kvm_signal_mask *sigmask;
1599 int r;
1600
Jan Kiszkaa426e122011-01-04 09:32:13 +01001601 if (!sigset) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001602 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001603 }
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001604
Anthony Liguori7267c092011-08-20 22:09:37 -05001605 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001606
1607 sigmask->len = 8;
1608 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1609 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
Anthony Liguori7267c092011-08-20 22:09:37 -05001610 g_free(sigmask);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001611
1612 return r;
1613}
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001614
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001615int kvm_set_ioeventfd_mmio_long(int fd, uint32_t addr, uint32_t val, bool assign)
1616{
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001617 int ret;
1618 struct kvm_ioeventfd iofd;
1619
1620 iofd.datamatch = val;
1621 iofd.addr = addr;
1622 iofd.len = 4;
1623 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1624 iofd.fd = fd;
1625
1626 if (!kvm_enabled()) {
1627 return -ENOSYS;
1628 }
1629
1630 if (!assign) {
1631 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1632 }
1633
1634 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1635
1636 if (ret < 0) {
1637 return -errno;
1638 }
1639
1640 return 0;
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001641}
1642
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001643int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1644{
1645 struct kvm_ioeventfd kick = {
1646 .datamatch = val,
1647 .addr = addr,
1648 .len = 2,
1649 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1650 .fd = fd,
1651 };
1652 int r;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001653 if (!kvm_enabled()) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001654 return -ENOSYS;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001655 }
1656 if (!assign) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001657 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001658 }
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001659 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001660 if (r < 0) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001661 return r;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001662 }
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001663 return 0;
Paolo Bonzini98c85732010-04-19 18:59:30 +00001664}
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01001665
1666int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr)
1667{
1668 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1669}
1670
1671int kvm_on_sigbus(int code, void *addr)
1672{
1673 return kvm_arch_on_sigbus(code, addr);
1674}