blob: c07823df988766626466755783baf153fbe1421b [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 Kivity50c1e142012-02-08 21:36:02 +0200683static void kvm_begin(MemoryListener *listener)
684{
685}
686
687static void kvm_commit(MemoryListener *listener)
688{
689}
690
Avi Kivitya01672d2011-12-18 14:06:05 +0200691static void kvm_region_add(MemoryListener *listener,
692 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200693{
Avi Kivitya01672d2011-12-18 14:06:05 +0200694 kvm_set_phys_mem(section, true);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200695}
696
Avi Kivitya01672d2011-12-18 14:06:05 +0200697static void kvm_region_del(MemoryListener *listener,
698 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200699{
Avi Kivitya01672d2011-12-18 14:06:05 +0200700 kvm_set_phys_mem(section, false);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200701}
702
Avi Kivity50c1e142012-02-08 21:36:02 +0200703static void kvm_region_nop(MemoryListener *listener,
704 MemoryRegionSection *section)
705{
706}
707
Avi Kivitya01672d2011-12-18 14:06:05 +0200708static void kvm_log_sync(MemoryListener *listener,
709 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200710{
Avi Kivitya01672d2011-12-18 14:06:05 +0200711 int r;
712
Avi Kivityffcde122011-12-19 13:18:13 +0200713 r = kvm_physical_sync_dirty_bitmap(section);
Avi Kivitya01672d2011-12-18 14:06:05 +0200714 if (r < 0) {
715 abort();
716 }
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200717}
718
Avi Kivitya01672d2011-12-18 14:06:05 +0200719static void kvm_log_global_start(struct MemoryListener *listener)
720{
721 int r;
722
723 r = kvm_set_migration_log(1);
724 assert(r >= 0);
725}
726
727static void kvm_log_global_stop(struct MemoryListener *listener)
728{
729 int r;
730
731 r = kvm_set_migration_log(0);
732 assert(r >= 0);
733}
734
Avi Kivity80a1ea32012-02-08 16:39:06 +0200735static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
736 bool match_data, uint64_t data, int fd)
737{
738 int r;
739
740 assert(match_data && section->size == 4);
741
742 r = kvm_set_ioeventfd_mmio_long(fd, section->offset_within_address_space,
743 data, true);
744 if (r < 0) {
745 abort();
746 }
747}
748
749static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
750 bool match_data, uint64_t data, int fd)
751{
752 int r;
753
754 r = kvm_set_ioeventfd_mmio_long(fd, section->offset_within_address_space,
755 data, false);
756 if (r < 0) {
757 abort();
758 }
759}
760
761static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
762 bool match_data, uint64_t data, int fd)
763{
764 int r;
765
766 assert(match_data && section->size == 2);
767
768 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
769 data, true);
770 if (r < 0) {
771 abort();
772 }
773}
774
775static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
776 bool match_data, uint64_t data, int fd)
777
778{
779 int r;
780
781 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
782 data, false);
783 if (r < 0) {
784 abort();
785 }
786}
787
788static void kvm_eventfd_add(MemoryListener *listener,
789 MemoryRegionSection *section,
790 bool match_data, uint64_t data, int fd)
791{
792 if (section->address_space == get_system_memory()) {
793 kvm_mem_ioeventfd_add(section, match_data, data, fd);
794 } else {
795 kvm_io_ioeventfd_add(section, match_data, data, fd);
796 }
797}
798
799static void kvm_eventfd_del(MemoryListener *listener,
800 MemoryRegionSection *section,
801 bool match_data, uint64_t data, int fd)
802{
803 if (section->address_space == get_system_memory()) {
804 kvm_mem_ioeventfd_del(section, match_data, data, fd);
805 } else {
806 kvm_io_ioeventfd_del(section, match_data, data, fd);
807 }
808}
809
Avi Kivitya01672d2011-12-18 14:06:05 +0200810static MemoryListener kvm_memory_listener = {
Avi Kivity50c1e142012-02-08 21:36:02 +0200811 .begin = kvm_begin,
812 .commit = kvm_commit,
Avi Kivitya01672d2011-12-18 14:06:05 +0200813 .region_add = kvm_region_add,
814 .region_del = kvm_region_del,
Avi Kivity50c1e142012-02-08 21:36:02 +0200815 .region_nop = kvm_region_nop,
Anthony PERARDe5896b12011-02-07 12:19:23 +0100816 .log_start = kvm_log_start,
817 .log_stop = kvm_log_stop,
Avi Kivitya01672d2011-12-18 14:06:05 +0200818 .log_sync = kvm_log_sync,
819 .log_global_start = kvm_log_global_start,
820 .log_global_stop = kvm_log_global_stop,
Avi Kivity80a1ea32012-02-08 16:39:06 +0200821 .eventfd_add = kvm_eventfd_add,
822 .eventfd_del = kvm_eventfd_del,
Avi Kivity72e22d22012-02-08 15:05:50 +0200823 .priority = 10,
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200824};
825
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +0200826static void kvm_handle_interrupt(CPUState *env, int mask)
827{
828 env->interrupt_request |= mask;
829
830 if (!qemu_cpu_is_self(env)) {
831 qemu_cpu_kick(env);
832 }
833}
834
Jan Kiszka84b058d2011-10-15 11:49:47 +0200835int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
836{
837 struct kvm_irq_level event;
838 int ret;
839
840 assert(s->irqchip_in_kernel);
841
842 event.level = level;
843 event.irq = irq;
844 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
845 if (ret < 0) {
846 perror("kvm_set_irqchip_line");
847 abort();
848 }
849
850 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
851}
852
853#ifdef KVM_CAP_IRQ_ROUTING
854static void set_gsi(KVMState *s, unsigned int gsi)
855{
856 assert(gsi < s->max_gsi);
857
858 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
859}
860
861static void kvm_init_irq_routing(KVMState *s)
862{
863 int gsi_count;
864
865 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
866 if (gsi_count > 0) {
867 unsigned int gsi_bits, i;
868
869 /* Round up so we can search ints using ffs */
870 gsi_bits = (gsi_count + 31) / 32;
871 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
872 s->max_gsi = gsi_bits;
873
874 /* Mark any over-allocated bits as already in use */
875 for (i = gsi_count; i < gsi_bits; i++) {
876 set_gsi(s, i);
877 }
878 }
879
880 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
881 s->nr_allocated_irq_routes = 0;
882
883 kvm_arch_init_irq_routing(s);
884}
885
886static void kvm_add_routing_entry(KVMState *s,
887 struct kvm_irq_routing_entry *entry)
888{
889 struct kvm_irq_routing_entry *new;
890 int n, size;
891
892 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
893 n = s->nr_allocated_irq_routes * 2;
894 if (n < 64) {
895 n = 64;
896 }
897 size = sizeof(struct kvm_irq_routing);
898 size += n * sizeof(*new);
899 s->irq_routes = g_realloc(s->irq_routes, size);
900 s->nr_allocated_irq_routes = n;
901 }
902 n = s->irq_routes->nr++;
903 new = &s->irq_routes->entries[n];
904 memset(new, 0, sizeof(*new));
905 new->gsi = entry->gsi;
906 new->type = entry->type;
907 new->flags = entry->flags;
908 new->u = entry->u;
909
910 set_gsi(s, entry->gsi);
911}
912
913void kvm_irqchip_add_route(KVMState *s, int irq, int irqchip, int pin)
914{
915 struct kvm_irq_routing_entry e;
916
917 e.gsi = irq;
918 e.type = KVM_IRQ_ROUTING_IRQCHIP;
919 e.flags = 0;
920 e.u.irqchip.irqchip = irqchip;
921 e.u.irqchip.pin = pin;
922 kvm_add_routing_entry(s, &e);
923}
924
925int kvm_irqchip_commit_routes(KVMState *s)
926{
927 s->irq_routes->flags = 0;
928 return kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
929}
930
931#else /* !KVM_CAP_IRQ_ROUTING */
932
933static void kvm_init_irq_routing(KVMState *s)
934{
935}
936#endif /* !KVM_CAP_IRQ_ROUTING */
937
938static int kvm_irqchip_create(KVMState *s)
939{
940 QemuOptsList *list = qemu_find_opts("machine");
941 int ret;
942
943 if (QTAILQ_EMPTY(&list->head) ||
944 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
945 "kernel_irqchip", false) ||
946 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
947 return 0;
948 }
949
950 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
951 if (ret < 0) {
952 fprintf(stderr, "Create kernel irqchip failed\n");
953 return ret;
954 }
955
956 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
957 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
958 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
959 }
960 s->irqchip_in_kernel = 1;
961
962 kvm_init_irq_routing(s);
963
964 return 0;
965}
966
Jan Kiszkacad1e282011-01-21 21:48:16 +0100967int kvm_init(void)
aliguori05330442008-11-05 16:29:27 +0000968{
Jan Kiszka168ccc12009-06-07 11:30:25 +0200969 static const char upgrade_note[] =
970 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
971 "(see http://sourceforge.net/projects/kvm).\n";
aliguori05330442008-11-05 16:29:27 +0000972 KVMState *s;
Jan Kiszka94a8d392011-01-21 21:48:17 +0100973 const KVMCapabilityInfo *missing_cap;
aliguori05330442008-11-05 16:29:27 +0000974 int ret;
975 int i;
976
Anthony Liguori7267c092011-08-20 22:09:37 -0500977 s = g_malloc0(sizeof(KVMState));
aliguori05330442008-11-05 16:29:27 +0000978
aliguorie22a25c2009-03-12 20:12:48 +0000979#ifdef KVM_CAP_SET_GUEST_DEBUG
Blue Swirl72cf2d42009-09-12 07:36:22 +0000980 QTAILQ_INIT(&s->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +0000981#endif
Jan Kiszkaa426e122011-01-04 09:32:13 +0100982 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
aliguori05330442008-11-05 16:29:27 +0000983 s->slots[i].slot = i;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100984 }
aliguori05330442008-11-05 16:29:27 +0000985 s->vmfd = -1;
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100986 s->fd = qemu_open("/dev/kvm", O_RDWR);
aliguori05330442008-11-05 16:29:27 +0000987 if (s->fd == -1) {
988 fprintf(stderr, "Could not access KVM kernel module: %m\n");
989 ret = -errno;
990 goto err;
991 }
992
993 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
994 if (ret < KVM_API_VERSION) {
Jan Kiszkaa426e122011-01-04 09:32:13 +0100995 if (ret > 0) {
aliguori05330442008-11-05 16:29:27 +0000996 ret = -EINVAL;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100997 }
aliguori05330442008-11-05 16:29:27 +0000998 fprintf(stderr, "kvm version too old\n");
999 goto err;
1000 }
1001
1002 if (ret > KVM_API_VERSION) {
1003 ret = -EINVAL;
1004 fprintf(stderr, "kvm version not supported\n");
1005 goto err;
1006 }
1007
1008 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
Alexander Graf0104dca2010-04-01 18:42:37 +02001009 if (s->vmfd < 0) {
1010#ifdef TARGET_S390X
1011 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1012 "your host kernel command line\n");
1013#endif
Xu He Jiedb9eae12011-10-27 10:15:13 +08001014 ret = s->vmfd;
aliguori05330442008-11-05 16:29:27 +00001015 goto err;
Alexander Graf0104dca2010-04-01 18:42:37 +02001016 }
aliguori05330442008-11-05 16:29:27 +00001017
Jan Kiszka94a8d392011-01-21 21:48:17 +01001018 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1019 if (!missing_cap) {
1020 missing_cap =
1021 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1022 }
1023 if (missing_cap) {
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001024 ret = -EINVAL;
Jan Kiszka94a8d392011-01-21 21:48:17 +01001025 fprintf(stderr, "kvm does not support %s\n%s",
1026 missing_cap->name, upgrade_note);
aliguori05330442008-11-05 16:29:27 +00001027 goto err;
1028 }
1029
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001030 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
aliguorif65ed4c2008-12-09 20:09:57 +00001031
Jan Kiszkae69917e2009-05-01 20:42:15 +02001032 s->broken_set_mem_region = 1;
Lai Jiangshan14a09512010-12-10 15:52:36 +08001033 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
Jan Kiszkae69917e2009-05-01 20:42:15 +02001034 if (ret > 0) {
1035 s->broken_set_mem_region = 0;
1036 }
Jan Kiszkae69917e2009-05-01 20:42:15 +02001037
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001038#ifdef KVM_CAP_VCPU_EVENTS
1039 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1040#endif
1041
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001042 s->robust_singlestep =
1043 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001044
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001045#ifdef KVM_CAP_DEBUGREGS
1046 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1047#endif
1048
Sheng Yangf1665b22010-06-17 17:53:07 +08001049#ifdef KVM_CAP_XSAVE
1050 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1051#endif
1052
Sheng Yangf1665b22010-06-17 17:53:07 +08001053#ifdef KVM_CAP_XCRS
1054 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1055#endif
1056
Jan Kiszkacad1e282011-01-21 21:48:16 +01001057 ret = kvm_arch_init(s);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001058 if (ret < 0) {
aliguori05330442008-11-05 16:29:27 +00001059 goto err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001060 }
aliguori05330442008-11-05 16:29:27 +00001061
Jan Kiszka84b058d2011-10-15 11:49:47 +02001062 ret = kvm_irqchip_create(s);
1063 if (ret < 0) {
1064 goto err;
1065 }
1066
aliguori05330442008-11-05 16:29:27 +00001067 kvm_state = s;
Avi Kivity7376e582012-02-08 21:05:17 +02001068 memory_listener_register(&kvm_memory_listener, NULL);
aliguori05330442008-11-05 16:29:27 +00001069
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001070 s->many_ioeventfds = kvm_check_many_ioeventfds();
1071
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +02001072 cpu_interrupt_handler = kvm_handle_interrupt;
1073
aliguori05330442008-11-05 16:29:27 +00001074 return 0;
1075
1076err:
1077 if (s) {
Xu He Jiedb9eae12011-10-27 10:15:13 +08001078 if (s->vmfd >= 0) {
aliguori05330442008-11-05 16:29:27 +00001079 close(s->vmfd);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001080 }
1081 if (s->fd != -1) {
aliguori05330442008-11-05 16:29:27 +00001082 close(s->fd);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001083 }
aliguori05330442008-11-05 16:29:27 +00001084 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001085 g_free(s);
aliguori05330442008-11-05 16:29:27 +00001086
1087 return ret;
1088}
1089
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001090static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1091 uint32_t count)
aliguori05330442008-11-05 16:29:27 +00001092{
1093 int i;
1094 uint8_t *ptr = data;
1095
1096 for (i = 0; i < count; i++) {
1097 if (direction == KVM_EXIT_IO_IN) {
1098 switch (size) {
1099 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001100 stb_p(ptr, cpu_inb(port));
aliguori05330442008-11-05 16:29:27 +00001101 break;
1102 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001103 stw_p(ptr, cpu_inw(port));
aliguori05330442008-11-05 16:29:27 +00001104 break;
1105 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001106 stl_p(ptr, cpu_inl(port));
aliguori05330442008-11-05 16:29:27 +00001107 break;
1108 }
1109 } else {
1110 switch (size) {
1111 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001112 cpu_outb(port, ldub_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001113 break;
1114 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001115 cpu_outw(port, lduw_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001116 break;
1117 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001118 cpu_outl(port, ldl_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001119 break;
1120 }
1121 }
1122
1123 ptr += size;
1124 }
aliguori05330442008-11-05 16:29:27 +00001125}
1126
Jan Kiszka73aaec42011-01-21 21:48:06 +01001127static int kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001128{
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001129 fprintf(stderr, "KVM internal error.");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001130 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1131 int i;
1132
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001133 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001134 for (i = 0; i < run->internal.ndata; ++i) {
1135 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1136 i, (uint64_t)run->internal.data[i]);
1137 }
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001138 } else {
1139 fprintf(stderr, "\n");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001140 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001141 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1142 fprintf(stderr, "emulation failure\n");
Jan Kiszkaa426e122011-01-04 09:32:13 +01001143 if (!kvm_arch_stop_on_emulation_error(env)) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001144 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001145 return EXCP_INTERRUPT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001146 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001147 }
1148 /* FIXME: Should trigger a qmp message to let management know
1149 * something went wrong.
1150 */
Jan Kiszka73aaec42011-01-21 21:48:06 +01001151 return -1;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001152}
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001153
Sheng Yang62a27442010-01-26 19:21:16 +08001154void kvm_flush_coalesced_mmio_buffer(void)
aliguorif65ed4c2008-12-09 20:09:57 +00001155{
aliguorif65ed4c2008-12-09 20:09:57 +00001156 KVMState *s = kvm_state;
Avi Kivity1cae88b2011-10-18 19:43:12 +02001157
1158 if (s->coalesced_flush_in_progress) {
1159 return;
1160 }
1161
1162 s->coalesced_flush_in_progress = true;
1163
Sheng Yang62a27442010-01-26 19:21:16 +08001164 if (s->coalesced_mmio_ring) {
1165 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
aliguorif65ed4c2008-12-09 20:09:57 +00001166 while (ring->first != ring->last) {
1167 struct kvm_coalesced_mmio *ent;
1168
1169 ent = &ring->coalesced_mmio[ring->first];
1170
1171 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
Marcelo Tosatti85199472010-02-22 13:57:54 -03001172 smp_wmb();
aliguorif65ed4c2008-12-09 20:09:57 +00001173 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1174 }
1175 }
Avi Kivity1cae88b2011-10-18 19:43:12 +02001176
1177 s->coalesced_flush_in_progress = false;
aliguorif65ed4c2008-12-09 20:09:57 +00001178}
1179
Jan Kiszka2705d562010-05-04 09:45:23 -03001180static void do_kvm_cpu_synchronize_state(void *_env)
Avi Kivity4c0960c2009-08-17 23:19:53 +03001181{
Jan Kiszka2705d562010-05-04 09:45:23 -03001182 CPUState *env = _env;
1183
Jan Kiszka9ded2742010-02-03 21:17:05 +01001184 if (!env->kvm_vcpu_dirty) {
Avi Kivity4c0960c2009-08-17 23:19:53 +03001185 kvm_arch_get_registers(env);
Jan Kiszka9ded2742010-02-03 21:17:05 +01001186 env->kvm_vcpu_dirty = 1;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001187 }
1188}
1189
Jan Kiszka2705d562010-05-04 09:45:23 -03001190void kvm_cpu_synchronize_state(CPUState *env)
1191{
Jan Kiszkaa426e122011-01-04 09:32:13 +01001192 if (!env->kvm_vcpu_dirty) {
Jan Kiszka2705d562010-05-04 09:45:23 -03001193 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001194 }
Jan Kiszka2705d562010-05-04 09:45:23 -03001195}
1196
Jan Kiszkaea375f92010-03-01 19:10:30 +01001197void kvm_cpu_synchronize_post_reset(CPUState *env)
1198{
1199 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1200 env->kvm_vcpu_dirty = 0;
1201}
1202
1203void kvm_cpu_synchronize_post_init(CPUState *env)
1204{
1205 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1206 env->kvm_vcpu_dirty = 0;
1207}
1208
aliguori05330442008-11-05 16:29:27 +00001209int kvm_cpu_exec(CPUState *env)
1210{
1211 struct kvm_run *run = env->kvm_run;
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001212 int ret, run_ret;
aliguori05330442008-11-05 16:29:27 +00001213
Blue Swirl8c0d5772010-04-18 14:22:14 +00001214 DPRINTF("kvm_cpu_exec()\n");
aliguori05330442008-11-05 16:29:27 +00001215
Jan Kiszka99036862011-03-02 08:56:13 +01001216 if (kvm_arch_process_async_events(env)) {
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001217 env->exit_request = 0;
Jan Kiszka6792a572011-02-07 12:19:18 +01001218 return EXCP_HLT;
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001219 }
1220
Jan Kiszka6792a572011-02-07 12:19:18 +01001221 cpu_single_env = env;
1222
aliguori05330442008-11-05 16:29:27 +00001223 do {
Jan Kiszka9ded2742010-02-03 21:17:05 +01001224 if (env->kvm_vcpu_dirty) {
Jan Kiszkaea375f92010-03-01 19:10:30 +01001225 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
Jan Kiszka9ded2742010-02-03 21:17:05 +01001226 env->kvm_vcpu_dirty = 0;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001227 }
1228
Jan Kiszka8c14c172009-05-30 10:01:45 +02001229 kvm_arch_pre_run(env, run);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001230 if (env->exit_request) {
1231 DPRINTF("interrupt exit requested\n");
1232 /*
1233 * KVM requires us to reenter the kernel after IO exits to complete
1234 * instruction emulation. This self-signal will ensure that we
1235 * leave ASAP again.
1236 */
1237 qemu_cpu_kick_self();
1238 }
Marcelo Tosatti273faf12010-05-04 09:45:19 -03001239 cpu_single_env = NULL;
Glauber Costad549db52009-10-07 16:38:03 -03001240 qemu_mutex_unlock_iothread();
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001241
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001242 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001243
Glauber Costad549db52009-10-07 16:38:03 -03001244 qemu_mutex_lock_iothread();
Marcelo Tosatti273faf12010-05-04 09:45:19 -03001245 cpu_single_env = env;
aliguori05330442008-11-05 16:29:27 +00001246 kvm_arch_post_run(env, run);
1247
Jan Kiszkab0c883b2011-01-21 21:48:19 +01001248 kvm_flush_coalesced_mmio_buffer();
1249
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001250 if (run_ret < 0) {
Jan Kiszkadc77d342011-03-15 12:26:26 +01001251 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1252 DPRINTF("io window exit\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001253 ret = EXCP_INTERRUPT;
Jan Kiszkadc77d342011-03-15 12:26:26 +01001254 break;
1255 }
Michael Ellerman7b011fb2011-12-16 11:20:20 +11001256 fprintf(stderr, "error: kvm run failed %s\n",
1257 strerror(-run_ret));
aliguori05330442008-11-05 16:29:27 +00001258 abort();
1259 }
1260
aliguori05330442008-11-05 16:29:27 +00001261 switch (run->exit_reason) {
1262 case KVM_EXIT_IO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001263 DPRINTF("handle_io\n");
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001264 kvm_handle_io(run->io.port,
1265 (uint8_t *)run + run->io.data_offset,
1266 run->io.direction,
1267 run->io.size,
1268 run->io.count);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001269 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001270 break;
1271 case KVM_EXIT_MMIO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001272 DPRINTF("handle_mmio\n");
aliguori05330442008-11-05 16:29:27 +00001273 cpu_physical_memory_rw(run->mmio.phys_addr,
1274 run->mmio.data,
1275 run->mmio.len,
1276 run->mmio.is_write);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001277 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001278 break;
1279 case KVM_EXIT_IRQ_WINDOW_OPEN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001280 DPRINTF("irq_window_open\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001281 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001282 break;
1283 case KVM_EXIT_SHUTDOWN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001284 DPRINTF("shutdown\n");
aliguori05330442008-11-05 16:29:27 +00001285 qemu_system_reset_request();
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001286 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001287 break;
1288 case KVM_EXIT_UNKNOWN:
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001289 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1290 (uint64_t)run->hw.hardware_exit_reason);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001291 ret = -1;
aliguori05330442008-11-05 16:29:27 +00001292 break;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001293 case KVM_EXIT_INTERNAL_ERROR:
Jan Kiszka73aaec42011-01-21 21:48:06 +01001294 ret = kvm_handle_internal_error(env, run);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001295 break;
aliguori05330442008-11-05 16:29:27 +00001296 default:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001297 DPRINTF("kvm_arch_handle_exit\n");
aliguori05330442008-11-05 16:29:27 +00001298 ret = kvm_arch_handle_exit(env, run);
1299 break;
1300 }
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001301 } while (ret == 0);
aliguori05330442008-11-05 16:29:27 +00001302
Jan Kiszka73aaec42011-01-21 21:48:06 +01001303 if (ret < 0) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001304 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001305 vm_stop(RUN_STATE_INTERNAL_ERROR);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001306 }
aliguoribecfc392008-11-10 15:55:14 +00001307
Jan Kiszka6792a572011-02-07 12:19:18 +01001308 env->exit_request = 0;
1309 cpu_single_env = NULL;
aliguori05330442008-11-05 16:29:27 +00001310 return ret;
1311}
1312
aliguori984b5182008-11-13 19:21:00 +00001313int kvm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001314{
1315 int ret;
aliguori984b5182008-11-13 19:21:00 +00001316 void *arg;
1317 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001318
aliguori984b5182008-11-13 19:21:00 +00001319 va_start(ap, type);
1320 arg = va_arg(ap, void *);
1321 va_end(ap);
1322
1323 ret = ioctl(s->fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001324 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001325 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001326 }
aliguori05330442008-11-05 16:29:27 +00001327 return ret;
1328}
1329
aliguori984b5182008-11-13 19:21:00 +00001330int kvm_vm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001331{
1332 int ret;
aliguori984b5182008-11-13 19:21:00 +00001333 void *arg;
1334 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001335
aliguori984b5182008-11-13 19:21:00 +00001336 va_start(ap, type);
1337 arg = va_arg(ap, void *);
1338 va_end(ap);
1339
1340 ret = ioctl(s->vmfd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001341 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001342 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001343 }
aliguori05330442008-11-05 16:29:27 +00001344 return ret;
1345}
1346
aliguori984b5182008-11-13 19:21:00 +00001347int kvm_vcpu_ioctl(CPUState *env, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001348{
1349 int ret;
aliguori984b5182008-11-13 19:21:00 +00001350 void *arg;
1351 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001352
aliguori984b5182008-11-13 19:21:00 +00001353 va_start(ap, type);
1354 arg = va_arg(ap, void *);
1355 va_end(ap);
1356
1357 ret = ioctl(env->kvm_fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001358 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001359 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001360 }
aliguori05330442008-11-05 16:29:27 +00001361 return ret;
1362}
aliguoribd322082008-12-04 20:33:06 +00001363
1364int kvm_has_sync_mmu(void)
1365{
Jan Kiszka94a8d392011-01-21 21:48:17 +01001366 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
aliguoribd322082008-12-04 20:33:06 +00001367}
aliguorie22a25c2009-03-12 20:12:48 +00001368
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001369int kvm_has_vcpu_events(void)
1370{
1371 return kvm_state->vcpu_events;
1372}
1373
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001374int kvm_has_robust_singlestep(void)
1375{
1376 return kvm_state->robust_singlestep;
1377}
1378
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001379int kvm_has_debugregs(void)
1380{
1381 return kvm_state->debugregs;
1382}
1383
Sheng Yangf1665b22010-06-17 17:53:07 +08001384int kvm_has_xsave(void)
1385{
1386 return kvm_state->xsave;
1387}
1388
1389int kvm_has_xcrs(void)
1390{
1391 return kvm_state->xcrs;
1392}
1393
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001394int kvm_has_many_ioeventfds(void)
1395{
1396 if (!kvm_enabled()) {
1397 return 0;
1398 }
1399 return kvm_state->many_ioeventfds;
1400}
1401
Jan Kiszka84b058d2011-10-15 11:49:47 +02001402int kvm_has_gsi_routing(void)
1403{
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001404#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszka84b058d2011-10-15 11:49:47 +02001405 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001406#else
1407 return false;
1408#endif
Jan Kiszka84b058d2011-10-15 11:49:47 +02001409}
1410
Jan Kiszka9b5b76d2011-10-15 14:08:26 +02001411int kvm_allows_irq0_override(void)
1412{
1413 return !kvm_enabled() || !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1414}
1415
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001416void kvm_setup_guest_memory(void *start, size_t size)
1417{
1418 if (!kvm_has_sync_mmu()) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001419 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001420
1421 if (ret) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001422 perror("qemu_madvise");
1423 fprintf(stderr,
1424 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001425 exit(1);
1426 }
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001427 }
1428}
1429
aliguorie22a25c2009-03-12 20:12:48 +00001430#ifdef KVM_CAP_SET_GUEST_DEBUG
1431struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1432 target_ulong pc)
1433{
1434 struct kvm_sw_breakpoint *bp;
1435
Blue Swirl72cf2d42009-09-12 07:36:22 +00001436 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001437 if (bp->pc == pc) {
aliguorie22a25c2009-03-12 20:12:48 +00001438 return bp;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001439 }
aliguorie22a25c2009-03-12 20:12:48 +00001440 }
1441 return NULL;
1442}
1443
1444int kvm_sw_breakpoints_active(CPUState *env)
1445{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001446 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +00001447}
1448
Glauber Costa452e4752009-07-16 17:55:28 -04001449struct kvm_set_guest_debug_data {
1450 struct kvm_guest_debug dbg;
1451 CPUState *env;
1452 int err;
1453};
1454
1455static void kvm_invoke_set_guest_debug(void *data)
1456{
1457 struct kvm_set_guest_debug_data *dbg_data = data;
Jan Kiszkab3807722009-09-17 20:05:58 +02001458 CPUState *env = dbg_data->env;
1459
Jan Kiszkab3807722009-09-17 20:05:58 +02001460 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001461}
1462
aliguorie22a25c2009-03-12 20:12:48 +00001463int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1464{
Glauber Costa452e4752009-07-16 17:55:28 -04001465 struct kvm_set_guest_debug_data data;
aliguorie22a25c2009-03-12 20:12:48 +00001466
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001467 data.dbg.control = reinject_trap;
aliguorie22a25c2009-03-12 20:12:48 +00001468
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001469 if (env->singlestep_enabled) {
1470 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1471 }
Glauber Costa452e4752009-07-16 17:55:28 -04001472 kvm_arch_update_guest_debug(env, &data.dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001473 data.env = env;
aliguorie22a25c2009-03-12 20:12:48 +00001474
Jan Kiszkabe41cbe2010-05-20 00:28:45 +02001475 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
Glauber Costa452e4752009-07-16 17:55:28 -04001476 return data.err;
aliguorie22a25c2009-03-12 20:12:48 +00001477}
1478
1479int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1480 target_ulong len, int type)
1481{
1482 struct kvm_sw_breakpoint *bp;
1483 CPUState *env;
1484 int err;
1485
1486 if (type == GDB_BREAKPOINT_SW) {
1487 bp = kvm_find_sw_breakpoint(current_env, addr);
1488 if (bp) {
1489 bp->use_count++;
1490 return 0;
1491 }
1492
Anthony Liguori7267c092011-08-20 22:09:37 -05001493 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
Jan Kiszkaa426e122011-01-04 09:32:13 +01001494 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001495 return -ENOMEM;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001496 }
aliguorie22a25c2009-03-12 20:12:48 +00001497
1498 bp->pc = addr;
1499 bp->use_count = 1;
1500 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1501 if (err) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001502 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001503 return err;
1504 }
1505
Blue Swirl72cf2d42009-09-12 07:36:22 +00001506 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
aliguorie22a25c2009-03-12 20:12:48 +00001507 bp, entry);
1508 } else {
1509 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001510 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001511 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001512 }
aliguorie22a25c2009-03-12 20:12:48 +00001513 }
1514
1515 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1516 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001517 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001518 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001519 }
aliguorie22a25c2009-03-12 20:12:48 +00001520 }
1521 return 0;
1522}
1523
1524int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1525 target_ulong len, int type)
1526{
1527 struct kvm_sw_breakpoint *bp;
1528 CPUState *env;
1529 int err;
1530
1531 if (type == GDB_BREAKPOINT_SW) {
1532 bp = kvm_find_sw_breakpoint(current_env, addr);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001533 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001534 return -ENOENT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001535 }
aliguorie22a25c2009-03-12 20:12:48 +00001536
1537 if (bp->use_count > 1) {
1538 bp->use_count--;
1539 return 0;
1540 }
1541
1542 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001543 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001544 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001545 }
aliguorie22a25c2009-03-12 20:12:48 +00001546
Blue Swirl72cf2d42009-09-12 07:36:22 +00001547 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001548 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001549 } else {
1550 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001551 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001552 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001553 }
aliguorie22a25c2009-03-12 20:12:48 +00001554 }
1555
1556 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1557 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001558 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001559 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001560 }
aliguorie22a25c2009-03-12 20:12:48 +00001561 }
1562 return 0;
1563}
1564
1565void kvm_remove_all_breakpoints(CPUState *current_env)
1566{
1567 struct kvm_sw_breakpoint *bp, *next;
1568 KVMState *s = current_env->kvm_state;
1569 CPUState *env;
1570
Blue Swirl72cf2d42009-09-12 07:36:22 +00001571 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
aliguorie22a25c2009-03-12 20:12:48 +00001572 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1573 /* Try harder to find a CPU that currently sees the breakpoint. */
1574 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001575 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
aliguorie22a25c2009-03-12 20:12:48 +00001576 break;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001577 }
aliguorie22a25c2009-03-12 20:12:48 +00001578 }
1579 }
1580 }
1581 kvm_arch_remove_all_hw_breakpoints();
1582
Jan Kiszkaa426e122011-01-04 09:32:13 +01001583 for (env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorie22a25c2009-03-12 20:12:48 +00001584 kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001585 }
aliguorie22a25c2009-03-12 20:12:48 +00001586}
1587
1588#else /* !KVM_CAP_SET_GUEST_DEBUG */
1589
1590int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1591{
1592 return -EINVAL;
1593}
1594
1595int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1596 target_ulong len, int type)
1597{
1598 return -EINVAL;
1599}
1600
1601int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1602 target_ulong len, int type)
1603{
1604 return -EINVAL;
1605}
1606
1607void kvm_remove_all_breakpoints(CPUState *current_env)
1608{
1609}
1610#endif /* !KVM_CAP_SET_GUEST_DEBUG */
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001611
1612int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset)
1613{
1614 struct kvm_signal_mask *sigmask;
1615 int r;
1616
Jan Kiszkaa426e122011-01-04 09:32:13 +01001617 if (!sigset) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001618 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001619 }
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001620
Anthony Liguori7267c092011-08-20 22:09:37 -05001621 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001622
1623 sigmask->len = 8;
1624 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1625 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
Anthony Liguori7267c092011-08-20 22:09:37 -05001626 g_free(sigmask);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001627
1628 return r;
1629}
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001630
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001631int kvm_set_ioeventfd_mmio_long(int fd, uint32_t addr, uint32_t val, bool assign)
1632{
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001633 int ret;
1634 struct kvm_ioeventfd iofd;
1635
1636 iofd.datamatch = val;
1637 iofd.addr = addr;
1638 iofd.len = 4;
1639 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1640 iofd.fd = fd;
1641
1642 if (!kvm_enabled()) {
1643 return -ENOSYS;
1644 }
1645
1646 if (!assign) {
1647 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1648 }
1649
1650 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1651
1652 if (ret < 0) {
1653 return -errno;
1654 }
1655
1656 return 0;
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001657}
1658
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001659int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1660{
1661 struct kvm_ioeventfd kick = {
1662 .datamatch = val,
1663 .addr = addr,
1664 .len = 2,
1665 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1666 .fd = fd,
1667 };
1668 int r;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001669 if (!kvm_enabled()) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001670 return -ENOSYS;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001671 }
1672 if (!assign) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001673 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001674 }
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001675 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001676 if (r < 0) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001677 return r;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001678 }
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001679 return 0;
Paolo Bonzini98c85732010-04-19 18:59:30 +00001680}
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01001681
1682int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr)
1683{
1684 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1685}
1686
1687int kvm_on_sigbus(int code, void *addr)
1688{
1689 return kvm_arch_on_sigbus(code, addr);
1690}