blob: f9873db4af12250601013358d6b495f953b6701b [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"
Paolo Bonziniebd063d2012-06-07 04:02:20 +020025#include "qemu-option.h"
26#include "qemu-config.h"
aliguori05330442008-11-05 16:29:27 +000027#include "sysemu.h"
Jan Kiszkad33a1812009-05-02 00:29:37 +020028#include "hw/hw.h"
Jan Kiszka04fa27f2012-05-16 15:41:10 -030029#include "hw/msi.h"
aliguorie22a25c2009-03-12 20:12:48 +000030#include "gdbstub.h"
aliguori05330442008-11-05 16:29:27 +000031#include "kvm.h"
Marcelo Tosatti8369e012010-04-23 14:04:14 -030032#include "bswap.h"
Avi Kivitya01672d2011-12-18 14:06:05 +020033#include "memory.h"
Avi Kivity80a1ea32012-02-08 16:39:06 +020034#include "exec-memory.h"
Paolo Bonzini753d5e12012-07-05 17:16:27 +020035#include "event_notifier.h"
aliguori05330442008-11-05 16:29:27 +000036
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +020037/* This check must be after config-host.h is included */
38#ifdef CONFIG_EVENTFD
39#include <sys/eventfd.h>
40#endif
41
Christian Borntraeger62fe8332012-08-10 15:11:45 +020042#ifdef CONFIG_VALGRIND_H
43#include <valgrind/memcheck.h>
44#endif
45
Stefan Weil93148aa2012-02-26 18:46:12 +010046/* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
aliguorif65ed4c2008-12-09 20:09:57 +000047#define PAGE_SIZE TARGET_PAGE_SIZE
48
aliguori05330442008-11-05 16:29:27 +000049//#define DEBUG_KVM
50
51#ifdef DEBUG_KVM
Blue Swirl8c0d5772010-04-18 14:22:14 +000052#define DPRINTF(fmt, ...) \
aliguori05330442008-11-05 16:29:27 +000053 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
54#else
Blue Swirl8c0d5772010-04-18 14:22:14 +000055#define DPRINTF(fmt, ...) \
aliguori05330442008-11-05 16:29:27 +000056 do { } while (0)
57#endif
58
Jan Kiszka04fa27f2012-05-16 15:41:10 -030059#define KVM_MSI_HASHTAB_SIZE 256
60
aliguori34fc6432008-11-19 17:41:58 +000061typedef struct KVMSlot
62{
Anthony Liguoric227f092009-10-01 16:12:16 -050063 target_phys_addr_t start_addr;
64 ram_addr_t memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +020065 void *ram;
aliguori34fc6432008-11-19 17:41:58 +000066 int slot;
67 int flags;
68} KVMSlot;
aliguori05330442008-11-05 16:29:27 +000069
aliguori5832d1f2008-11-24 19:36:26 +000070typedef struct kvm_dirty_log KVMDirtyLog;
71
aliguori05330442008-11-05 16:29:27 +000072struct KVMState
73{
74 KVMSlot slots[32];
75 int fd;
76 int vmfd;
aliguorif65ed4c2008-12-09 20:09:57 +000077 int coalesced_mmio;
Sheng Yang62a27442010-01-26 19:21:16 +080078 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
Avi Kivity1cae88b2011-10-18 19:43:12 +020079 bool coalesced_flush_in_progress;
Jan Kiszkae69917e2009-05-01 20:42:15 +020080 int broken_set_mem_region;
Jan Kiszka4495d6a2009-05-01 20:52:46 +020081 int migration_log;
Jan Kiszkaa0fb0022009-11-25 00:33:03 +010082 int vcpu_events;
Jan Kiszkab0b1d692010-03-01 19:10:29 +010083 int robust_singlestep;
Jan Kiszkaff44f1a2010-03-12 15:20:49 +010084 int debugregs;
aliguorie22a25c2009-03-12 20:12:48 +000085#ifdef KVM_CAP_SET_GUEST_DEBUG
86 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
87#endif
Jan Kiszka8a7c7392012-03-02 20:28:48 +010088 int pit_state2;
Sheng Yangf1665b22010-06-17 17:53:07 +080089 int xsave, xcrs;
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +020090 int many_ioeventfds;
Jan Kiszka3ab73842012-08-27 08:28:39 +020091 int intx_set_mask;
David Gibson92e4b512012-03-07 14:41:09 +000092 /* The man page (and posix) say ioctl numbers are signed int, but
93 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
94 * unsigned, and treating them as signed here can break things */
95 unsigned irqchip_inject_ioctl;
Jan Kiszka84b058d2011-10-15 11:49:47 +020096#ifdef KVM_CAP_IRQ_ROUTING
97 struct kvm_irq_routing *irq_routes;
98 int nr_allocated_irq_routes;
99 uint32_t *used_gsi_bitmap;
Jan Kiszka4e2e4e62012-05-16 15:41:08 -0300100 unsigned int gsi_count;
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300101 QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE];
Jan Kiszka4a3adeb2012-05-16 15:41:14 -0300102 bool direct_msi;
Jan Kiszka84b058d2011-10-15 11:49:47 +0200103#endif
aliguori05330442008-11-05 16:29:27 +0000104};
105
Jan Kiszka6a7af8c2011-02-07 12:19:25 +0100106KVMState *kvm_state;
Jan Kiszka3d4b2642012-01-31 19:17:52 +0100107bool kvm_kernel_irqchip;
Peter Maydell7ae26bd2012-07-26 15:35:11 +0100108bool kvm_async_interrupts_allowed;
Peter Maydellcc7e0dd2012-07-26 15:35:14 +0100109bool kvm_irqfds_allowed;
Peter Maydell614e41b2012-07-26 15:35:15 +0100110bool kvm_msi_via_irqfd_allowed;
Peter Maydellf3e1bed2012-07-26 15:35:16 +0100111bool kvm_gsi_routing_allowed;
aliguori05330442008-11-05 16:29:27 +0000112
Jan Kiszka94a8d392011-01-21 21:48:17 +0100113static const KVMCapabilityInfo kvm_required_capabilites[] = {
114 KVM_CAP_INFO(USER_MEMORY),
115 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
116 KVM_CAP_LAST_INFO
117};
118
aliguori05330442008-11-05 16:29:27 +0000119static KVMSlot *kvm_alloc_slot(KVMState *s)
120{
121 int i;
122
123 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
Jan Kiszkaa426e122011-01-04 09:32:13 +0100124 if (s->slots[i].memory_size == 0) {
aliguori05330442008-11-05 16:29:27 +0000125 return &s->slots[i];
Jan Kiszkaa426e122011-01-04 09:32:13 +0100126 }
aliguori05330442008-11-05 16:29:27 +0000127 }
128
aliguorid3f8d372009-04-17 14:26:29 +0000129 fprintf(stderr, "%s: no free slot available\n", __func__);
130 abort();
131}
132
133static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
Anthony Liguoric227f092009-10-01 16:12:16 -0500134 target_phys_addr_t start_addr,
135 target_phys_addr_t end_addr)
aliguorid3f8d372009-04-17 14:26:29 +0000136{
137 int i;
138
139 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
140 KVMSlot *mem = &s->slots[i];
141
142 if (start_addr == mem->start_addr &&
143 end_addr == mem->start_addr + mem->memory_size) {
144 return mem;
145 }
146 }
147
aliguori05330442008-11-05 16:29:27 +0000148 return NULL;
149}
150
aliguori6152e2a2009-04-17 14:26:33 +0000151/*
152 * Find overlapping slot with lowest start address
153 */
154static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
Anthony Liguoric227f092009-10-01 16:12:16 -0500155 target_phys_addr_t start_addr,
156 target_phys_addr_t end_addr)
aliguori05330442008-11-05 16:29:27 +0000157{
aliguori6152e2a2009-04-17 14:26:33 +0000158 KVMSlot *found = NULL;
aliguori05330442008-11-05 16:29:27 +0000159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
162 KVMSlot *mem = &s->slots[i];
163
aliguori6152e2a2009-04-17 14:26:33 +0000164 if (mem->memory_size == 0 ||
165 (found && found->start_addr < mem->start_addr)) {
166 continue;
167 }
168
169 if (end_addr > mem->start_addr &&
170 start_addr < mem->start_addr + mem->memory_size) {
171 found = mem;
172 }
aliguori05330442008-11-05 16:29:27 +0000173 }
174
aliguori6152e2a2009-04-17 14:26:33 +0000175 return found;
aliguori05330442008-11-05 16:29:27 +0000176}
177
Avi Kivity9f213ed2011-12-15 19:55:26 +0200178int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
179 target_phys_addr_t *phys_addr)
Huang Ying983dfc32010-10-11 15:31:20 -0300180{
181 int i;
182
183 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
184 KVMSlot *mem = &s->slots[i];
185
Avi Kivity9f213ed2011-12-15 19:55:26 +0200186 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
187 *phys_addr = mem->start_addr + (ram - mem->ram);
Huang Ying983dfc32010-10-11 15:31:20 -0300188 return 1;
189 }
190 }
191
192 return 0;
193}
194
aliguori5832d1f2008-11-24 19:36:26 +0000195static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
196{
197 struct kvm_userspace_memory_region mem;
198
199 mem.slot = slot->slot;
200 mem.guest_phys_addr = slot->start_addr;
201 mem.memory_size = slot->memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200202 mem.userspace_addr = (unsigned long)slot->ram;
aliguori5832d1f2008-11-24 19:36:26 +0000203 mem.flags = slot->flags;
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200204 if (s->migration_log) {
205 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
206 }
aliguori5832d1f2008-11-24 19:36:26 +0000207 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
208}
209
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200210static void kvm_reset_vcpu(void *opaque)
211{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100212 CPUArchState *env = opaque;
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200213
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100214 kvm_arch_reset_vcpu(env);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200215}
aliguori5832d1f2008-11-24 19:36:26 +0000216
Andreas Färber9349b4f2012-03-14 01:38:32 +0100217int kvm_init_vcpu(CPUArchState *env)
aliguori05330442008-11-05 16:29:27 +0000218{
219 KVMState *s = kvm_state;
220 long mmap_size;
221 int ret;
222
Blue Swirl8c0d5772010-04-18 14:22:14 +0000223 DPRINTF("kvm_init_vcpu\n");
aliguori05330442008-11-05 16:29:27 +0000224
aliguori984b5182008-11-13 19:21:00 +0000225 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
aliguori05330442008-11-05 16:29:27 +0000226 if (ret < 0) {
Blue Swirl8c0d5772010-04-18 14:22:14 +0000227 DPRINTF("kvm_create_vcpu failed\n");
aliguori05330442008-11-05 16:29:27 +0000228 goto err;
229 }
230
231 env->kvm_fd = ret;
232 env->kvm_state = s;
Jan Kiszkad841b6c2011-03-15 12:26:20 +0100233 env->kvm_vcpu_dirty = 1;
aliguori05330442008-11-05 16:29:27 +0000234
235 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
236 if (mmap_size < 0) {
Jan Kiszka748a6802011-02-01 22:15:48 +0100237 ret = mmap_size;
Blue Swirl8c0d5772010-04-18 14:22:14 +0000238 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
aliguori05330442008-11-05 16:29:27 +0000239 goto err;
240 }
241
242 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
243 env->kvm_fd, 0);
244 if (env->kvm_run == MAP_FAILED) {
245 ret = -errno;
Blue Swirl8c0d5772010-04-18 14:22:14 +0000246 DPRINTF("mmap'ing vcpu state failed\n");
aliguori05330442008-11-05 16:29:27 +0000247 goto err;
248 }
249
Jan Kiszkaa426e122011-01-04 09:32:13 +0100250 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
251 s->coalesced_mmio_ring =
252 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
253 }
Sheng Yang62a27442010-01-26 19:21:16 +0800254
aliguori05330442008-11-05 16:29:27 +0000255 ret = kvm_arch_init_vcpu(env);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200256 if (ret == 0) {
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200257 qemu_register_reset(kvm_reset_vcpu, env);
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100258 kvm_arch_reset_vcpu(env);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200259 }
aliguori05330442008-11-05 16:29:27 +0000260err:
261 return ret;
262}
263
aliguori5832d1f2008-11-24 19:36:26 +0000264/*
265 * dirty pages logging control
266 */
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300267
268static int kvm_mem_flags(KVMState *s, bool log_dirty)
269{
270 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
271}
272
273static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
aliguori5832d1f2008-11-24 19:36:26 +0000274{
275 KVMState *s = kvm_state;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300276 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200277 int old_flags;
278
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200279 old_flags = mem->flags;
aliguori5832d1f2008-11-24 19:36:26 +0000280
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300281 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
aliguori5832d1f2008-11-24 19:36:26 +0000282 mem->flags = flags;
283
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200284 /* If nothing changed effectively, no need to issue ioctl */
285 if (s->migration_log) {
286 flags |= KVM_MEM_LOG_DIRTY_PAGES;
287 }
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300288
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200289 if (flags == old_flags) {
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300290 return 0;
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200291 }
292
aliguori5832d1f2008-11-24 19:36:26 +0000293 return kvm_set_user_memory_region(s, mem);
294}
295
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300296static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
297 ram_addr_t size, bool log_dirty)
298{
299 KVMState *s = kvm_state;
300 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
301
302 if (mem == NULL) {
303 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
304 TARGET_FMT_plx "\n", __func__, phys_addr,
305 (target_phys_addr_t)(phys_addr + size - 1));
306 return -EINVAL;
307 }
308 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
309}
310
Avi Kivitya01672d2011-12-18 14:06:05 +0200311static void kvm_log_start(MemoryListener *listener,
312 MemoryRegionSection *section)
aliguori5832d1f2008-11-24 19:36:26 +0000313{
Avi Kivitya01672d2011-12-18 14:06:05 +0200314 int r;
315
316 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
317 section->size, true);
318 if (r < 0) {
319 abort();
320 }
aliguori5832d1f2008-11-24 19:36:26 +0000321}
322
Avi Kivitya01672d2011-12-18 14:06:05 +0200323static void kvm_log_stop(MemoryListener *listener,
324 MemoryRegionSection *section)
aliguori5832d1f2008-11-24 19:36:26 +0000325{
Avi Kivitya01672d2011-12-18 14:06:05 +0200326 int r;
327
328 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
329 section->size, false);
330 if (r < 0) {
331 abort();
332 }
aliguori5832d1f2008-11-24 19:36:26 +0000333}
334
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200335static int kvm_set_migration_log(int enable)
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200336{
337 KVMState *s = kvm_state;
338 KVMSlot *mem;
339 int i, err;
340
341 s->migration_log = enable;
342
343 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
344 mem = &s->slots[i];
345
Alex Williamson70fedd72010-07-14 13:36:49 -0600346 if (!mem->memory_size) {
347 continue;
348 }
Jan Kiszka4495d6a2009-05-01 20:52:46 +0200349 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
350 continue;
351 }
352 err = kvm_set_user_memory_region(s, mem);
353 if (err) {
354 return err;
355 }
356 }
357 return 0;
358}
359
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300360/* get kvm's dirty pages bitmap and update qemu's */
Avi Kivityffcde122011-12-19 13:18:13 +0200361static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
362 unsigned long *bitmap)
Alexander Graf96c16062009-07-27 12:49:56 +0200363{
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300364 unsigned int i, j;
Benjamin Herrenschmidtaa90fec2012-01-11 19:46:21 +0000365 unsigned long page_number, c;
366 target_phys_addr_t addr, addr1;
Avi Kivityffcde122011-12-19 13:18:13 +0200367 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
David Gibson3145fcb2012-04-04 11:15:54 +1000368 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300369
370 /*
371 * bitmap-traveling is faster than memory-traveling (for addr...)
372 * especially when most of the memory is not dirty.
373 */
374 for (i = 0; i < len; i++) {
375 if (bitmap[i] != 0) {
376 c = leul_to_cpu(bitmap[i]);
377 do {
378 j = ffsl(c) - 1;
379 c &= ~(1ul << j);
David Gibson3145fcb2012-04-04 11:15:54 +1000380 page_number = (i * HOST_LONG_BITS + j) * hpratio;
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300381 addr1 = page_number * TARGET_PAGE_SIZE;
Avi Kivityffcde122011-12-19 13:18:13 +0200382 addr = section->offset_within_region + addr1;
David Gibson3145fcb2012-04-04 11:15:54 +1000383 memory_region_set_dirty(section->mr, addr,
384 TARGET_PAGE_SIZE * hpratio);
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300385 } while (c != 0);
386 }
387 }
388 return 0;
Alexander Graf96c16062009-07-27 12:49:56 +0200389}
390
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300391#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
392
aliguori5832d1f2008-11-24 19:36:26 +0000393/**
394 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
Blue Swirlfd4aa972011-10-16 16:04:59 +0000395 * This function updates qemu's dirty bitmap using
396 * memory_region_set_dirty(). This means all bits are set
397 * to dirty.
aliguori5832d1f2008-11-24 19:36:26 +0000398 *
aliguorid3f8d372009-04-17 14:26:29 +0000399 * @start_add: start of logged region.
aliguori5832d1f2008-11-24 19:36:26 +0000400 * @end_addr: end of logged region.
401 */
Avi Kivityffcde122011-12-19 13:18:13 +0200402static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
aliguori5832d1f2008-11-24 19:36:26 +0000403{
404 KVMState *s = kvm_state;
Jan Kiszka151f7742009-05-01 20:52:47 +0200405 unsigned long size, allocated_size = 0;
Jan Kiszka151f7742009-05-01 20:52:47 +0200406 KVMDirtyLog d;
407 KVMSlot *mem;
408 int ret = 0;
Avi Kivityffcde122011-12-19 13:18:13 +0200409 target_phys_addr_t start_addr = section->offset_within_address_space;
410 target_phys_addr_t end_addr = start_addr + section->size;
aliguori5832d1f2008-11-24 19:36:26 +0000411
Jan Kiszka151f7742009-05-01 20:52:47 +0200412 d.dirty_bitmap = NULL;
413 while (start_addr < end_addr) {
414 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
415 if (mem == NULL) {
416 break;
417 }
418
Michael Tokarev51b0c602011-04-26 20:13:49 +0400419 /* XXX bad kernel interface alert
420 * For dirty bitmap, kernel allocates array of size aligned to
421 * bits-per-long. But for case when the kernel is 64bits and
422 * the userspace is 32bits, userspace can't align to the same
423 * bits-per-long, since sizeof(long) is different between kernel
424 * and user space. This way, userspace will provide buffer which
425 * may be 4 bytes less than the kernel will use, resulting in
426 * userspace memory corruption (which is not detectable by valgrind
427 * too, in most cases).
428 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
429 * a hope that sizeof(long) wont become >8 any time soon.
430 */
431 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
432 /*HOST_LONG_BITS*/ 64) / 8;
Jan Kiszka151f7742009-05-01 20:52:47 +0200433 if (!d.dirty_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500434 d.dirty_bitmap = g_malloc(size);
Jan Kiszka151f7742009-05-01 20:52:47 +0200435 } else if (size > allocated_size) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500436 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
Jan Kiszka151f7742009-05-01 20:52:47 +0200437 }
438 allocated_size = size;
439 memset(d.dirty_bitmap, 0, allocated_size);
440
441 d.slot = mem->slot;
442
Anthony Liguori6e489f32009-07-27 15:23:59 -0500443 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
Blue Swirl8c0d5772010-04-18 14:22:14 +0000444 DPRINTF("ioctl failed %d\n", errno);
Jan Kiszka151f7742009-05-01 20:52:47 +0200445 ret = -1;
446 break;
447 }
448
Avi Kivityffcde122011-12-19 13:18:13 +0200449 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
Marcelo Tosatti8369e012010-04-23 14:04:14 -0300450 start_addr = mem->start_addr + mem->memory_size;
aliguori5832d1f2008-11-24 19:36:26 +0000451 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500452 g_free(d.dirty_bitmap);
Jan Kiszka151f7742009-05-01 20:52:47 +0200453
454 return ret;
aliguori5832d1f2008-11-24 19:36:26 +0000455}
456
Anthony Liguoric227f092009-10-01 16:12:16 -0500457int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +0000458{
459 int ret = -ENOSYS;
aliguorif65ed4c2008-12-09 20:09:57 +0000460 KVMState *s = kvm_state;
461
462 if (s->coalesced_mmio) {
463 struct kvm_coalesced_mmio_zone zone;
464
465 zone.addr = start;
466 zone.size = size;
Michael S. Tsirkin7e680752012-02-29 17:54:29 +0200467 zone.pad = 0;
aliguorif65ed4c2008-12-09 20:09:57 +0000468
469 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
470 }
aliguorif65ed4c2008-12-09 20:09:57 +0000471
472 return ret;
473}
474
Anthony Liguoric227f092009-10-01 16:12:16 -0500475int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +0000476{
477 int ret = -ENOSYS;
aliguorif65ed4c2008-12-09 20:09:57 +0000478 KVMState *s = kvm_state;
479
480 if (s->coalesced_mmio) {
481 struct kvm_coalesced_mmio_zone zone;
482
483 zone.addr = start;
484 zone.size = size;
Michael S. Tsirkin7e680752012-02-29 17:54:29 +0200485 zone.pad = 0;
aliguorif65ed4c2008-12-09 20:09:57 +0000486
487 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
488 }
aliguorif65ed4c2008-12-09 20:09:57 +0000489
490 return ret;
491}
492
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500493int kvm_check_extension(KVMState *s, unsigned int extension)
494{
495 int ret;
496
497 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
498 if (ret < 0) {
499 ret = 0;
500 }
501
502 return ret;
503}
504
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200505static int kvm_check_many_ioeventfds(void)
506{
Stefan Hajnoczid0dcac82011-01-25 16:17:14 +0000507 /* Userspace can use ioeventfd for io notification. This requires a host
508 * that supports eventfd(2) and an I/O thread; since eventfd does not
509 * support SIGIO it cannot interrupt the vcpu.
510 *
511 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200512 * can avoid creating too many ioeventfds.
513 */
Anthony Liguori12d45362011-08-22 08:24:58 -0500514#if defined(CONFIG_EVENTFD)
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200515 int ioeventfds[7];
516 int i, ret = 0;
517 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
518 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
519 if (ioeventfds[i] < 0) {
520 break;
521 }
522 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
523 if (ret < 0) {
524 close(ioeventfds[i]);
525 break;
526 }
527 }
528
529 /* Decide whether many devices are supported or not */
530 ret = i == ARRAY_SIZE(ioeventfds);
531
532 while (i-- > 0) {
533 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
534 close(ioeventfds[i]);
535 }
536 return ret;
537#else
538 return 0;
539#endif
540}
541
Jan Kiszka94a8d392011-01-21 21:48:17 +0100542static const KVMCapabilityInfo *
543kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
544{
545 while (list->name) {
546 if (!kvm_check_extension(s, list->value)) {
547 return list;
548 }
549 list++;
550 }
551 return NULL;
552}
553
Avi Kivitya01672d2011-12-18 14:06:05 +0200554static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200555{
556 KVMState *s = kvm_state;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200557 KVMSlot *mem, old;
558 int err;
Avi Kivitya01672d2011-12-18 14:06:05 +0200559 MemoryRegion *mr = section->mr;
560 bool log_dirty = memory_region_is_logging(mr);
561 target_phys_addr_t start_addr = section->offset_within_address_space;
562 ram_addr_t size = section->size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200563 void *ram = NULL;
Avi Kivity8f6f9622012-02-29 13:22:12 +0200564 unsigned delta;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200565
Gleb Natapov14542fe2010-07-28 18:13:23 +0300566 /* kvm works in page size chunks, but the function may be called
567 with sub-page size and unaligned start address. */
Avi Kivity8f6f9622012-02-29 13:22:12 +0200568 delta = TARGET_PAGE_ALIGN(size) - size;
569 if (delta > size) {
570 return;
571 }
572 start_addr += delta;
573 size -= delta;
574 size &= TARGET_PAGE_MASK;
575 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
576 return;
577 }
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200578
Avi Kivitya01672d2011-12-18 14:06:05 +0200579 if (!memory_region_is_ram(mr)) {
580 return;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200581 }
582
Avi Kivity8f6f9622012-02-29 13:22:12 +0200583 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
Avi Kivitya01672d2011-12-18 14:06:05 +0200584
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200585 while (1) {
586 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
587 if (!mem) {
588 break;
589 }
590
Avi Kivitya01672d2011-12-18 14:06:05 +0200591 if (add && start_addr >= mem->start_addr &&
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200592 (start_addr + size <= mem->start_addr + mem->memory_size) &&
Avi Kivity9f213ed2011-12-15 19:55:26 +0200593 (ram - start_addr == mem->ram - mem->start_addr)) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200594 /* The new slot fits into the existing one and comes with
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300595 * identical parameters - update flags and done. */
596 kvm_slot_dirty_pages_log_change(mem, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200597 return;
598 }
599
600 old = *mem;
601
Avi Kivity3fbffb62012-01-15 16:13:59 +0200602 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
603 kvm_physical_sync_dirty_bitmap(section);
604 }
605
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200606 /* unregister the overlapping slot */
607 mem->memory_size = 0;
608 err = kvm_set_user_memory_region(s, mem);
609 if (err) {
610 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
611 __func__, strerror(-err));
612 abort();
613 }
614
615 /* Workaround for older KVM versions: we can't join slots, even not by
616 * unregistering the previous ones and then registering the larger
617 * slot. We have to maintain the existing fragmentation. Sigh.
618 *
619 * This workaround assumes that the new slot starts at the same
620 * address as the first existing one. If not or if some overlapping
621 * slot comes around later, we will fail (not seen in practice so far)
622 * - and actually require a recent KVM version. */
623 if (s->broken_set_mem_region &&
Avi Kivitya01672d2011-12-18 14:06:05 +0200624 old.start_addr == start_addr && old.memory_size < size && add) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200625 mem = kvm_alloc_slot(s);
626 mem->memory_size = old.memory_size;
627 mem->start_addr = old.start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200628 mem->ram = old.ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300629 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200630
631 err = kvm_set_user_memory_region(s, mem);
632 if (err) {
633 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
634 strerror(-err));
635 abort();
636 }
637
638 start_addr += old.memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200639 ram += old.memory_size;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200640 size -= old.memory_size;
641 continue;
642 }
643
644 /* register prefix slot */
645 if (old.start_addr < start_addr) {
646 mem = kvm_alloc_slot(s);
647 mem->memory_size = start_addr - old.start_addr;
648 mem->start_addr = old.start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200649 mem->ram = old.ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300650 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200651
652 err = kvm_set_user_memory_region(s, mem);
653 if (err) {
654 fprintf(stderr, "%s: error registering prefix slot: %s\n",
655 __func__, strerror(-err));
Alexander Grafd4d68682011-04-16 10:15:11 +0200656#ifdef TARGET_PPC
657 fprintf(stderr, "%s: This is probably because your kernel's " \
658 "PAGE_SIZE is too big. Please try to use 4k " \
659 "PAGE_SIZE!\n", __func__);
660#endif
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200661 abort();
662 }
663 }
664
665 /* register suffix slot */
666 if (old.start_addr + old.memory_size > start_addr + size) {
667 ram_addr_t size_delta;
668
669 mem = kvm_alloc_slot(s);
670 mem->start_addr = start_addr + size;
671 size_delta = mem->start_addr - old.start_addr;
672 mem->memory_size = old.memory_size - size_delta;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200673 mem->ram = old.ram + size_delta;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300674 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200675
676 err = kvm_set_user_memory_region(s, mem);
677 if (err) {
678 fprintf(stderr, "%s: error registering suffix slot: %s\n",
679 __func__, strerror(-err));
680 abort();
681 }
682 }
683 }
684
685 /* in case the KVM bug workaround already "consumed" the new slot */
Jan Kiszkaa426e122011-01-04 09:32:13 +0100686 if (!size) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200687 return;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100688 }
Avi Kivitya01672d2011-12-18 14:06:05 +0200689 if (!add) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200690 return;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100691 }
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200692 mem = kvm_alloc_slot(s);
693 mem->memory_size = size;
694 mem->start_addr = start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200695 mem->ram = ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300696 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200697
698 err = kvm_set_user_memory_region(s, mem);
699 if (err) {
700 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
701 strerror(-err));
702 abort();
703 }
704}
705
Avi Kivity50c1e142012-02-08 21:36:02 +0200706static void kvm_begin(MemoryListener *listener)
707{
708}
709
710static void kvm_commit(MemoryListener *listener)
711{
712}
713
Avi Kivitya01672d2011-12-18 14:06:05 +0200714static void kvm_region_add(MemoryListener *listener,
715 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200716{
Avi Kivitya01672d2011-12-18 14:06:05 +0200717 kvm_set_phys_mem(section, true);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200718}
719
Avi Kivitya01672d2011-12-18 14:06:05 +0200720static void kvm_region_del(MemoryListener *listener,
721 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200722{
Avi Kivitya01672d2011-12-18 14:06:05 +0200723 kvm_set_phys_mem(section, false);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200724}
725
Avi Kivity50c1e142012-02-08 21:36:02 +0200726static void kvm_region_nop(MemoryListener *listener,
727 MemoryRegionSection *section)
728{
729}
730
Avi Kivitya01672d2011-12-18 14:06:05 +0200731static void kvm_log_sync(MemoryListener *listener,
732 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200733{
Avi Kivitya01672d2011-12-18 14:06:05 +0200734 int r;
735
Avi Kivityffcde122011-12-19 13:18:13 +0200736 r = kvm_physical_sync_dirty_bitmap(section);
Avi Kivitya01672d2011-12-18 14:06:05 +0200737 if (r < 0) {
738 abort();
739 }
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200740}
741
Avi Kivitya01672d2011-12-18 14:06:05 +0200742static void kvm_log_global_start(struct MemoryListener *listener)
743{
744 int r;
745
746 r = kvm_set_migration_log(1);
747 assert(r >= 0);
748}
749
750static void kvm_log_global_stop(struct MemoryListener *listener)
751{
752 int r;
753
754 r = kvm_set_migration_log(0);
755 assert(r >= 0);
756}
757
Avi Kivity80a1ea32012-02-08 16:39:06 +0200758static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
759 bool match_data, uint64_t data, int fd)
760{
761 int r;
762
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200763 assert(match_data && section->size <= 8);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200764
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200765 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
766 data, true, section->size);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200767 if (r < 0) {
768 abort();
769 }
770}
771
772static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
773 bool match_data, uint64_t data, int fd)
774{
775 int r;
776
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200777 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
778 data, false, section->size);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200779 if (r < 0) {
780 abort();
781 }
782}
783
784static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
785 bool match_data, uint64_t data, int fd)
786{
787 int r;
788
789 assert(match_data && section->size == 2);
790
791 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
792 data, true);
793 if (r < 0) {
794 abort();
795 }
796}
797
798static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
799 bool match_data, uint64_t data, int fd)
800
801{
802 int r;
803
804 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
805 data, false);
806 if (r < 0) {
807 abort();
808 }
809}
810
811static void kvm_eventfd_add(MemoryListener *listener,
812 MemoryRegionSection *section,
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200813 bool match_data, uint64_t data,
814 EventNotifier *e)
Avi Kivity80a1ea32012-02-08 16:39:06 +0200815{
816 if (section->address_space == get_system_memory()) {
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200817 kvm_mem_ioeventfd_add(section, match_data, data,
818 event_notifier_get_fd(e));
Avi Kivity80a1ea32012-02-08 16:39:06 +0200819 } else {
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200820 kvm_io_ioeventfd_add(section, match_data, data,
821 event_notifier_get_fd(e));
Avi Kivity80a1ea32012-02-08 16:39:06 +0200822 }
823}
824
825static void kvm_eventfd_del(MemoryListener *listener,
826 MemoryRegionSection *section,
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200827 bool match_data, uint64_t data,
828 EventNotifier *e)
Avi Kivity80a1ea32012-02-08 16:39:06 +0200829{
830 if (section->address_space == get_system_memory()) {
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200831 kvm_mem_ioeventfd_del(section, match_data, data,
832 event_notifier_get_fd(e));
Avi Kivity80a1ea32012-02-08 16:39:06 +0200833 } else {
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200834 kvm_io_ioeventfd_del(section, match_data, data,
835 event_notifier_get_fd(e));
Avi Kivity80a1ea32012-02-08 16:39:06 +0200836 }
837}
838
Avi Kivitya01672d2011-12-18 14:06:05 +0200839static MemoryListener kvm_memory_listener = {
Avi Kivity50c1e142012-02-08 21:36:02 +0200840 .begin = kvm_begin,
841 .commit = kvm_commit,
Avi Kivitya01672d2011-12-18 14:06:05 +0200842 .region_add = kvm_region_add,
843 .region_del = kvm_region_del,
Avi Kivity50c1e142012-02-08 21:36:02 +0200844 .region_nop = kvm_region_nop,
Anthony PERARDe5896b12011-02-07 12:19:23 +0100845 .log_start = kvm_log_start,
846 .log_stop = kvm_log_stop,
Avi Kivitya01672d2011-12-18 14:06:05 +0200847 .log_sync = kvm_log_sync,
848 .log_global_start = kvm_log_global_start,
849 .log_global_stop = kvm_log_global_stop,
Avi Kivity80a1ea32012-02-08 16:39:06 +0200850 .eventfd_add = kvm_eventfd_add,
851 .eventfd_del = kvm_eventfd_del,
Avi Kivity72e22d22012-02-08 15:05:50 +0200852 .priority = 10,
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200853};
854
Andreas Färber9349b4f2012-03-14 01:38:32 +0100855static void kvm_handle_interrupt(CPUArchState *env, int mask)
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +0200856{
857 env->interrupt_request |= mask;
858
859 if (!qemu_cpu_is_self(env)) {
860 qemu_cpu_kick(env);
861 }
862}
863
Peter Maydell3889c3f2012-07-26 15:35:12 +0100864int kvm_set_irq(KVMState *s, int irq, int level)
Jan Kiszka84b058d2011-10-15 11:49:47 +0200865{
866 struct kvm_irq_level event;
867 int ret;
868
Peter Maydell7ae26bd2012-07-26 15:35:11 +0100869 assert(kvm_async_interrupts_enabled());
Jan Kiszka84b058d2011-10-15 11:49:47 +0200870
871 event.level = level;
872 event.irq = irq;
873 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
874 if (ret < 0) {
Peter Maydell3889c3f2012-07-26 15:35:12 +0100875 perror("kvm_set_irq");
Jan Kiszka84b058d2011-10-15 11:49:47 +0200876 abort();
877 }
878
879 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
880}
881
882#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszkad3d3bef2012-06-05 21:03:57 +0200883typedef struct KVMMSIRoute {
884 struct kvm_irq_routing_entry kroute;
885 QTAILQ_ENTRY(KVMMSIRoute) entry;
886} KVMMSIRoute;
887
Jan Kiszka84b058d2011-10-15 11:49:47 +0200888static void set_gsi(KVMState *s, unsigned int gsi)
889{
Jan Kiszka84b058d2011-10-15 11:49:47 +0200890 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
891}
892
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300893static void clear_gsi(KVMState *s, unsigned int gsi)
894{
895 s->used_gsi_bitmap[gsi / 32] &= ~(1U << (gsi % 32));
896}
897
Jan Kiszka84b058d2011-10-15 11:49:47 +0200898static void kvm_init_irq_routing(KVMState *s)
899{
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300900 int gsi_count, i;
Jan Kiszka84b058d2011-10-15 11:49:47 +0200901
902 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
903 if (gsi_count > 0) {
904 unsigned int gsi_bits, i;
905
906 /* Round up so we can search ints using ffs */
Jason Baronbc8c6782012-03-28 14:18:05 -0400907 gsi_bits = ALIGN(gsi_count, 32);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200908 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
Jan Kiszka4e2e4e62012-05-16 15:41:08 -0300909 s->gsi_count = gsi_count;
Jan Kiszka84b058d2011-10-15 11:49:47 +0200910
911 /* Mark any over-allocated bits as already in use */
912 for (i = gsi_count; i < gsi_bits; i++) {
913 set_gsi(s, i);
914 }
915 }
916
917 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
918 s->nr_allocated_irq_routes = 0;
919
Jan Kiszka4a3adeb2012-05-16 15:41:14 -0300920 if (!s->direct_msi) {
921 for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) {
922 QTAILQ_INIT(&s->msi_hashtab[i]);
923 }
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300924 }
925
Jan Kiszka84b058d2011-10-15 11:49:47 +0200926 kvm_arch_init_irq_routing(s);
927}
928
Jan Kiszkae7b20302012-05-17 10:32:35 -0300929static void kvm_irqchip_commit_routes(KVMState *s)
930{
931 int ret;
932
933 s->irq_routes->flags = 0;
934 ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
935 assert(ret == 0);
936}
937
Jan Kiszka84b058d2011-10-15 11:49:47 +0200938static void kvm_add_routing_entry(KVMState *s,
939 struct kvm_irq_routing_entry *entry)
940{
941 struct kvm_irq_routing_entry *new;
942 int n, size;
943
944 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
945 n = s->nr_allocated_irq_routes * 2;
946 if (n < 64) {
947 n = 64;
948 }
949 size = sizeof(struct kvm_irq_routing);
950 size += n * sizeof(*new);
951 s->irq_routes = g_realloc(s->irq_routes, size);
952 s->nr_allocated_irq_routes = n;
953 }
954 n = s->irq_routes->nr++;
955 new = &s->irq_routes->entries[n];
956 memset(new, 0, sizeof(*new));
957 new->gsi = entry->gsi;
958 new->type = entry->type;
959 new->flags = entry->flags;
960 new->u = entry->u;
961
962 set_gsi(s, entry->gsi);
Jan Kiszkae7b20302012-05-17 10:32:35 -0300963
964 kvm_irqchip_commit_routes(s);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200965}
966
Jan Kiszkacc574072012-08-27 08:28:38 +0200967static int kvm_update_routing_entry(KVMState *s,
968 struct kvm_irq_routing_entry *new_entry)
969{
970 struct kvm_irq_routing_entry *entry;
971 int n;
972
973 for (n = 0; n < s->irq_routes->nr; n++) {
974 entry = &s->irq_routes->entries[n];
975 if (entry->gsi != new_entry->gsi) {
976 continue;
977 }
978
979 entry->type = new_entry->type;
980 entry->flags = new_entry->flags;
981 entry->u = new_entry->u;
982
983 kvm_irqchip_commit_routes(s);
984
985 return 0;
986 }
987
988 return -ESRCH;
989}
990
Jan Kiszka1df186d2012-05-17 10:32:32 -0300991void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin)
Jan Kiszka84b058d2011-10-15 11:49:47 +0200992{
993 struct kvm_irq_routing_entry e;
994
Jan Kiszka4e2e4e62012-05-16 15:41:08 -0300995 assert(pin < s->gsi_count);
996
Jan Kiszka84b058d2011-10-15 11:49:47 +0200997 e.gsi = irq;
998 e.type = KVM_IRQ_ROUTING_IRQCHIP;
999 e.flags = 0;
1000 e.u.irqchip.irqchip = irqchip;
1001 e.u.irqchip.pin = pin;
1002 kvm_add_routing_entry(s, &e);
1003}
1004
Jan Kiszka1e2aa8b2012-05-17 10:32:34 -03001005void kvm_irqchip_release_virq(KVMState *s, int virq)
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001006{
1007 struct kvm_irq_routing_entry *e;
1008 int i;
1009
1010 for (i = 0; i < s->irq_routes->nr; i++) {
1011 e = &s->irq_routes->entries[i];
1012 if (e->gsi == virq) {
1013 s->irq_routes->nr--;
1014 *e = s->irq_routes->entries[s->irq_routes->nr];
1015 }
1016 }
1017 clear_gsi(s, virq);
Jan Kiszkae7b20302012-05-17 10:32:35 -03001018
1019 kvm_irqchip_commit_routes(s);
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001020}
1021
1022static unsigned int kvm_hash_msi(uint32_t data)
1023{
1024 /* This is optimized for IA32 MSI layout. However, no other arch shall
1025 * repeat the mistake of not providing a direct MSI injection API. */
1026 return data & 0xff;
1027}
1028
1029static void kvm_flush_dynamic_msi_routes(KVMState *s)
1030{
1031 KVMMSIRoute *route, *next;
1032 unsigned int hash;
1033
1034 for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) {
1035 QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) {
1036 kvm_irqchip_release_virq(s, route->kroute.gsi);
1037 QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry);
1038 g_free(route);
1039 }
1040 }
1041}
1042
1043static int kvm_irqchip_get_virq(KVMState *s)
1044{
1045 uint32_t *word = s->used_gsi_bitmap;
1046 int max_words = ALIGN(s->gsi_count, 32) / 32;
1047 int i, bit;
1048 bool retry = true;
1049
1050again:
1051 /* Return the lowest unused GSI in the bitmap */
1052 for (i = 0; i < max_words; i++) {
1053 bit = ffs(~word[i]);
1054 if (!bit) {
1055 continue;
1056 }
1057
1058 return bit - 1 + i * 32;
1059 }
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001060 if (!s->direct_msi && retry) {
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001061 retry = false;
1062 kvm_flush_dynamic_msi_routes(s);
1063 goto again;
1064 }
1065 return -ENOSPC;
1066
1067}
1068
1069static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
1070{
1071 unsigned int hash = kvm_hash_msi(msg.data);
1072 KVMMSIRoute *route;
1073
1074 QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) {
1075 if (route->kroute.u.msi.address_lo == (uint32_t)msg.address &&
1076 route->kroute.u.msi.address_hi == (msg.address >> 32) &&
1077 route->kroute.u.msi.data == msg.data) {
1078 return route;
1079 }
1080 }
1081 return NULL;
1082}
1083
1084int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1085{
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001086 struct kvm_msi msi;
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001087 KVMMSIRoute *route;
1088
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001089 if (s->direct_msi) {
1090 msi.address_lo = (uint32_t)msg.address;
1091 msi.address_hi = msg.address >> 32;
1092 msi.data = msg.data;
1093 msi.flags = 0;
1094 memset(msi.pad, 0, sizeof(msi.pad));
1095
1096 return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
1097 }
1098
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001099 route = kvm_lookup_msi_route(s, msg);
1100 if (!route) {
Jan Kiszkae7b20302012-05-17 10:32:35 -03001101 int virq;
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001102
1103 virq = kvm_irqchip_get_virq(s);
1104 if (virq < 0) {
1105 return virq;
1106 }
1107
1108 route = g_malloc(sizeof(KVMMSIRoute));
1109 route->kroute.gsi = virq;
1110 route->kroute.type = KVM_IRQ_ROUTING_MSI;
1111 route->kroute.flags = 0;
1112 route->kroute.u.msi.address_lo = (uint32_t)msg.address;
1113 route->kroute.u.msi.address_hi = msg.address >> 32;
1114 route->kroute.u.msi.data = msg.data;
1115
1116 kvm_add_routing_entry(s, &route->kroute);
1117
1118 QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route,
1119 entry);
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001120 }
1121
1122 assert(route->kroute.type == KVM_IRQ_ROUTING_MSI);
1123
Peter Maydell3889c3f2012-07-26 15:35:12 +01001124 return kvm_set_irq(s, route->kroute.gsi, 1);
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001125}
1126
Jan Kiszka92b4e482012-05-17 10:32:33 -03001127int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1128{
1129 struct kvm_irq_routing_entry kroute;
1130 int virq;
1131
Peter Maydellf3e1bed2012-07-26 15:35:16 +01001132 if (!kvm_gsi_routing_enabled()) {
Jan Kiszka92b4e482012-05-17 10:32:33 -03001133 return -ENOSYS;
1134 }
1135
1136 virq = kvm_irqchip_get_virq(s);
1137 if (virq < 0) {
1138 return virq;
1139 }
1140
1141 kroute.gsi = virq;
1142 kroute.type = KVM_IRQ_ROUTING_MSI;
1143 kroute.flags = 0;
1144 kroute.u.msi.address_lo = (uint32_t)msg.address;
1145 kroute.u.msi.address_hi = msg.address >> 32;
1146 kroute.u.msi.data = msg.data;
1147
1148 kvm_add_routing_entry(s, &kroute);
1149
1150 return virq;
1151}
1152
Jan Kiszkacc574072012-08-27 08:28:38 +02001153int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg)
1154{
1155 struct kvm_irq_routing_entry kroute;
1156
1157 if (!kvm_irqchip_in_kernel()) {
1158 return -ENOSYS;
1159 }
1160
1161 kroute.gsi = virq;
1162 kroute.type = KVM_IRQ_ROUTING_MSI;
1163 kroute.flags = 0;
1164 kroute.u.msi.address_lo = (uint32_t)msg.address;
1165 kroute.u.msi.address_hi = msg.address >> 32;
1166 kroute.u.msi.data = msg.data;
1167
1168 return kvm_update_routing_entry(s, &kroute);
1169}
1170
Jan Kiszka39853bb2012-05-17 10:32:36 -03001171static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1172{
1173 struct kvm_irqfd irqfd = {
1174 .fd = fd,
1175 .gsi = virq,
1176 .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN,
1177 };
1178
Peter Maydellcc7e0dd2012-07-26 15:35:14 +01001179 if (!kvm_irqfds_enabled()) {
Jan Kiszka39853bb2012-05-17 10:32:36 -03001180 return -ENOSYS;
1181 }
1182
1183 return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd);
1184}
1185
Jan Kiszka84b058d2011-10-15 11:49:47 +02001186#else /* !KVM_CAP_IRQ_ROUTING */
1187
1188static void kvm_init_irq_routing(KVMState *s)
1189{
1190}
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001191
Jan Kiszkad3d3bef2012-06-05 21:03:57 +02001192void kvm_irqchip_release_virq(KVMState *s, int virq)
1193{
1194}
1195
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001196int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1197{
1198 abort();
1199}
Jan Kiszka92b4e482012-05-17 10:32:33 -03001200
1201int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1202{
Alex Williamsondf410672012-06-25 09:40:39 -06001203 return -ENOSYS;
Jan Kiszka92b4e482012-05-17 10:32:33 -03001204}
Jan Kiszka39853bb2012-05-17 10:32:36 -03001205
1206static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1207{
1208 abort();
1209}
Jan Kiszka84b058d2011-10-15 11:49:47 +02001210#endif /* !KVM_CAP_IRQ_ROUTING */
1211
Jan Kiszkab131c742012-08-20 10:55:56 +02001212int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, int virq)
Jan Kiszka39853bb2012-05-17 10:32:36 -03001213{
Jan Kiszkab131c742012-08-20 10:55:56 +02001214 return kvm_irqchip_assign_irqfd(s, event_notifier_get_fd(n), virq, true);
Jan Kiszka39853bb2012-05-17 10:32:36 -03001215}
1216
Jan Kiszkab131c742012-08-20 10:55:56 +02001217int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, int virq)
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02001218{
Jan Kiszkab131c742012-08-20 10:55:56 +02001219 return kvm_irqchip_assign_irqfd(s, event_notifier_get_fd(n), virq, false);
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02001220}
1221
Jan Kiszka84b058d2011-10-15 11:49:47 +02001222static int kvm_irqchip_create(KVMState *s)
1223{
1224 QemuOptsList *list = qemu_find_opts("machine");
1225 int ret;
1226
1227 if (QTAILQ_EMPTY(&list->head) ||
1228 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
Jan Kiszkaa24b9102012-05-16 15:41:15 -03001229 "kernel_irqchip", true) ||
Jan Kiszka84b058d2011-10-15 11:49:47 +02001230 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
1231 return 0;
1232 }
1233
1234 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
1235 if (ret < 0) {
1236 fprintf(stderr, "Create kernel irqchip failed\n");
1237 return ret;
1238 }
1239
Jan Kiszka3d4b2642012-01-31 19:17:52 +01001240 kvm_kernel_irqchip = true;
Peter Maydell7ae26bd2012-07-26 15:35:11 +01001241 /* If we have an in-kernel IRQ chip then we must have asynchronous
1242 * interrupt delivery (though the reverse is not necessarily true)
1243 */
1244 kvm_async_interrupts_allowed = true;
Jan Kiszka84b058d2011-10-15 11:49:47 +02001245
1246 kvm_init_irq_routing(s);
1247
1248 return 0;
1249}
1250
Dunrong Huang3ed444e2012-07-31 19:18:17 +08001251static int kvm_max_vcpus(KVMState *s)
1252{
1253 int ret;
1254
1255 /* Find number of supported CPUs using the recommended
1256 * procedure from the kernel API documentation to cope with
1257 * older kernels that may be missing capabilities.
1258 */
1259 ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
1260 if (ret) {
1261 return ret;
1262 }
1263 ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS);
1264 if (ret) {
1265 return ret;
1266 }
1267
1268 return 4;
1269}
1270
Jan Kiszkacad1e282011-01-21 21:48:16 +01001271int kvm_init(void)
aliguori05330442008-11-05 16:29:27 +00001272{
Jan Kiszka168ccc12009-06-07 11:30:25 +02001273 static const char upgrade_note[] =
1274 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1275 "(see http://sourceforge.net/projects/kvm).\n";
aliguori05330442008-11-05 16:29:27 +00001276 KVMState *s;
Jan Kiszka94a8d392011-01-21 21:48:17 +01001277 const KVMCapabilityInfo *missing_cap;
aliguori05330442008-11-05 16:29:27 +00001278 int ret;
1279 int i;
Dunrong Huang3ed444e2012-07-31 19:18:17 +08001280 int max_vcpus;
aliguori05330442008-11-05 16:29:27 +00001281
Anthony Liguori7267c092011-08-20 22:09:37 -05001282 s = g_malloc0(sizeof(KVMState));
aliguori05330442008-11-05 16:29:27 +00001283
David Gibson3145fcb2012-04-04 11:15:54 +10001284 /*
1285 * On systems where the kernel can support different base page
1286 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1287 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1288 * page size for the system though.
1289 */
1290 assert(TARGET_PAGE_SIZE <= getpagesize());
1291
aliguorie22a25c2009-03-12 20:12:48 +00001292#ifdef KVM_CAP_SET_GUEST_DEBUG
Blue Swirl72cf2d42009-09-12 07:36:22 +00001293 QTAILQ_INIT(&s->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +00001294#endif
Jan Kiszkaa426e122011-01-04 09:32:13 +01001295 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
aliguori05330442008-11-05 16:29:27 +00001296 s->slots[i].slot = i;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001297 }
aliguori05330442008-11-05 16:29:27 +00001298 s->vmfd = -1;
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001299 s->fd = qemu_open("/dev/kvm", O_RDWR);
aliguori05330442008-11-05 16:29:27 +00001300 if (s->fd == -1) {
1301 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1302 ret = -errno;
1303 goto err;
1304 }
1305
1306 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1307 if (ret < KVM_API_VERSION) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001308 if (ret > 0) {
aliguori05330442008-11-05 16:29:27 +00001309 ret = -EINVAL;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001310 }
aliguori05330442008-11-05 16:29:27 +00001311 fprintf(stderr, "kvm version too old\n");
1312 goto err;
1313 }
1314
1315 if (ret > KVM_API_VERSION) {
1316 ret = -EINVAL;
1317 fprintf(stderr, "kvm version not supported\n");
1318 goto err;
1319 }
1320
Dunrong Huang3ed444e2012-07-31 19:18:17 +08001321 max_vcpus = kvm_max_vcpus(s);
1322 if (smp_cpus > max_vcpus) {
1323 ret = -EINVAL;
1324 fprintf(stderr, "Number of SMP cpus requested (%d) exceeds max cpus "
1325 "supported by KVM (%d)\n", smp_cpus, max_vcpus);
1326 goto err;
1327 }
1328
aliguori05330442008-11-05 16:29:27 +00001329 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
Alexander Graf0104dca2010-04-01 18:42:37 +02001330 if (s->vmfd < 0) {
1331#ifdef TARGET_S390X
1332 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1333 "your host kernel command line\n");
1334#endif
Xu He Jiedb9eae12011-10-27 10:15:13 +08001335 ret = s->vmfd;
aliguori05330442008-11-05 16:29:27 +00001336 goto err;
Alexander Graf0104dca2010-04-01 18:42:37 +02001337 }
aliguori05330442008-11-05 16:29:27 +00001338
Jan Kiszka94a8d392011-01-21 21:48:17 +01001339 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1340 if (!missing_cap) {
1341 missing_cap =
1342 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1343 }
1344 if (missing_cap) {
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001345 ret = -EINVAL;
Jan Kiszka94a8d392011-01-21 21:48:17 +01001346 fprintf(stderr, "kvm does not support %s\n%s",
1347 missing_cap->name, upgrade_note);
aliguori05330442008-11-05 16:29:27 +00001348 goto err;
1349 }
1350
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001351 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
aliguorif65ed4c2008-12-09 20:09:57 +00001352
Jan Kiszkae69917e2009-05-01 20:42:15 +02001353 s->broken_set_mem_region = 1;
Lai Jiangshan14a09512010-12-10 15:52:36 +08001354 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
Jan Kiszkae69917e2009-05-01 20:42:15 +02001355 if (ret > 0) {
1356 s->broken_set_mem_region = 0;
1357 }
Jan Kiszkae69917e2009-05-01 20:42:15 +02001358
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001359#ifdef KVM_CAP_VCPU_EVENTS
1360 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1361#endif
1362
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001363 s->robust_singlestep =
1364 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001365
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001366#ifdef KVM_CAP_DEBUGREGS
1367 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1368#endif
1369
Sheng Yangf1665b22010-06-17 17:53:07 +08001370#ifdef KVM_CAP_XSAVE
1371 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1372#endif
1373
Sheng Yangf1665b22010-06-17 17:53:07 +08001374#ifdef KVM_CAP_XCRS
1375 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1376#endif
1377
Jan Kiszka8a7c7392012-03-02 20:28:48 +01001378#ifdef KVM_CAP_PIT_STATE2
1379 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1380#endif
1381
Jan Kiszkad3d3bef2012-06-05 21:03:57 +02001382#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001383 s->direct_msi = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);
Jan Kiszkad3d3bef2012-06-05 21:03:57 +02001384#endif
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001385
Jan Kiszka3ab73842012-08-27 08:28:39 +02001386 s->intx_set_mask = kvm_check_extension(s, KVM_CAP_PCI_2_3);
1387
Peter Maydell8732fbd2012-08-15 12:08:13 +01001388 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
1389 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
1390 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
1391 }
1392
Jan Kiszkacad1e282011-01-21 21:48:16 +01001393 ret = kvm_arch_init(s);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001394 if (ret < 0) {
aliguori05330442008-11-05 16:29:27 +00001395 goto err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001396 }
aliguori05330442008-11-05 16:29:27 +00001397
Jan Kiszka84b058d2011-10-15 11:49:47 +02001398 ret = kvm_irqchip_create(s);
1399 if (ret < 0) {
1400 goto err;
1401 }
1402
aliguori05330442008-11-05 16:29:27 +00001403 kvm_state = s;
Avi Kivity7376e582012-02-08 21:05:17 +02001404 memory_listener_register(&kvm_memory_listener, NULL);
aliguori05330442008-11-05 16:29:27 +00001405
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001406 s->many_ioeventfds = kvm_check_many_ioeventfds();
1407
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +02001408 cpu_interrupt_handler = kvm_handle_interrupt;
1409
aliguori05330442008-11-05 16:29:27 +00001410 return 0;
1411
1412err:
1413 if (s) {
Xu He Jiedb9eae12011-10-27 10:15:13 +08001414 if (s->vmfd >= 0) {
aliguori05330442008-11-05 16:29:27 +00001415 close(s->vmfd);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001416 }
1417 if (s->fd != -1) {
aliguori05330442008-11-05 16:29:27 +00001418 close(s->fd);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001419 }
aliguori05330442008-11-05 16:29:27 +00001420 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001421 g_free(s);
aliguori05330442008-11-05 16:29:27 +00001422
1423 return ret;
1424}
1425
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001426static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1427 uint32_t count)
aliguori05330442008-11-05 16:29:27 +00001428{
1429 int i;
1430 uint8_t *ptr = data;
1431
1432 for (i = 0; i < count; i++) {
1433 if (direction == KVM_EXIT_IO_IN) {
1434 switch (size) {
1435 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001436 stb_p(ptr, cpu_inb(port));
aliguori05330442008-11-05 16:29:27 +00001437 break;
1438 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001439 stw_p(ptr, cpu_inw(port));
aliguori05330442008-11-05 16:29:27 +00001440 break;
1441 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001442 stl_p(ptr, cpu_inl(port));
aliguori05330442008-11-05 16:29:27 +00001443 break;
1444 }
1445 } else {
1446 switch (size) {
1447 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001448 cpu_outb(port, ldub_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001449 break;
1450 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001451 cpu_outw(port, lduw_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001452 break;
1453 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001454 cpu_outl(port, ldl_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001455 break;
1456 }
1457 }
1458
1459 ptr += size;
1460 }
aliguori05330442008-11-05 16:29:27 +00001461}
1462
Andreas Färber9349b4f2012-03-14 01:38:32 +01001463static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001464{
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001465 fprintf(stderr, "KVM internal error.");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001466 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1467 int i;
1468
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001469 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001470 for (i = 0; i < run->internal.ndata; ++i) {
1471 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1472 i, (uint64_t)run->internal.data[i]);
1473 }
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001474 } else {
1475 fprintf(stderr, "\n");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001476 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001477 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1478 fprintf(stderr, "emulation failure\n");
Jan Kiszkaa426e122011-01-04 09:32:13 +01001479 if (!kvm_arch_stop_on_emulation_error(env)) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001480 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001481 return EXCP_INTERRUPT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001482 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001483 }
1484 /* FIXME: Should trigger a qmp message to let management know
1485 * something went wrong.
1486 */
Jan Kiszka73aaec42011-01-21 21:48:06 +01001487 return -1;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001488}
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001489
Sheng Yang62a27442010-01-26 19:21:16 +08001490void kvm_flush_coalesced_mmio_buffer(void)
aliguorif65ed4c2008-12-09 20:09:57 +00001491{
aliguorif65ed4c2008-12-09 20:09:57 +00001492 KVMState *s = kvm_state;
Avi Kivity1cae88b2011-10-18 19:43:12 +02001493
1494 if (s->coalesced_flush_in_progress) {
1495 return;
1496 }
1497
1498 s->coalesced_flush_in_progress = true;
1499
Sheng Yang62a27442010-01-26 19:21:16 +08001500 if (s->coalesced_mmio_ring) {
1501 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
aliguorif65ed4c2008-12-09 20:09:57 +00001502 while (ring->first != ring->last) {
1503 struct kvm_coalesced_mmio *ent;
1504
1505 ent = &ring->coalesced_mmio[ring->first];
1506
1507 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
Marcelo Tosatti85199472010-02-22 13:57:54 -03001508 smp_wmb();
aliguorif65ed4c2008-12-09 20:09:57 +00001509 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1510 }
1511 }
Avi Kivity1cae88b2011-10-18 19:43:12 +02001512
1513 s->coalesced_flush_in_progress = false;
aliguorif65ed4c2008-12-09 20:09:57 +00001514}
1515
Jan Kiszka2705d562010-05-04 09:45:23 -03001516static void do_kvm_cpu_synchronize_state(void *_env)
Avi Kivity4c0960c2009-08-17 23:19:53 +03001517{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001518 CPUArchState *env = _env;
Jan Kiszka2705d562010-05-04 09:45:23 -03001519
Jan Kiszka9ded2742010-02-03 21:17:05 +01001520 if (!env->kvm_vcpu_dirty) {
Avi Kivity4c0960c2009-08-17 23:19:53 +03001521 kvm_arch_get_registers(env);
Jan Kiszka9ded2742010-02-03 21:17:05 +01001522 env->kvm_vcpu_dirty = 1;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001523 }
1524}
1525
Andreas Färber9349b4f2012-03-14 01:38:32 +01001526void kvm_cpu_synchronize_state(CPUArchState *env)
Jan Kiszka2705d562010-05-04 09:45:23 -03001527{
Jan Kiszkaa426e122011-01-04 09:32:13 +01001528 if (!env->kvm_vcpu_dirty) {
Jan Kiszka2705d562010-05-04 09:45:23 -03001529 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001530 }
Jan Kiszka2705d562010-05-04 09:45:23 -03001531}
1532
Andreas Färber9349b4f2012-03-14 01:38:32 +01001533void kvm_cpu_synchronize_post_reset(CPUArchState *env)
Jan Kiszkaea375f92010-03-01 19:10:30 +01001534{
1535 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1536 env->kvm_vcpu_dirty = 0;
1537}
1538
Andreas Färber9349b4f2012-03-14 01:38:32 +01001539void kvm_cpu_synchronize_post_init(CPUArchState *env)
Jan Kiszkaea375f92010-03-01 19:10:30 +01001540{
1541 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1542 env->kvm_vcpu_dirty = 0;
1543}
1544
Andreas Färber9349b4f2012-03-14 01:38:32 +01001545int kvm_cpu_exec(CPUArchState *env)
aliguori05330442008-11-05 16:29:27 +00001546{
1547 struct kvm_run *run = env->kvm_run;
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001548 int ret, run_ret;
aliguori05330442008-11-05 16:29:27 +00001549
Blue Swirl8c0d5772010-04-18 14:22:14 +00001550 DPRINTF("kvm_cpu_exec()\n");
aliguori05330442008-11-05 16:29:27 +00001551
Jan Kiszka99036862011-03-02 08:56:13 +01001552 if (kvm_arch_process_async_events(env)) {
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001553 env->exit_request = 0;
Jan Kiszka6792a572011-02-07 12:19:18 +01001554 return EXCP_HLT;
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001555 }
1556
aliguori05330442008-11-05 16:29:27 +00001557 do {
Jan Kiszka9ded2742010-02-03 21:17:05 +01001558 if (env->kvm_vcpu_dirty) {
Jan Kiszkaea375f92010-03-01 19:10:30 +01001559 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
Jan Kiszka9ded2742010-02-03 21:17:05 +01001560 env->kvm_vcpu_dirty = 0;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001561 }
1562
Jan Kiszka8c14c172009-05-30 10:01:45 +02001563 kvm_arch_pre_run(env, run);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001564 if (env->exit_request) {
1565 DPRINTF("interrupt exit requested\n");
1566 /*
1567 * KVM requires us to reenter the kernel after IO exits to complete
1568 * instruction emulation. This self-signal will ensure that we
1569 * leave ASAP again.
1570 */
1571 qemu_cpu_kick_self();
1572 }
Glauber Costad549db52009-10-07 16:38:03 -03001573 qemu_mutex_unlock_iothread();
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001574
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001575 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001576
Glauber Costad549db52009-10-07 16:38:03 -03001577 qemu_mutex_lock_iothread();
aliguori05330442008-11-05 16:29:27 +00001578 kvm_arch_post_run(env, run);
1579
Jan Kiszkab0c883b2011-01-21 21:48:19 +01001580 kvm_flush_coalesced_mmio_buffer();
1581
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001582 if (run_ret < 0) {
Jan Kiszkadc77d342011-03-15 12:26:26 +01001583 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1584 DPRINTF("io window exit\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001585 ret = EXCP_INTERRUPT;
Jan Kiszkadc77d342011-03-15 12:26:26 +01001586 break;
1587 }
Michael Ellerman7b011fb2011-12-16 11:20:20 +11001588 fprintf(stderr, "error: kvm run failed %s\n",
1589 strerror(-run_ret));
aliguori05330442008-11-05 16:29:27 +00001590 abort();
1591 }
1592
aliguori05330442008-11-05 16:29:27 +00001593 switch (run->exit_reason) {
1594 case KVM_EXIT_IO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001595 DPRINTF("handle_io\n");
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001596 kvm_handle_io(run->io.port,
1597 (uint8_t *)run + run->io.data_offset,
1598 run->io.direction,
1599 run->io.size,
1600 run->io.count);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001601 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001602 break;
1603 case KVM_EXIT_MMIO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001604 DPRINTF("handle_mmio\n");
aliguori05330442008-11-05 16:29:27 +00001605 cpu_physical_memory_rw(run->mmio.phys_addr,
1606 run->mmio.data,
1607 run->mmio.len,
1608 run->mmio.is_write);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001609 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001610 break;
1611 case KVM_EXIT_IRQ_WINDOW_OPEN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001612 DPRINTF("irq_window_open\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001613 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001614 break;
1615 case KVM_EXIT_SHUTDOWN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001616 DPRINTF("shutdown\n");
aliguori05330442008-11-05 16:29:27 +00001617 qemu_system_reset_request();
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001618 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001619 break;
1620 case KVM_EXIT_UNKNOWN:
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001621 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1622 (uint64_t)run->hw.hardware_exit_reason);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001623 ret = -1;
aliguori05330442008-11-05 16:29:27 +00001624 break;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001625 case KVM_EXIT_INTERNAL_ERROR:
Jan Kiszka73aaec42011-01-21 21:48:06 +01001626 ret = kvm_handle_internal_error(env, run);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001627 break;
aliguori05330442008-11-05 16:29:27 +00001628 default:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001629 DPRINTF("kvm_arch_handle_exit\n");
aliguori05330442008-11-05 16:29:27 +00001630 ret = kvm_arch_handle_exit(env, run);
1631 break;
1632 }
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001633 } while (ret == 0);
aliguori05330442008-11-05 16:29:27 +00001634
Jan Kiszka73aaec42011-01-21 21:48:06 +01001635 if (ret < 0) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001636 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001637 vm_stop(RUN_STATE_INTERNAL_ERROR);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001638 }
aliguoribecfc392008-11-10 15:55:14 +00001639
Jan Kiszka6792a572011-02-07 12:19:18 +01001640 env->exit_request = 0;
aliguori05330442008-11-05 16:29:27 +00001641 return ret;
1642}
1643
aliguori984b5182008-11-13 19:21:00 +00001644int kvm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001645{
1646 int ret;
aliguori984b5182008-11-13 19:21:00 +00001647 void *arg;
1648 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001649
aliguori984b5182008-11-13 19:21:00 +00001650 va_start(ap, type);
1651 arg = va_arg(ap, void *);
1652 va_end(ap);
1653
1654 ret = ioctl(s->fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001655 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001656 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001657 }
aliguori05330442008-11-05 16:29:27 +00001658 return ret;
1659}
1660
aliguori984b5182008-11-13 19:21:00 +00001661int kvm_vm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001662{
1663 int ret;
aliguori984b5182008-11-13 19:21:00 +00001664 void *arg;
1665 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001666
aliguori984b5182008-11-13 19:21:00 +00001667 va_start(ap, type);
1668 arg = va_arg(ap, void *);
1669 va_end(ap);
1670
1671 ret = ioctl(s->vmfd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001672 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001673 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001674 }
aliguori05330442008-11-05 16:29:27 +00001675 return ret;
1676}
1677
Andreas Färber9349b4f2012-03-14 01:38:32 +01001678int kvm_vcpu_ioctl(CPUArchState *env, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001679{
1680 int ret;
aliguori984b5182008-11-13 19:21:00 +00001681 void *arg;
1682 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001683
aliguori984b5182008-11-13 19:21:00 +00001684 va_start(ap, type);
1685 arg = va_arg(ap, void *);
1686 va_end(ap);
1687
1688 ret = ioctl(env->kvm_fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001689 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001690 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001691 }
aliguori05330442008-11-05 16:29:27 +00001692 return ret;
1693}
aliguoribd322082008-12-04 20:33:06 +00001694
1695int kvm_has_sync_mmu(void)
1696{
Jan Kiszka94a8d392011-01-21 21:48:17 +01001697 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
aliguoribd322082008-12-04 20:33:06 +00001698}
aliguorie22a25c2009-03-12 20:12:48 +00001699
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001700int kvm_has_vcpu_events(void)
1701{
1702 return kvm_state->vcpu_events;
1703}
1704
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001705int kvm_has_robust_singlestep(void)
1706{
1707 return kvm_state->robust_singlestep;
1708}
1709
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001710int kvm_has_debugregs(void)
1711{
1712 return kvm_state->debugregs;
1713}
1714
Sheng Yangf1665b22010-06-17 17:53:07 +08001715int kvm_has_xsave(void)
1716{
1717 return kvm_state->xsave;
1718}
1719
1720int kvm_has_xcrs(void)
1721{
1722 return kvm_state->xcrs;
1723}
1724
Jan Kiszka8a7c7392012-03-02 20:28:48 +01001725int kvm_has_pit_state2(void)
1726{
1727 return kvm_state->pit_state2;
1728}
1729
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001730int kvm_has_many_ioeventfds(void)
1731{
1732 if (!kvm_enabled()) {
1733 return 0;
1734 }
1735 return kvm_state->many_ioeventfds;
1736}
1737
Jan Kiszka84b058d2011-10-15 11:49:47 +02001738int kvm_has_gsi_routing(void)
1739{
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001740#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszka84b058d2011-10-15 11:49:47 +02001741 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001742#else
1743 return false;
1744#endif
Jan Kiszka84b058d2011-10-15 11:49:47 +02001745}
1746
Jan Kiszka3ab73842012-08-27 08:28:39 +02001747int kvm_has_intx_set_mask(void)
1748{
1749 return kvm_state->intx_set_mask;
1750}
1751
Christian Borntraegerfdec9912012-06-15 05:10:30 +00001752void *kvm_vmalloc(ram_addr_t size)
1753{
1754#ifdef TARGET_S390X
1755 void *mem;
1756
1757 mem = kvm_arch_vmalloc(size);
1758 if (mem) {
1759 return mem;
1760 }
1761#endif
1762 return qemu_vmalloc(size);
1763}
1764
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001765void kvm_setup_guest_memory(void *start, size_t size)
1766{
Christian Borntraeger62fe8332012-08-10 15:11:45 +02001767#ifdef CONFIG_VALGRIND_H
1768 VALGRIND_MAKE_MEM_DEFINED(start, size);
1769#endif
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001770 if (!kvm_has_sync_mmu()) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001771 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001772
1773 if (ret) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001774 perror("qemu_madvise");
1775 fprintf(stderr,
1776 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001777 exit(1);
1778 }
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001779 }
1780}
1781
aliguorie22a25c2009-03-12 20:12:48 +00001782#ifdef KVM_CAP_SET_GUEST_DEBUG
Andreas Färber9349b4f2012-03-14 01:38:32 +01001783struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
aliguorie22a25c2009-03-12 20:12:48 +00001784 target_ulong pc)
1785{
1786 struct kvm_sw_breakpoint *bp;
1787
Blue Swirl72cf2d42009-09-12 07:36:22 +00001788 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001789 if (bp->pc == pc) {
aliguorie22a25c2009-03-12 20:12:48 +00001790 return bp;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001791 }
aliguorie22a25c2009-03-12 20:12:48 +00001792 }
1793 return NULL;
1794}
1795
Andreas Färber9349b4f2012-03-14 01:38:32 +01001796int kvm_sw_breakpoints_active(CPUArchState *env)
aliguorie22a25c2009-03-12 20:12:48 +00001797{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001798 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +00001799}
1800
Glauber Costa452e4752009-07-16 17:55:28 -04001801struct kvm_set_guest_debug_data {
1802 struct kvm_guest_debug dbg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001803 CPUArchState *env;
Glauber Costa452e4752009-07-16 17:55:28 -04001804 int err;
1805};
1806
1807static void kvm_invoke_set_guest_debug(void *data)
1808{
1809 struct kvm_set_guest_debug_data *dbg_data = data;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001810 CPUArchState *env = dbg_data->env;
Jan Kiszkab3807722009-09-17 20:05:58 +02001811
Jan Kiszkab3807722009-09-17 20:05:58 +02001812 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001813}
1814
Andreas Färber9349b4f2012-03-14 01:38:32 +01001815int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
aliguorie22a25c2009-03-12 20:12:48 +00001816{
Glauber Costa452e4752009-07-16 17:55:28 -04001817 struct kvm_set_guest_debug_data data;
aliguorie22a25c2009-03-12 20:12:48 +00001818
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001819 data.dbg.control = reinject_trap;
aliguorie22a25c2009-03-12 20:12:48 +00001820
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001821 if (env->singlestep_enabled) {
1822 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1823 }
Glauber Costa452e4752009-07-16 17:55:28 -04001824 kvm_arch_update_guest_debug(env, &data.dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001825 data.env = env;
aliguorie22a25c2009-03-12 20:12:48 +00001826
Jan Kiszkabe41cbe2010-05-20 00:28:45 +02001827 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
Glauber Costa452e4752009-07-16 17:55:28 -04001828 return data.err;
aliguorie22a25c2009-03-12 20:12:48 +00001829}
1830
Andreas Färber9349b4f2012-03-14 01:38:32 +01001831int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001832 target_ulong len, int type)
1833{
1834 struct kvm_sw_breakpoint *bp;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001835 CPUArchState *env;
aliguorie22a25c2009-03-12 20:12:48 +00001836 int err;
1837
1838 if (type == GDB_BREAKPOINT_SW) {
1839 bp = kvm_find_sw_breakpoint(current_env, addr);
1840 if (bp) {
1841 bp->use_count++;
1842 return 0;
1843 }
1844
Anthony Liguori7267c092011-08-20 22:09:37 -05001845 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
Jan Kiszkaa426e122011-01-04 09:32:13 +01001846 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001847 return -ENOMEM;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001848 }
aliguorie22a25c2009-03-12 20:12:48 +00001849
1850 bp->pc = addr;
1851 bp->use_count = 1;
1852 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1853 if (err) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001854 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001855 return err;
1856 }
1857
Blue Swirl72cf2d42009-09-12 07:36:22 +00001858 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
aliguorie22a25c2009-03-12 20:12:48 +00001859 bp, entry);
1860 } else {
1861 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001862 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001863 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001864 }
aliguorie22a25c2009-03-12 20:12:48 +00001865 }
1866
1867 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1868 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001869 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001870 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001871 }
aliguorie22a25c2009-03-12 20:12:48 +00001872 }
1873 return 0;
1874}
1875
Andreas Färber9349b4f2012-03-14 01:38:32 +01001876int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001877 target_ulong len, int type)
1878{
1879 struct kvm_sw_breakpoint *bp;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001880 CPUArchState *env;
aliguorie22a25c2009-03-12 20:12:48 +00001881 int err;
1882
1883 if (type == GDB_BREAKPOINT_SW) {
1884 bp = kvm_find_sw_breakpoint(current_env, addr);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001885 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001886 return -ENOENT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001887 }
aliguorie22a25c2009-03-12 20:12:48 +00001888
1889 if (bp->use_count > 1) {
1890 bp->use_count--;
1891 return 0;
1892 }
1893
1894 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001895 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001896 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001897 }
aliguorie22a25c2009-03-12 20:12:48 +00001898
Blue Swirl72cf2d42009-09-12 07:36:22 +00001899 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001900 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001901 } else {
1902 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001903 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001904 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001905 }
aliguorie22a25c2009-03-12 20:12:48 +00001906 }
1907
1908 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1909 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001910 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001911 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001912 }
aliguorie22a25c2009-03-12 20:12:48 +00001913 }
1914 return 0;
1915}
1916
Andreas Färber9349b4f2012-03-14 01:38:32 +01001917void kvm_remove_all_breakpoints(CPUArchState *current_env)
aliguorie22a25c2009-03-12 20:12:48 +00001918{
1919 struct kvm_sw_breakpoint *bp, *next;
1920 KVMState *s = current_env->kvm_state;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001921 CPUArchState *env;
aliguorie22a25c2009-03-12 20:12:48 +00001922
Blue Swirl72cf2d42009-09-12 07:36:22 +00001923 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
aliguorie22a25c2009-03-12 20:12:48 +00001924 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1925 /* Try harder to find a CPU that currently sees the breakpoint. */
1926 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001927 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
aliguorie22a25c2009-03-12 20:12:48 +00001928 break;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001929 }
aliguorie22a25c2009-03-12 20:12:48 +00001930 }
1931 }
1932 }
1933 kvm_arch_remove_all_hw_breakpoints();
1934
Jan Kiszkaa426e122011-01-04 09:32:13 +01001935 for (env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorie22a25c2009-03-12 20:12:48 +00001936 kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001937 }
aliguorie22a25c2009-03-12 20:12:48 +00001938}
1939
1940#else /* !KVM_CAP_SET_GUEST_DEBUG */
1941
Andreas Färber9349b4f2012-03-14 01:38:32 +01001942int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
aliguorie22a25c2009-03-12 20:12:48 +00001943{
1944 return -EINVAL;
1945}
1946
Andreas Färber9349b4f2012-03-14 01:38:32 +01001947int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001948 target_ulong len, int type)
1949{
1950 return -EINVAL;
1951}
1952
Andreas Färber9349b4f2012-03-14 01:38:32 +01001953int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001954 target_ulong len, int type)
1955{
1956 return -EINVAL;
1957}
1958
Andreas Färber9349b4f2012-03-14 01:38:32 +01001959void kvm_remove_all_breakpoints(CPUArchState *current_env)
aliguorie22a25c2009-03-12 20:12:48 +00001960{
1961}
1962#endif /* !KVM_CAP_SET_GUEST_DEBUG */
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001963
Andreas Färber9349b4f2012-03-14 01:38:32 +01001964int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001965{
1966 struct kvm_signal_mask *sigmask;
1967 int r;
1968
Jan Kiszkaa426e122011-01-04 09:32:13 +01001969 if (!sigset) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001970 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001971 }
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001972
Anthony Liguori7267c092011-08-20 22:09:37 -05001973 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001974
1975 sigmask->len = 8;
1976 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1977 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
Anthony Liguori7267c092011-08-20 22:09:37 -05001978 g_free(sigmask);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02001979
1980 return r;
1981}
Michael S. Tsirkinca821802010-03-17 13:07:54 +02001982
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +02001983int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val, bool assign,
1984 uint32_t size)
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001985{
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001986 int ret;
1987 struct kvm_ioeventfd iofd;
1988
1989 iofd.datamatch = val;
1990 iofd.addr = addr;
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +02001991 iofd.len = size;
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06001992 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1993 iofd.fd = fd;
1994
1995 if (!kvm_enabled()) {
1996 return -ENOSYS;
1997 }
1998
1999 if (!assign) {
2000 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2001 }
2002
2003 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
2004
2005 if (ret < 0) {
2006 return -errno;
2007 }
2008
2009 return 0;
Cam Macdonell44f1a3d2010-07-26 18:10:59 -06002010}
2011
Michael S. Tsirkinca821802010-03-17 13:07:54 +02002012int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
2013{
2014 struct kvm_ioeventfd kick = {
2015 .datamatch = val,
2016 .addr = addr,
2017 .len = 2,
2018 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
2019 .fd = fd,
2020 };
2021 int r;
Jan Kiszkaa426e122011-01-04 09:32:13 +01002022 if (!kvm_enabled()) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02002023 return -ENOSYS;
Jan Kiszkaa426e122011-01-04 09:32:13 +01002024 }
2025 if (!assign) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02002026 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
Jan Kiszkaa426e122011-01-04 09:32:13 +01002027 }
Michael S. Tsirkinca821802010-03-17 13:07:54 +02002028 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
Jan Kiszkaa426e122011-01-04 09:32:13 +01002029 if (r < 0) {
Michael S. Tsirkinca821802010-03-17 13:07:54 +02002030 return r;
Jan Kiszkaa426e122011-01-04 09:32:13 +01002031 }
Michael S. Tsirkinca821802010-03-17 13:07:54 +02002032 return 0;
Paolo Bonzini98c85732010-04-19 18:59:30 +00002033}
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01002034
Andreas Färber9349b4f2012-03-14 01:38:32 +01002035int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr)
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01002036{
2037 return kvm_arch_on_sigbus_vcpu(env, code, addr);
2038}
2039
2040int kvm_on_sigbus(int code, void *addr)
2041{
2042 return kvm_arch_on_sigbus(code, addr);
2043}