blob: 2d927217bd64aa756736d833fec08fa471aac314 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/atomic.h"
25#include "qemu/option.h"
26#include "qemu/config-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Jan Kiszkad33a1812009-05-02 00:29:37 +020028#include "hw/hw.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/msi.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010030#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/bswap.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/memory.h"
34#include "exec/address-spaces.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/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{
Avi Kivitya8170e52012-10-23 12:30:10 +020063 hwaddr start_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -050064 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 */
Jan Kiszkae333cd62012-08-24 13:34:47 +020095 unsigned irq_set_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,
Avi Kivitya8170e52012-10-23 12:30:10 +0200134 hwaddr start_addr,
135 hwaddr 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,
Avi Kivitya8170e52012-10-23 12:30:10 +0200155 hwaddr start_addr,
156 hwaddr 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,
Avi Kivitya8170e52012-10-23 12:30:10 +0200179 hwaddr *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ärber20d695a2012-10-31 06:57:49 +0100212 CPUState *cpu = opaque;
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200213
Andreas Färber20d695a2012-10-31 06:57:49 +0100214 kvm_arch_reset_vcpu(cpu);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200215}
aliguori5832d1f2008-11-24 19:36:26 +0000216
Andreas Färber504134d2012-12-17 06:38:45 +0100217int kvm_init_vcpu(CPUState *cpu)
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
Eduardo Habkostb164e482013-01-22 18:25:01 -0200225 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)kvm_arch_vcpu_id(cpu));
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
Andreas Färber8737c512012-10-31 05:29:00 +0100231 cpu->kvm_fd = ret;
Andreas Färbera60f24b2012-12-01 05:35:08 +0100232 cpu->kvm_state = s;
Andreas Färber20d695a2012-10-31 06:57:49 +0100233 cpu->kvm_vcpu_dirty = true;
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
Andreas Färberf7575c962012-12-01 06:18:14 +0100242 cpu->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
Andreas Färber8737c512012-10-31 05:29:00 +0100243 cpu->kvm_fd, 0);
Andreas Färberf7575c962012-12-01 06:18:14 +0100244 if (cpu->kvm_run == MAP_FAILED) {
aliguori05330442008-11-05 16:29:27 +0000245 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 =
Andreas Färberf7575c962012-12-01 06:18:14 +0100252 (void *)cpu->kvm_run + s->coalesced_mmio * PAGE_SIZE;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100253 }
Sheng Yang62a27442010-01-26 19:21:16 +0800254
Andreas Färber20d695a2012-10-31 06:57:49 +0100255 ret = kvm_arch_init_vcpu(cpu);
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +0200256 if (ret == 0) {
Andreas Färber20d695a2012-10-31 06:57:49 +0100257 qemu_register_reset(kvm_reset_vcpu, cpu);
258 kvm_arch_reset_vcpu(cpu);
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
Avi Kivitya8170e52012-10-23 12:30:10 +0200296static int kvm_dirty_pages_log_change(hwaddr phys_addr,
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300297 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,
Avi Kivitya8170e52012-10-23 12:30:10 +0200305 (hwaddr)(phys_addr + size - 1));
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300306 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;
Avi Kivitya8170e52012-10-23 12:30:10 +0200366 hwaddr addr, addr1;
Alexey Kardashevskiy752ced02012-11-19 15:40:47 +0000367 unsigned int len = ((section->size / getpagesize()) + 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 Kivitya8170e52012-10-23 12:30:10 +0200409 hwaddr start_addr = section->offset_within_address_space;
410 hwaddr 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
Avi Kivity95d29942012-10-02 18:21:54 +0200457static void kvm_coalesce_mmio_region(MemoryListener *listener,
458 MemoryRegionSection *secion,
Avi Kivitya8170e52012-10-23 12:30:10 +0200459 hwaddr start, hwaddr size)
aliguorif65ed4c2008-12-09 20:09:57 +0000460{
aliguorif65ed4c2008-12-09 20:09:57 +0000461 KVMState *s = kvm_state;
462
463 if (s->coalesced_mmio) {
464 struct kvm_coalesced_mmio_zone zone;
465
466 zone.addr = start;
467 zone.size = size;
Michael S. Tsirkin7e680752012-02-29 17:54:29 +0200468 zone.pad = 0;
aliguorif65ed4c2008-12-09 20:09:57 +0000469
Avi Kivity95d29942012-10-02 18:21:54 +0200470 (void)kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
aliguorif65ed4c2008-12-09 20:09:57 +0000471 }
aliguorif65ed4c2008-12-09 20:09:57 +0000472}
473
Avi Kivity95d29942012-10-02 18:21:54 +0200474static void kvm_uncoalesce_mmio_region(MemoryListener *listener,
475 MemoryRegionSection *secion,
Avi Kivitya8170e52012-10-23 12:30:10 +0200476 hwaddr start, hwaddr size)
aliguorif65ed4c2008-12-09 20:09:57 +0000477{
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
Avi Kivity95d29942012-10-02 18:21:54 +0200487 (void)kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
aliguorif65ed4c2008-12-09 20:09:57 +0000488 }
aliguorif65ed4c2008-12-09 20:09:57 +0000489}
490
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500491int kvm_check_extension(KVMState *s, unsigned int extension)
492{
493 int ret;
494
495 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
496 if (ret < 0) {
497 ret = 0;
498 }
499
500 return ret;
501}
502
Michael S. Tsirkin44c3f8f2013-04-02 00:54:45 +0300503static int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300504 bool assign, uint32_t size, bool datamatch)
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300505{
506 int ret;
507 struct kvm_ioeventfd iofd;
508
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300509 iofd.datamatch = datamatch ? val : 0;
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300510 iofd.addr = addr;
511 iofd.len = size;
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300512 iofd.flags = 0;
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300513 iofd.fd = fd;
514
515 if (!kvm_enabled()) {
516 return -ENOSYS;
517 }
518
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300519 if (datamatch) {
520 iofd.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH;
521 }
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300522 if (!assign) {
523 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
524 }
525
526 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
527
528 if (ret < 0) {
529 return -errno;
530 }
531
532 return 0;
533}
534
Michael S. Tsirkin44c3f8f2013-04-02 00:54:45 +0300535static int kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint16_t val,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300536 bool assign, uint32_t size, bool datamatch)
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300537{
538 struct kvm_ioeventfd kick = {
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300539 .datamatch = datamatch ? val : 0,
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300540 .addr = addr,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300541 .flags = KVM_IOEVENTFD_FLAG_PIO,
Michael S. Tsirkin44c3f8f2013-04-02 00:54:45 +0300542 .len = size,
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300543 .fd = fd,
544 };
545 int r;
546 if (!kvm_enabled()) {
547 return -ENOSYS;
548 }
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300549 if (datamatch) {
550 kick.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH;
551 }
Michael S. Tsirkin500ffd42013-04-02 00:05:21 +0300552 if (!assign) {
553 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
554 }
555 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
556 if (r < 0) {
557 return r;
558 }
559 return 0;
560}
561
562
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200563static int kvm_check_many_ioeventfds(void)
564{
Stefan Hajnoczid0dcac82011-01-25 16:17:14 +0000565 /* Userspace can use ioeventfd for io notification. This requires a host
566 * that supports eventfd(2) and an I/O thread; since eventfd does not
567 * support SIGIO it cannot interrupt the vcpu.
568 *
569 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200570 * can avoid creating too many ioeventfds.
571 */
Anthony Liguori12d45362011-08-22 08:24:58 -0500572#if defined(CONFIG_EVENTFD)
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200573 int ioeventfds[7];
574 int i, ret = 0;
575 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
576 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
577 if (ioeventfds[i] < 0) {
578 break;
579 }
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300580 ret = kvm_set_ioeventfd_pio(ioeventfds[i], 0, i, true, 2, true);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200581 if (ret < 0) {
582 close(ioeventfds[i]);
583 break;
584 }
585 }
586
587 /* Decide whether many devices are supported or not */
588 ret = i == ARRAY_SIZE(ioeventfds);
589
590 while (i-- > 0) {
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300591 kvm_set_ioeventfd_pio(ioeventfds[i], 0, i, false, 2, true);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200592 close(ioeventfds[i]);
593 }
594 return ret;
595#else
596 return 0;
597#endif
598}
599
Jan Kiszka94a8d392011-01-21 21:48:17 +0100600static const KVMCapabilityInfo *
601kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
602{
603 while (list->name) {
604 if (!kvm_check_extension(s, list->value)) {
605 return list;
606 }
607 list++;
608 }
609 return NULL;
610}
611
Avi Kivitya01672d2011-12-18 14:06:05 +0200612static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200613{
614 KVMState *s = kvm_state;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200615 KVMSlot *mem, old;
616 int err;
Avi Kivitya01672d2011-12-18 14:06:05 +0200617 MemoryRegion *mr = section->mr;
618 bool log_dirty = memory_region_is_logging(mr);
Avi Kivitya8170e52012-10-23 12:30:10 +0200619 hwaddr start_addr = section->offset_within_address_space;
Avi Kivitya01672d2011-12-18 14:06:05 +0200620 ram_addr_t size = section->size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200621 void *ram = NULL;
Avi Kivity8f6f9622012-02-29 13:22:12 +0200622 unsigned delta;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200623
Gleb Natapov14542fe2010-07-28 18:13:23 +0300624 /* kvm works in page size chunks, but the function may be called
625 with sub-page size and unaligned start address. */
Avi Kivity8f6f9622012-02-29 13:22:12 +0200626 delta = TARGET_PAGE_ALIGN(size) - size;
627 if (delta > size) {
628 return;
629 }
630 start_addr += delta;
631 size -= delta;
632 size &= TARGET_PAGE_MASK;
633 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
634 return;
635 }
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200636
Avi Kivitya01672d2011-12-18 14:06:05 +0200637 if (!memory_region_is_ram(mr)) {
638 return;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200639 }
640
Avi Kivity8f6f9622012-02-29 13:22:12 +0200641 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
Avi Kivitya01672d2011-12-18 14:06:05 +0200642
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200643 while (1) {
644 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
645 if (!mem) {
646 break;
647 }
648
Avi Kivitya01672d2011-12-18 14:06:05 +0200649 if (add && start_addr >= mem->start_addr &&
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200650 (start_addr + size <= mem->start_addr + mem->memory_size) &&
Avi Kivity9f213ed2011-12-15 19:55:26 +0200651 (ram - start_addr == mem->ram - mem->start_addr)) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200652 /* The new slot fits into the existing one and comes with
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300653 * identical parameters - update flags and done. */
654 kvm_slot_dirty_pages_log_change(mem, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200655 return;
656 }
657
658 old = *mem;
659
Avi Kivity3fbffb62012-01-15 16:13:59 +0200660 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
661 kvm_physical_sync_dirty_bitmap(section);
662 }
663
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200664 /* unregister the overlapping slot */
665 mem->memory_size = 0;
666 err = kvm_set_user_memory_region(s, mem);
667 if (err) {
668 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
669 __func__, strerror(-err));
670 abort();
671 }
672
673 /* Workaround for older KVM versions: we can't join slots, even not by
674 * unregistering the previous ones and then registering the larger
675 * slot. We have to maintain the existing fragmentation. Sigh.
676 *
677 * This workaround assumes that the new slot starts at the same
678 * address as the first existing one. If not or if some overlapping
679 * slot comes around later, we will fail (not seen in practice so far)
680 * - and actually require a recent KVM version. */
681 if (s->broken_set_mem_region &&
Avi Kivitya01672d2011-12-18 14:06:05 +0200682 old.start_addr == start_addr && old.memory_size < size && add) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200683 mem = kvm_alloc_slot(s);
684 mem->memory_size = old.memory_size;
685 mem->start_addr = old.start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200686 mem->ram = old.ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300687 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200688
689 err = kvm_set_user_memory_region(s, mem);
690 if (err) {
691 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
692 strerror(-err));
693 abort();
694 }
695
696 start_addr += old.memory_size;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200697 ram += old.memory_size;
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200698 size -= old.memory_size;
699 continue;
700 }
701
702 /* register prefix slot */
703 if (old.start_addr < start_addr) {
704 mem = kvm_alloc_slot(s);
705 mem->memory_size = start_addr - old.start_addr;
706 mem->start_addr = old.start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200707 mem->ram = old.ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300708 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200709
710 err = kvm_set_user_memory_region(s, mem);
711 if (err) {
712 fprintf(stderr, "%s: error registering prefix slot: %s\n",
713 __func__, strerror(-err));
Alexander Grafd4d68682011-04-16 10:15:11 +0200714#ifdef TARGET_PPC
715 fprintf(stderr, "%s: This is probably because your kernel's " \
716 "PAGE_SIZE is too big. Please try to use 4k " \
717 "PAGE_SIZE!\n", __func__);
718#endif
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200719 abort();
720 }
721 }
722
723 /* register suffix slot */
724 if (old.start_addr + old.memory_size > start_addr + size) {
725 ram_addr_t size_delta;
726
727 mem = kvm_alloc_slot(s);
728 mem->start_addr = start_addr + size;
729 size_delta = mem->start_addr - old.start_addr;
730 mem->memory_size = old.memory_size - size_delta;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200731 mem->ram = old.ram + size_delta;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300732 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200733
734 err = kvm_set_user_memory_region(s, mem);
735 if (err) {
736 fprintf(stderr, "%s: error registering suffix slot: %s\n",
737 __func__, strerror(-err));
738 abort();
739 }
740 }
741 }
742
743 /* in case the KVM bug workaround already "consumed" the new slot */
Jan Kiszkaa426e122011-01-04 09:32:13 +0100744 if (!size) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200745 return;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100746 }
Avi Kivitya01672d2011-12-18 14:06:05 +0200747 if (!add) {
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200748 return;
Jan Kiszkaa426e122011-01-04 09:32:13 +0100749 }
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200750 mem = kvm_alloc_slot(s);
751 mem->memory_size = size;
752 mem->start_addr = start_addr;
Avi Kivity9f213ed2011-12-15 19:55:26 +0200753 mem->ram = ram;
Michael S. Tsirkin25254bb2011-04-06 22:09:54 +0300754 mem->flags = kvm_mem_flags(s, log_dirty);
Michael S. Tsirkin46dbef62010-01-27 22:07:08 +0200755
756 err = kvm_set_user_memory_region(s, mem);
757 if (err) {
758 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
759 strerror(-err));
760 abort();
761 }
762}
763
Avi Kivitya01672d2011-12-18 14:06:05 +0200764static void kvm_region_add(MemoryListener *listener,
765 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200766{
Avi Kivitya01672d2011-12-18 14:06:05 +0200767 kvm_set_phys_mem(section, true);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200768}
769
Avi Kivitya01672d2011-12-18 14:06:05 +0200770static void kvm_region_del(MemoryListener *listener,
771 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200772{
Avi Kivitya01672d2011-12-18 14:06:05 +0200773 kvm_set_phys_mem(section, false);
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200774}
775
Avi Kivitya01672d2011-12-18 14:06:05 +0200776static void kvm_log_sync(MemoryListener *listener,
777 MemoryRegionSection *section)
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200778{
Avi Kivitya01672d2011-12-18 14:06:05 +0200779 int r;
780
Avi Kivityffcde122011-12-19 13:18:13 +0200781 r = kvm_physical_sync_dirty_bitmap(section);
Avi Kivitya01672d2011-12-18 14:06:05 +0200782 if (r < 0) {
783 abort();
784 }
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200785}
786
Avi Kivitya01672d2011-12-18 14:06:05 +0200787static void kvm_log_global_start(struct MemoryListener *listener)
788{
789 int r;
790
791 r = kvm_set_migration_log(1);
792 assert(r >= 0);
793}
794
795static void kvm_log_global_stop(struct MemoryListener *listener)
796{
797 int r;
798
799 r = kvm_set_migration_log(0);
800 assert(r >= 0);
801}
802
Avi Kivityd22b0962012-09-30 22:21:11 +0200803static void kvm_mem_ioeventfd_add(MemoryListener *listener,
804 MemoryRegionSection *section,
805 bool match_data, uint64_t data,
806 EventNotifier *e)
807{
808 int fd = event_notifier_get_fd(e);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200809 int r;
810
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200811 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300812 data, true, section->size, match_data);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200813 if (r < 0) {
814 abort();
815 }
816}
817
Avi Kivityd22b0962012-09-30 22:21:11 +0200818static void kvm_mem_ioeventfd_del(MemoryListener *listener,
819 MemoryRegionSection *section,
820 bool match_data, uint64_t data,
821 EventNotifier *e)
Avi Kivity80a1ea32012-02-08 16:39:06 +0200822{
Avi Kivityd22b0962012-09-30 22:21:11 +0200823 int fd = event_notifier_get_fd(e);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200824 int r;
825
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200826 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300827 data, false, section->size, match_data);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200828 if (r < 0) {
829 abort();
830 }
831}
832
Avi Kivityd22b0962012-09-30 22:21:11 +0200833static void kvm_io_ioeventfd_add(MemoryListener *listener,
834 MemoryRegionSection *section,
835 bool match_data, uint64_t data,
836 EventNotifier *e)
Avi Kivity80a1ea32012-02-08 16:39:06 +0200837{
Avi Kivityd22b0962012-09-30 22:21:11 +0200838 int fd = event_notifier_get_fd(e);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200839 int r;
840
Michael S. Tsirkin44c3f8f2013-04-02 00:54:45 +0300841 r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300842 data, true, section->size, match_data);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200843 if (r < 0) {
844 abort();
845 }
846}
847
Avi Kivityd22b0962012-09-30 22:21:11 +0200848static void kvm_io_ioeventfd_del(MemoryListener *listener,
849 MemoryRegionSection *section,
850 bool match_data, uint64_t data,
851 EventNotifier *e)
Avi Kivity80a1ea32012-02-08 16:39:06 +0200852
853{
Avi Kivityd22b0962012-09-30 22:21:11 +0200854 int fd = event_notifier_get_fd(e);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200855 int r;
856
Michael S. Tsirkin44c3f8f2013-04-02 00:54:45 +0300857 r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space,
Michael S. Tsirkin41cb62c2013-04-02 16:52:25 +0300858 data, false, section->size, match_data);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200859 if (r < 0) {
860 abort();
861 }
862}
863
Avi Kivitya01672d2011-12-18 14:06:05 +0200864static MemoryListener kvm_memory_listener = {
865 .region_add = kvm_region_add,
866 .region_del = kvm_region_del,
Anthony PERARDe5896b12011-02-07 12:19:23 +0100867 .log_start = kvm_log_start,
868 .log_stop = kvm_log_stop,
Avi Kivitya01672d2011-12-18 14:06:05 +0200869 .log_sync = kvm_log_sync,
870 .log_global_start = kvm_log_global_start,
871 .log_global_stop = kvm_log_global_stop,
Avi Kivityd22b0962012-09-30 22:21:11 +0200872 .eventfd_add = kvm_mem_ioeventfd_add,
873 .eventfd_del = kvm_mem_ioeventfd_del,
Avi Kivity95d29942012-10-02 18:21:54 +0200874 .coalesced_mmio_add = kvm_coalesce_mmio_region,
875 .coalesced_mmio_del = kvm_uncoalesce_mmio_region,
Avi Kivityd22b0962012-09-30 22:21:11 +0200876 .priority = 10,
877};
878
879static MemoryListener kvm_io_listener = {
Avi Kivityd22b0962012-09-30 22:21:11 +0200880 .eventfd_add = kvm_io_ioeventfd_add,
881 .eventfd_del = kvm_io_ioeventfd_del,
Avi Kivity72e22d22012-02-08 15:05:50 +0200882 .priority = 10,
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +0200883};
884
Andreas Färberc3affe52013-01-18 15:03:43 +0100885static void kvm_handle_interrupt(CPUState *cpu, int mask)
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +0200886{
Andreas Färber259186a2013-01-17 18:51:17 +0100887 cpu->interrupt_request |= mask;
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +0200888
Andreas Färber60e82572012-05-02 22:23:49 +0200889 if (!qemu_cpu_is_self(cpu)) {
Andreas Färberc08d7422012-05-03 04:34:15 +0200890 qemu_cpu_kick(cpu);
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +0200891 }
892}
893
Peter Maydell3889c3f2012-07-26 15:35:12 +0100894int kvm_set_irq(KVMState *s, int irq, int level)
Jan Kiszka84b058d2011-10-15 11:49:47 +0200895{
896 struct kvm_irq_level event;
897 int ret;
898
Peter Maydell7ae26bd2012-07-26 15:35:11 +0100899 assert(kvm_async_interrupts_enabled());
Jan Kiszka84b058d2011-10-15 11:49:47 +0200900
901 event.level = level;
902 event.irq = irq;
Jan Kiszkae333cd62012-08-24 13:34:47 +0200903 ret = kvm_vm_ioctl(s, s->irq_set_ioctl, &event);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200904 if (ret < 0) {
Peter Maydell3889c3f2012-07-26 15:35:12 +0100905 perror("kvm_set_irq");
Jan Kiszka84b058d2011-10-15 11:49:47 +0200906 abort();
907 }
908
Jan Kiszkae333cd62012-08-24 13:34:47 +0200909 return (s->irq_set_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
Jan Kiszka84b058d2011-10-15 11:49:47 +0200910}
911
912#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszkad3d3bef2012-06-05 21:03:57 +0200913typedef struct KVMMSIRoute {
914 struct kvm_irq_routing_entry kroute;
915 QTAILQ_ENTRY(KVMMSIRoute) entry;
916} KVMMSIRoute;
917
Jan Kiszka84b058d2011-10-15 11:49:47 +0200918static void set_gsi(KVMState *s, unsigned int gsi)
919{
Jan Kiszka84b058d2011-10-15 11:49:47 +0200920 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
921}
922
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300923static void clear_gsi(KVMState *s, unsigned int gsi)
924{
925 s->used_gsi_bitmap[gsi / 32] &= ~(1U << (gsi % 32));
926}
927
Jan Kiszka84b058d2011-10-15 11:49:47 +0200928static void kvm_init_irq_routing(KVMState *s)
929{
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300930 int gsi_count, i;
Jan Kiszka84b058d2011-10-15 11:49:47 +0200931
932 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
933 if (gsi_count > 0) {
934 unsigned int gsi_bits, i;
935
936 /* Round up so we can search ints using ffs */
Jason Baronbc8c6782012-03-28 14:18:05 -0400937 gsi_bits = ALIGN(gsi_count, 32);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200938 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
Jan Kiszka4e2e4e62012-05-16 15:41:08 -0300939 s->gsi_count = gsi_count;
Jan Kiszka84b058d2011-10-15 11:49:47 +0200940
941 /* Mark any over-allocated bits as already in use */
942 for (i = gsi_count; i < gsi_bits; i++) {
943 set_gsi(s, i);
944 }
945 }
946
947 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
948 s->nr_allocated_irq_routes = 0;
949
Jan Kiszka4a3adeb2012-05-16 15:41:14 -0300950 if (!s->direct_msi) {
951 for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) {
952 QTAILQ_INIT(&s->msi_hashtab[i]);
953 }
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300954 }
955
Jan Kiszka84b058d2011-10-15 11:49:47 +0200956 kvm_arch_init_irq_routing(s);
957}
958
Jan Kiszkae7b20302012-05-17 10:32:35 -0300959static void kvm_irqchip_commit_routes(KVMState *s)
960{
961 int ret;
962
963 s->irq_routes->flags = 0;
964 ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
965 assert(ret == 0);
966}
967
Jan Kiszka84b058d2011-10-15 11:49:47 +0200968static void kvm_add_routing_entry(KVMState *s,
969 struct kvm_irq_routing_entry *entry)
970{
971 struct kvm_irq_routing_entry *new;
972 int n, size;
973
974 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
975 n = s->nr_allocated_irq_routes * 2;
976 if (n < 64) {
977 n = 64;
978 }
979 size = sizeof(struct kvm_irq_routing);
980 size += n * sizeof(*new);
981 s->irq_routes = g_realloc(s->irq_routes, size);
982 s->nr_allocated_irq_routes = n;
983 }
984 n = s->irq_routes->nr++;
985 new = &s->irq_routes->entries[n];
986 memset(new, 0, sizeof(*new));
987 new->gsi = entry->gsi;
988 new->type = entry->type;
989 new->flags = entry->flags;
990 new->u = entry->u;
991
992 set_gsi(s, entry->gsi);
Jan Kiszkae7b20302012-05-17 10:32:35 -0300993
994 kvm_irqchip_commit_routes(s);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200995}
996
Jan Kiszkacc574072012-08-27 08:28:38 +0200997static int kvm_update_routing_entry(KVMState *s,
998 struct kvm_irq_routing_entry *new_entry)
999{
1000 struct kvm_irq_routing_entry *entry;
1001 int n;
1002
1003 for (n = 0; n < s->irq_routes->nr; n++) {
1004 entry = &s->irq_routes->entries[n];
1005 if (entry->gsi != new_entry->gsi) {
1006 continue;
1007 }
1008
1009 entry->type = new_entry->type;
1010 entry->flags = new_entry->flags;
1011 entry->u = new_entry->u;
1012
1013 kvm_irqchip_commit_routes(s);
1014
1015 return 0;
1016 }
1017
1018 return -ESRCH;
1019}
1020
Jan Kiszka1df186d2012-05-17 10:32:32 -03001021void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin)
Jan Kiszka84b058d2011-10-15 11:49:47 +02001022{
1023 struct kvm_irq_routing_entry e;
1024
Jan Kiszka4e2e4e62012-05-16 15:41:08 -03001025 assert(pin < s->gsi_count);
1026
Jan Kiszka84b058d2011-10-15 11:49:47 +02001027 e.gsi = irq;
1028 e.type = KVM_IRQ_ROUTING_IRQCHIP;
1029 e.flags = 0;
1030 e.u.irqchip.irqchip = irqchip;
1031 e.u.irqchip.pin = pin;
1032 kvm_add_routing_entry(s, &e);
1033}
1034
Jan Kiszka1e2aa8b2012-05-17 10:32:34 -03001035void kvm_irqchip_release_virq(KVMState *s, int virq)
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001036{
1037 struct kvm_irq_routing_entry *e;
1038 int i;
1039
1040 for (i = 0; i < s->irq_routes->nr; i++) {
1041 e = &s->irq_routes->entries[i];
1042 if (e->gsi == virq) {
1043 s->irq_routes->nr--;
1044 *e = s->irq_routes->entries[s->irq_routes->nr];
1045 }
1046 }
1047 clear_gsi(s, virq);
1048}
1049
1050static unsigned int kvm_hash_msi(uint32_t data)
1051{
1052 /* This is optimized for IA32 MSI layout. However, no other arch shall
1053 * repeat the mistake of not providing a direct MSI injection API. */
1054 return data & 0xff;
1055}
1056
1057static void kvm_flush_dynamic_msi_routes(KVMState *s)
1058{
1059 KVMMSIRoute *route, *next;
1060 unsigned int hash;
1061
1062 for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) {
1063 QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) {
1064 kvm_irqchip_release_virq(s, route->kroute.gsi);
1065 QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry);
1066 g_free(route);
1067 }
1068 }
1069}
1070
1071static int kvm_irqchip_get_virq(KVMState *s)
1072{
1073 uint32_t *word = s->used_gsi_bitmap;
1074 int max_words = ALIGN(s->gsi_count, 32) / 32;
1075 int i, bit;
1076 bool retry = true;
1077
1078again:
1079 /* Return the lowest unused GSI in the bitmap */
1080 for (i = 0; i < max_words; i++) {
1081 bit = ffs(~word[i]);
1082 if (!bit) {
1083 continue;
1084 }
1085
1086 return bit - 1 + i * 32;
1087 }
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001088 if (!s->direct_msi && retry) {
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001089 retry = false;
1090 kvm_flush_dynamic_msi_routes(s);
1091 goto again;
1092 }
1093 return -ENOSPC;
1094
1095}
1096
1097static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
1098{
1099 unsigned int hash = kvm_hash_msi(msg.data);
1100 KVMMSIRoute *route;
1101
1102 QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) {
1103 if (route->kroute.u.msi.address_lo == (uint32_t)msg.address &&
1104 route->kroute.u.msi.address_hi == (msg.address >> 32) &&
1105 route->kroute.u.msi.data == msg.data) {
1106 return route;
1107 }
1108 }
1109 return NULL;
1110}
1111
1112int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1113{
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001114 struct kvm_msi msi;
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001115 KVMMSIRoute *route;
1116
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001117 if (s->direct_msi) {
1118 msi.address_lo = (uint32_t)msg.address;
1119 msi.address_hi = msg.address >> 32;
1120 msi.data = msg.data;
1121 msi.flags = 0;
1122 memset(msi.pad, 0, sizeof(msi.pad));
1123
1124 return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
1125 }
1126
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001127 route = kvm_lookup_msi_route(s, msg);
1128 if (!route) {
Jan Kiszkae7b20302012-05-17 10:32:35 -03001129 int virq;
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001130
1131 virq = kvm_irqchip_get_virq(s);
1132 if (virq < 0) {
1133 return virq;
1134 }
1135
1136 route = g_malloc(sizeof(KVMMSIRoute));
1137 route->kroute.gsi = virq;
1138 route->kroute.type = KVM_IRQ_ROUTING_MSI;
1139 route->kroute.flags = 0;
1140 route->kroute.u.msi.address_lo = (uint32_t)msg.address;
1141 route->kroute.u.msi.address_hi = msg.address >> 32;
1142 route->kroute.u.msi.data = msg.data;
1143
1144 kvm_add_routing_entry(s, &route->kroute);
1145
1146 QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route,
1147 entry);
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001148 }
1149
1150 assert(route->kroute.type == KVM_IRQ_ROUTING_MSI);
1151
Peter Maydell3889c3f2012-07-26 15:35:12 +01001152 return kvm_set_irq(s, route->kroute.gsi, 1);
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001153}
1154
Jan Kiszka92b4e482012-05-17 10:32:33 -03001155int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1156{
1157 struct kvm_irq_routing_entry kroute;
1158 int virq;
1159
Peter Maydellf3e1bed2012-07-26 15:35:16 +01001160 if (!kvm_gsi_routing_enabled()) {
Jan Kiszka92b4e482012-05-17 10:32:33 -03001161 return -ENOSYS;
1162 }
1163
1164 virq = kvm_irqchip_get_virq(s);
1165 if (virq < 0) {
1166 return virq;
1167 }
1168
1169 kroute.gsi = virq;
1170 kroute.type = KVM_IRQ_ROUTING_MSI;
1171 kroute.flags = 0;
1172 kroute.u.msi.address_lo = (uint32_t)msg.address;
1173 kroute.u.msi.address_hi = msg.address >> 32;
1174 kroute.u.msi.data = msg.data;
1175
1176 kvm_add_routing_entry(s, &kroute);
1177
1178 return virq;
1179}
1180
Jan Kiszkacc574072012-08-27 08:28:38 +02001181int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg)
1182{
1183 struct kvm_irq_routing_entry kroute;
1184
1185 if (!kvm_irqchip_in_kernel()) {
1186 return -ENOSYS;
1187 }
1188
1189 kroute.gsi = virq;
1190 kroute.type = KVM_IRQ_ROUTING_MSI;
1191 kroute.flags = 0;
1192 kroute.u.msi.address_lo = (uint32_t)msg.address;
1193 kroute.u.msi.address_hi = msg.address >> 32;
1194 kroute.u.msi.data = msg.data;
1195
1196 return kvm_update_routing_entry(s, &kroute);
1197}
1198
Jan Kiszka39853bb2012-05-17 10:32:36 -03001199static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1200{
1201 struct kvm_irqfd irqfd = {
1202 .fd = fd,
1203 .gsi = virq,
1204 .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN,
1205 };
1206
Peter Maydellcc7e0dd2012-07-26 15:35:14 +01001207 if (!kvm_irqfds_enabled()) {
Jan Kiszka39853bb2012-05-17 10:32:36 -03001208 return -ENOSYS;
1209 }
1210
1211 return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd);
1212}
1213
Jan Kiszka84b058d2011-10-15 11:49:47 +02001214#else /* !KVM_CAP_IRQ_ROUTING */
1215
1216static void kvm_init_irq_routing(KVMState *s)
1217{
1218}
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001219
Jan Kiszkad3d3bef2012-06-05 21:03:57 +02001220void kvm_irqchip_release_virq(KVMState *s, int virq)
1221{
1222}
1223
Jan Kiszka04fa27f2012-05-16 15:41:10 -03001224int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1225{
1226 abort();
1227}
Jan Kiszka92b4e482012-05-17 10:32:33 -03001228
1229int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1230{
Alex Williamsondf410672012-06-25 09:40:39 -06001231 return -ENOSYS;
Jan Kiszka92b4e482012-05-17 10:32:33 -03001232}
Jan Kiszka39853bb2012-05-17 10:32:36 -03001233
1234static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1235{
1236 abort();
1237}
Michael S. Tsirkindabe3142013-01-15 19:50:13 +02001238
1239int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg)
1240{
1241 return -ENOSYS;
1242}
Jan Kiszka84b058d2011-10-15 11:49:47 +02001243#endif /* !KVM_CAP_IRQ_ROUTING */
1244
Jan Kiszkab131c742012-08-20 10:55:56 +02001245int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, int virq)
Jan Kiszka39853bb2012-05-17 10:32:36 -03001246{
Jan Kiszkab131c742012-08-20 10:55:56 +02001247 return kvm_irqchip_assign_irqfd(s, event_notifier_get_fd(n), virq, true);
Jan Kiszka39853bb2012-05-17 10:32:36 -03001248}
1249
Jan Kiszkab131c742012-08-20 10:55:56 +02001250int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, int virq)
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02001251{
Jan Kiszkab131c742012-08-20 10:55:56 +02001252 return kvm_irqchip_assign_irqfd(s, event_notifier_get_fd(n), virq, false);
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02001253}
1254
Jan Kiszka84b058d2011-10-15 11:49:47 +02001255static int kvm_irqchip_create(KVMState *s)
1256{
1257 QemuOptsList *list = qemu_find_opts("machine");
1258 int ret;
1259
1260 if (QTAILQ_EMPTY(&list->head) ||
1261 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
Jan Kiszkaa24b9102012-05-16 15:41:15 -03001262 "kernel_irqchip", true) ||
Jan Kiszka84b058d2011-10-15 11:49:47 +02001263 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
1264 return 0;
1265 }
1266
1267 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
1268 if (ret < 0) {
1269 fprintf(stderr, "Create kernel irqchip failed\n");
1270 return ret;
1271 }
1272
Jan Kiszka3d4b2642012-01-31 19:17:52 +01001273 kvm_kernel_irqchip = true;
Peter Maydell7ae26bd2012-07-26 15:35:11 +01001274 /* If we have an in-kernel IRQ chip then we must have asynchronous
1275 * interrupt delivery (though the reverse is not necessarily true)
1276 */
1277 kvm_async_interrupts_allowed = true;
Jan Kiszka84b058d2011-10-15 11:49:47 +02001278
1279 kvm_init_irq_routing(s);
1280
1281 return 0;
1282}
1283
Dunrong Huang3ed444e2012-07-31 19:18:17 +08001284static int kvm_max_vcpus(KVMState *s)
1285{
1286 int ret;
1287
1288 /* Find number of supported CPUs using the recommended
1289 * procedure from the kernel API documentation to cope with
1290 * older kernels that may be missing capabilities.
1291 */
1292 ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
1293 if (ret) {
1294 return ret;
1295 }
1296 ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS);
1297 if (ret) {
1298 return ret;
1299 }
1300
1301 return 4;
1302}
1303
Jan Kiszkacad1e282011-01-21 21:48:16 +01001304int kvm_init(void)
aliguori05330442008-11-05 16:29:27 +00001305{
Jan Kiszka168ccc12009-06-07 11:30:25 +02001306 static const char upgrade_note[] =
1307 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1308 "(see http://sourceforge.net/projects/kvm).\n";
aliguori05330442008-11-05 16:29:27 +00001309 KVMState *s;
Jan Kiszka94a8d392011-01-21 21:48:17 +01001310 const KVMCapabilityInfo *missing_cap;
aliguori05330442008-11-05 16:29:27 +00001311 int ret;
1312 int i;
Dunrong Huang3ed444e2012-07-31 19:18:17 +08001313 int max_vcpus;
aliguori05330442008-11-05 16:29:27 +00001314
Anthony Liguori7267c092011-08-20 22:09:37 -05001315 s = g_malloc0(sizeof(KVMState));
aliguori05330442008-11-05 16:29:27 +00001316
David Gibson3145fcb2012-04-04 11:15:54 +10001317 /*
1318 * On systems where the kernel can support different base page
1319 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1320 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1321 * page size for the system though.
1322 */
1323 assert(TARGET_PAGE_SIZE <= getpagesize());
1324
aliguorie22a25c2009-03-12 20:12:48 +00001325#ifdef KVM_CAP_SET_GUEST_DEBUG
Blue Swirl72cf2d42009-09-12 07:36:22 +00001326 QTAILQ_INIT(&s->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +00001327#endif
Jan Kiszkaa426e122011-01-04 09:32:13 +01001328 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
aliguori05330442008-11-05 16:29:27 +00001329 s->slots[i].slot = i;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001330 }
aliguori05330442008-11-05 16:29:27 +00001331 s->vmfd = -1;
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001332 s->fd = qemu_open("/dev/kvm", O_RDWR);
aliguori05330442008-11-05 16:29:27 +00001333 if (s->fd == -1) {
1334 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1335 ret = -errno;
1336 goto err;
1337 }
1338
1339 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1340 if (ret < KVM_API_VERSION) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001341 if (ret > 0) {
aliguori05330442008-11-05 16:29:27 +00001342 ret = -EINVAL;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001343 }
aliguori05330442008-11-05 16:29:27 +00001344 fprintf(stderr, "kvm version too old\n");
1345 goto err;
1346 }
1347
1348 if (ret > KVM_API_VERSION) {
1349 ret = -EINVAL;
1350 fprintf(stderr, "kvm version not supported\n");
1351 goto err;
1352 }
1353
Dunrong Huang3ed444e2012-07-31 19:18:17 +08001354 max_vcpus = kvm_max_vcpus(s);
1355 if (smp_cpus > max_vcpus) {
1356 ret = -EINVAL;
1357 fprintf(stderr, "Number of SMP cpus requested (%d) exceeds max cpus "
1358 "supported by KVM (%d)\n", smp_cpus, max_vcpus);
1359 goto err;
1360 }
1361
aliguori05330442008-11-05 16:29:27 +00001362 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
Alexander Graf0104dca2010-04-01 18:42:37 +02001363 if (s->vmfd < 0) {
1364#ifdef TARGET_S390X
1365 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1366 "your host kernel command line\n");
1367#endif
Xu He Jiedb9eae12011-10-27 10:15:13 +08001368 ret = s->vmfd;
aliguori05330442008-11-05 16:29:27 +00001369 goto err;
Alexander Graf0104dca2010-04-01 18:42:37 +02001370 }
aliguori05330442008-11-05 16:29:27 +00001371
Jan Kiszka94a8d392011-01-21 21:48:17 +01001372 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1373 if (!missing_cap) {
1374 missing_cap =
1375 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1376 }
1377 if (missing_cap) {
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001378 ret = -EINVAL;
Jan Kiszka94a8d392011-01-21 21:48:17 +01001379 fprintf(stderr, "kvm does not support %s\n%s",
1380 missing_cap->name, upgrade_note);
aliguori05330442008-11-05 16:29:27 +00001381 goto err;
1382 }
1383
Anthony Liguoriad7b8b32009-05-08 15:33:24 -05001384 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
aliguorif65ed4c2008-12-09 20:09:57 +00001385
Jan Kiszkae69917e2009-05-01 20:42:15 +02001386 s->broken_set_mem_region = 1;
Lai Jiangshan14a09512010-12-10 15:52:36 +08001387 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
Jan Kiszkae69917e2009-05-01 20:42:15 +02001388 if (ret > 0) {
1389 s->broken_set_mem_region = 0;
1390 }
Jan Kiszkae69917e2009-05-01 20:42:15 +02001391
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001392#ifdef KVM_CAP_VCPU_EVENTS
1393 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1394#endif
1395
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001396 s->robust_singlestep =
1397 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001398
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001399#ifdef KVM_CAP_DEBUGREGS
1400 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1401#endif
1402
Sheng Yangf1665b22010-06-17 17:53:07 +08001403#ifdef KVM_CAP_XSAVE
1404 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1405#endif
1406
Sheng Yangf1665b22010-06-17 17:53:07 +08001407#ifdef KVM_CAP_XCRS
1408 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1409#endif
1410
Jan Kiszka8a7c7392012-03-02 20:28:48 +01001411#ifdef KVM_CAP_PIT_STATE2
1412 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1413#endif
1414
Jan Kiszkad3d3bef2012-06-05 21:03:57 +02001415#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001416 s->direct_msi = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);
Jan Kiszkad3d3bef2012-06-05 21:03:57 +02001417#endif
Jan Kiszka4a3adeb2012-05-16 15:41:14 -03001418
Jan Kiszka3ab73842012-08-27 08:28:39 +02001419 s->intx_set_mask = kvm_check_extension(s, KVM_CAP_PCI_2_3);
1420
Jan Kiszkae333cd62012-08-24 13:34:47 +02001421 s->irq_set_ioctl = KVM_IRQ_LINE;
Peter Maydell8732fbd2012-08-15 12:08:13 +01001422 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
Jan Kiszkae333cd62012-08-24 13:34:47 +02001423 s->irq_set_ioctl = KVM_IRQ_LINE_STATUS;
Peter Maydell8732fbd2012-08-15 12:08:13 +01001424 }
1425
Jan Kiszkacad1e282011-01-21 21:48:16 +01001426 ret = kvm_arch_init(s);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001427 if (ret < 0) {
aliguori05330442008-11-05 16:29:27 +00001428 goto err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001429 }
aliguori05330442008-11-05 16:29:27 +00001430
Jan Kiszka84b058d2011-10-15 11:49:47 +02001431 ret = kvm_irqchip_create(s);
1432 if (ret < 0) {
1433 goto err;
1434 }
1435
aliguori05330442008-11-05 16:29:27 +00001436 kvm_state = s;
Avi Kivityf6790af2012-10-02 20:13:51 +02001437 memory_listener_register(&kvm_memory_listener, &address_space_memory);
1438 memory_listener_register(&kvm_io_listener, &address_space_io);
aliguori05330442008-11-05 16:29:27 +00001439
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001440 s->many_ioeventfds = kvm_check_many_ioeventfds();
1441
Jan Kiszkaaa7f74d2011-04-13 01:32:56 +02001442 cpu_interrupt_handler = kvm_handle_interrupt;
1443
aliguori05330442008-11-05 16:29:27 +00001444 return 0;
1445
1446err:
Stefan Weil6d1cc322012-09-03 22:40:40 +02001447 if (s->vmfd >= 0) {
1448 close(s->vmfd);
1449 }
1450 if (s->fd != -1) {
1451 close(s->fd);
aliguori05330442008-11-05 16:29:27 +00001452 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001453 g_free(s);
aliguori05330442008-11-05 16:29:27 +00001454
1455 return ret;
1456}
1457
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001458static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1459 uint32_t count)
aliguori05330442008-11-05 16:29:27 +00001460{
1461 int i;
1462 uint8_t *ptr = data;
1463
1464 for (i = 0; i < count; i++) {
1465 if (direction == KVM_EXIT_IO_IN) {
1466 switch (size) {
1467 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001468 stb_p(ptr, cpu_inb(port));
aliguori05330442008-11-05 16:29:27 +00001469 break;
1470 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001471 stw_p(ptr, cpu_inw(port));
aliguori05330442008-11-05 16:29:27 +00001472 break;
1473 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001474 stl_p(ptr, cpu_inl(port));
aliguori05330442008-11-05 16:29:27 +00001475 break;
1476 }
1477 } else {
1478 switch (size) {
1479 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001480 cpu_outb(port, ldub_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001481 break;
1482 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001483 cpu_outw(port, lduw_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001484 break;
1485 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001486 cpu_outl(port, ldl_p(ptr));
aliguori05330442008-11-05 16:29:27 +00001487 break;
1488 }
1489 }
1490
1491 ptr += size;
1492 }
aliguori05330442008-11-05 16:29:27 +00001493}
1494
Andreas Färber9349b4f2012-03-14 01:38:32 +01001495static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001496{
Andreas Färber20d695a2012-10-31 06:57:49 +01001497 CPUState *cpu = ENV_GET_CPU(env);
1498
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001499 fprintf(stderr, "KVM internal error.");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001500 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1501 int i;
1502
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001503 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001504 for (i = 0; i < run->internal.ndata; ++i) {
1505 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1506 i, (uint64_t)run->internal.data[i]);
1507 }
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001508 } else {
1509 fprintf(stderr, "\n");
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001510 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001511 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1512 fprintf(stderr, "emulation failure\n");
Andreas Färber20d695a2012-10-31 06:57:49 +01001513 if (!kvm_arch_stop_on_emulation_error(cpu)) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001514 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001515 return EXCP_INTERRUPT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001516 }
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001517 }
1518 /* FIXME: Should trigger a qmp message to let management know
1519 * something went wrong.
1520 */
Jan Kiszka73aaec42011-01-21 21:48:06 +01001521 return -1;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001522}
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001523
Sheng Yang62a27442010-01-26 19:21:16 +08001524void kvm_flush_coalesced_mmio_buffer(void)
aliguorif65ed4c2008-12-09 20:09:57 +00001525{
aliguorif65ed4c2008-12-09 20:09:57 +00001526 KVMState *s = kvm_state;
Avi Kivity1cae88b2011-10-18 19:43:12 +02001527
1528 if (s->coalesced_flush_in_progress) {
1529 return;
1530 }
1531
1532 s->coalesced_flush_in_progress = true;
1533
Sheng Yang62a27442010-01-26 19:21:16 +08001534 if (s->coalesced_mmio_ring) {
1535 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
aliguorif65ed4c2008-12-09 20:09:57 +00001536 while (ring->first != ring->last) {
1537 struct kvm_coalesced_mmio *ent;
1538
1539 ent = &ring->coalesced_mmio[ring->first];
1540
1541 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
Marcelo Tosatti85199472010-02-22 13:57:54 -03001542 smp_wmb();
aliguorif65ed4c2008-12-09 20:09:57 +00001543 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1544 }
1545 }
Avi Kivity1cae88b2011-10-18 19:43:12 +02001546
1547 s->coalesced_flush_in_progress = false;
aliguorif65ed4c2008-12-09 20:09:57 +00001548}
1549
Andreas Färber20d695a2012-10-31 06:57:49 +01001550static void do_kvm_cpu_synchronize_state(void *arg)
Avi Kivity4c0960c2009-08-17 23:19:53 +03001551{
Andreas Färber20d695a2012-10-31 06:57:49 +01001552 CPUState *cpu = arg;
Jan Kiszka2705d562010-05-04 09:45:23 -03001553
Andreas Färber20d695a2012-10-31 06:57:49 +01001554 if (!cpu->kvm_vcpu_dirty) {
1555 kvm_arch_get_registers(cpu);
1556 cpu->kvm_vcpu_dirty = true;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001557 }
1558}
1559
Andreas Färber9349b4f2012-03-14 01:38:32 +01001560void kvm_cpu_synchronize_state(CPUArchState *env)
Jan Kiszka2705d562010-05-04 09:45:23 -03001561{
Andreas Färberf100f0b2012-05-03 14:58:47 +02001562 CPUState *cpu = ENV_GET_CPU(env);
1563
Andreas Färber20d695a2012-10-31 06:57:49 +01001564 if (!cpu->kvm_vcpu_dirty) {
1565 run_on_cpu(cpu, do_kvm_cpu_synchronize_state, cpu);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001566 }
Jan Kiszka2705d562010-05-04 09:45:23 -03001567}
1568
Igor Mammedov3f24a582013-04-11 16:51:41 +02001569void kvm_cpu_synchronize_post_reset(CPUState *cpu)
Jan Kiszkaea375f92010-03-01 19:10:30 +01001570{
Andreas Färber20d695a2012-10-31 06:57:49 +01001571 kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE);
1572 cpu->kvm_vcpu_dirty = false;
Jan Kiszkaea375f92010-03-01 19:10:30 +01001573}
1574
Igor Mammedov3f24a582013-04-11 16:51:41 +02001575void kvm_cpu_synchronize_post_init(CPUState *cpu)
Jan Kiszkaea375f92010-03-01 19:10:30 +01001576{
Andreas Färber20d695a2012-10-31 06:57:49 +01001577 kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE);
1578 cpu->kvm_vcpu_dirty = false;
Jan Kiszkaea375f92010-03-01 19:10:30 +01001579}
1580
Andreas Färber9349b4f2012-03-14 01:38:32 +01001581int kvm_cpu_exec(CPUArchState *env)
aliguori05330442008-11-05 16:29:27 +00001582{
Andreas Färber20d695a2012-10-31 06:57:49 +01001583 CPUState *cpu = ENV_GET_CPU(env);
Andreas Färberf7575c962012-12-01 06:18:14 +01001584 struct kvm_run *run = cpu->kvm_run;
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001585 int ret, run_ret;
aliguori05330442008-11-05 16:29:27 +00001586
Blue Swirl8c0d5772010-04-18 14:22:14 +00001587 DPRINTF("kvm_cpu_exec()\n");
aliguori05330442008-11-05 16:29:27 +00001588
Andreas Färber20d695a2012-10-31 06:57:49 +01001589 if (kvm_arch_process_async_events(cpu)) {
Andreas Färberfcd7d002012-12-17 08:02:44 +01001590 cpu->exit_request = 0;
Jan Kiszka6792a572011-02-07 12:19:18 +01001591 return EXCP_HLT;
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001592 }
1593
aliguori05330442008-11-05 16:29:27 +00001594 do {
Andreas Färber20d695a2012-10-31 06:57:49 +01001595 if (cpu->kvm_vcpu_dirty) {
1596 kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE);
1597 cpu->kvm_vcpu_dirty = false;
Avi Kivity4c0960c2009-08-17 23:19:53 +03001598 }
1599
Andreas Färber20d695a2012-10-31 06:57:49 +01001600 kvm_arch_pre_run(cpu, run);
Andreas Färberfcd7d002012-12-17 08:02:44 +01001601 if (cpu->exit_request) {
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001602 DPRINTF("interrupt exit requested\n");
1603 /*
1604 * KVM requires us to reenter the kernel after IO exits to complete
1605 * instruction emulation. This self-signal will ensure that we
1606 * leave ASAP again.
1607 */
1608 qemu_cpu_kick_self();
1609 }
Glauber Costad549db52009-10-07 16:38:03 -03001610 qemu_mutex_unlock_iothread();
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001611
Andreas Färber1bc22652012-10-31 06:06:49 +01001612 run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
Jan Kiszka9ccfac92011-02-01 22:16:00 +01001613
Glauber Costad549db52009-10-07 16:38:03 -03001614 qemu_mutex_lock_iothread();
Andreas Färber20d695a2012-10-31 06:57:49 +01001615 kvm_arch_post_run(cpu, run);
aliguori05330442008-11-05 16:29:27 +00001616
Jan Kiszka7cbb5332011-03-15 12:26:25 +01001617 if (run_ret < 0) {
Jan Kiszkadc77d342011-03-15 12:26:26 +01001618 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1619 DPRINTF("io window exit\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001620 ret = EXCP_INTERRUPT;
Jan Kiszkadc77d342011-03-15 12:26:26 +01001621 break;
1622 }
Michael Ellerman7b011fb2011-12-16 11:20:20 +11001623 fprintf(stderr, "error: kvm run failed %s\n",
1624 strerror(-run_ret));
aliguori05330442008-11-05 16:29:27 +00001625 abort();
1626 }
1627
aliguori05330442008-11-05 16:29:27 +00001628 switch (run->exit_reason) {
1629 case KVM_EXIT_IO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001630 DPRINTF("handle_io\n");
Jan Kiszkab30e93e2011-02-01 22:16:01 +01001631 kvm_handle_io(run->io.port,
1632 (uint8_t *)run + run->io.data_offset,
1633 run->io.direction,
1634 run->io.size,
1635 run->io.count);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001636 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001637 break;
1638 case KVM_EXIT_MMIO:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001639 DPRINTF("handle_mmio\n");
aliguori05330442008-11-05 16:29:27 +00001640 cpu_physical_memory_rw(run->mmio.phys_addr,
1641 run->mmio.data,
1642 run->mmio.len,
1643 run->mmio.is_write);
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001644 ret = 0;
aliguori05330442008-11-05 16:29:27 +00001645 break;
1646 case KVM_EXIT_IRQ_WINDOW_OPEN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001647 DPRINTF("irq_window_open\n");
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001648 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001649 break;
1650 case KVM_EXIT_SHUTDOWN:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001651 DPRINTF("shutdown\n");
aliguori05330442008-11-05 16:29:27 +00001652 qemu_system_reset_request();
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001653 ret = EXCP_INTERRUPT;
aliguori05330442008-11-05 16:29:27 +00001654 break;
1655 case KVM_EXIT_UNKNOWN:
Jan Kiszkabb44e0d2011-01-21 21:48:07 +01001656 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1657 (uint64_t)run->hw.hardware_exit_reason);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001658 ret = -1;
aliguori05330442008-11-05 16:29:27 +00001659 break;
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001660 case KVM_EXIT_INTERNAL_ERROR:
Jan Kiszka73aaec42011-01-21 21:48:06 +01001661 ret = kvm_handle_internal_error(env, run);
Marcelo Tosatti7c80eef2010-03-23 13:37:11 -03001662 break;
aliguori05330442008-11-05 16:29:27 +00001663 default:
Blue Swirl8c0d5772010-04-18 14:22:14 +00001664 DPRINTF("kvm_arch_handle_exit\n");
Andreas Färber20d695a2012-10-31 06:57:49 +01001665 ret = kvm_arch_handle_exit(cpu, run);
aliguori05330442008-11-05 16:29:27 +00001666 break;
1667 }
Jan Kiszkad73cd8f2011-03-15 12:26:27 +01001668 } while (ret == 0);
aliguori05330442008-11-05 16:29:27 +00001669
Jan Kiszka73aaec42011-01-21 21:48:06 +01001670 if (ret < 0) {
Jan Kiszkaf5c848e2011-01-21 21:48:08 +01001671 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001672 vm_stop(RUN_STATE_INTERNAL_ERROR);
Jan Kiszka73aaec42011-01-21 21:48:06 +01001673 }
aliguoribecfc392008-11-10 15:55:14 +00001674
Andreas Färberfcd7d002012-12-17 08:02:44 +01001675 cpu->exit_request = 0;
aliguori05330442008-11-05 16:29:27 +00001676 return ret;
1677}
1678
aliguori984b5182008-11-13 19:21:00 +00001679int kvm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001680{
1681 int ret;
aliguori984b5182008-11-13 19:21:00 +00001682 void *arg;
1683 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001684
aliguori984b5182008-11-13 19:21:00 +00001685 va_start(ap, type);
1686 arg = va_arg(ap, void *);
1687 va_end(ap);
1688
1689 ret = ioctl(s->fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001690 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001691 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001692 }
aliguori05330442008-11-05 16:29:27 +00001693 return ret;
1694}
1695
aliguori984b5182008-11-13 19:21:00 +00001696int kvm_vm_ioctl(KVMState *s, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001697{
1698 int ret;
aliguori984b5182008-11-13 19:21:00 +00001699 void *arg;
1700 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001701
aliguori984b5182008-11-13 19:21:00 +00001702 va_start(ap, type);
1703 arg = va_arg(ap, void *);
1704 va_end(ap);
1705
1706 ret = ioctl(s->vmfd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001707 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001708 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001709 }
aliguori05330442008-11-05 16:29:27 +00001710 return ret;
1711}
1712
Andreas Färber1bc22652012-10-31 06:06:49 +01001713int kvm_vcpu_ioctl(CPUState *cpu, int type, ...)
aliguori05330442008-11-05 16:29:27 +00001714{
1715 int ret;
aliguori984b5182008-11-13 19:21:00 +00001716 void *arg;
1717 va_list ap;
aliguori05330442008-11-05 16:29:27 +00001718
aliguori984b5182008-11-13 19:21:00 +00001719 va_start(ap, type);
1720 arg = va_arg(ap, void *);
1721 va_end(ap);
1722
Andreas Färber8737c512012-10-31 05:29:00 +01001723 ret = ioctl(cpu->kvm_fd, type, arg);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001724 if (ret == -1) {
aliguori05330442008-11-05 16:29:27 +00001725 ret = -errno;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001726 }
aliguori05330442008-11-05 16:29:27 +00001727 return ret;
1728}
aliguoribd322082008-12-04 20:33:06 +00001729
1730int kvm_has_sync_mmu(void)
1731{
Jan Kiszka94a8d392011-01-21 21:48:17 +01001732 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
aliguoribd322082008-12-04 20:33:06 +00001733}
aliguorie22a25c2009-03-12 20:12:48 +00001734
Jan Kiszkaa0fb0022009-11-25 00:33:03 +01001735int kvm_has_vcpu_events(void)
1736{
1737 return kvm_state->vcpu_events;
1738}
1739
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001740int kvm_has_robust_singlestep(void)
1741{
1742 return kvm_state->robust_singlestep;
1743}
1744
Jan Kiszkaff44f1a2010-03-12 15:20:49 +01001745int kvm_has_debugregs(void)
1746{
1747 return kvm_state->debugregs;
1748}
1749
Sheng Yangf1665b22010-06-17 17:53:07 +08001750int kvm_has_xsave(void)
1751{
1752 return kvm_state->xsave;
1753}
1754
1755int kvm_has_xcrs(void)
1756{
1757 return kvm_state->xcrs;
1758}
1759
Jan Kiszka8a7c7392012-03-02 20:28:48 +01001760int kvm_has_pit_state2(void)
1761{
1762 return kvm_state->pit_state2;
1763}
1764
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +02001765int kvm_has_many_ioeventfds(void)
1766{
1767 if (!kvm_enabled()) {
1768 return 0;
1769 }
1770 return kvm_state->many_ioeventfds;
1771}
1772
Jan Kiszka84b058d2011-10-15 11:49:47 +02001773int kvm_has_gsi_routing(void)
1774{
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001775#ifdef KVM_CAP_IRQ_ROUTING
Jan Kiszka84b058d2011-10-15 11:49:47 +02001776 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
Alexander Grafa9c5eb02012-01-25 18:28:05 +01001777#else
1778 return false;
1779#endif
Jan Kiszka84b058d2011-10-15 11:49:47 +02001780}
1781
Jan Kiszka3ab73842012-08-27 08:28:39 +02001782int kvm_has_intx_set_mask(void)
1783{
1784 return kvm_state->intx_set_mask;
1785}
1786
Christian Borntraegerfdec9912012-06-15 05:10:30 +00001787void *kvm_vmalloc(ram_addr_t size)
1788{
1789#ifdef TARGET_S390X
1790 void *mem;
1791
1792 mem = kvm_arch_vmalloc(size);
1793 if (mem) {
1794 return mem;
1795 }
1796#endif
1797 return qemu_vmalloc(size);
1798}
1799
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001800void kvm_setup_guest_memory(void *start, size_t size)
1801{
Christian Borntraeger62fe8332012-08-10 15:11:45 +02001802#ifdef CONFIG_VALGRIND_H
1803 VALGRIND_MAKE_MEM_DEFINED(start, size);
1804#endif
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001805 if (!kvm_has_sync_mmu()) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001806 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001807
1808 if (ret) {
Andreas Färbere78815a2010-09-25 11:26:05 +00001809 perror("qemu_madvise");
1810 fprintf(stderr,
1811 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001812 exit(1);
1813 }
Jan Kiszka6f0437e2009-04-26 18:03:40 +02001814 }
1815}
1816
aliguorie22a25c2009-03-12 20:12:48 +00001817#ifdef KVM_CAP_SET_GUEST_DEBUG
Andreas Färbera60f24b2012-12-01 05:35:08 +01001818struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +00001819 target_ulong pc)
1820{
1821 struct kvm_sw_breakpoint *bp;
1822
Andreas Färbera60f24b2012-12-01 05:35:08 +01001823 QTAILQ_FOREACH(bp, &cpu->kvm_state->kvm_sw_breakpoints, entry) {
Jan Kiszkaa426e122011-01-04 09:32:13 +01001824 if (bp->pc == pc) {
aliguorie22a25c2009-03-12 20:12:48 +00001825 return bp;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001826 }
aliguorie22a25c2009-03-12 20:12:48 +00001827 }
1828 return NULL;
1829}
1830
Andreas Färbera60f24b2012-12-01 05:35:08 +01001831int kvm_sw_breakpoints_active(CPUState *cpu)
aliguorie22a25c2009-03-12 20:12:48 +00001832{
Andreas Färbera60f24b2012-12-01 05:35:08 +01001833 return !QTAILQ_EMPTY(&cpu->kvm_state->kvm_sw_breakpoints);
aliguorie22a25c2009-03-12 20:12:48 +00001834}
1835
Glauber Costa452e4752009-07-16 17:55:28 -04001836struct kvm_set_guest_debug_data {
1837 struct kvm_guest_debug dbg;
Andreas Färbera60f24b2012-12-01 05:35:08 +01001838 CPUState *cpu;
Glauber Costa452e4752009-07-16 17:55:28 -04001839 int err;
1840};
1841
1842static void kvm_invoke_set_guest_debug(void *data)
1843{
1844 struct kvm_set_guest_debug_data *dbg_data = data;
Jan Kiszkab3807722009-09-17 20:05:58 +02001845
Andreas Färbera60f24b2012-12-01 05:35:08 +01001846 dbg_data->err = kvm_vcpu_ioctl(dbg_data->cpu, KVM_SET_GUEST_DEBUG,
1847 &dbg_data->dbg);
Glauber Costa452e4752009-07-16 17:55:28 -04001848}
1849
Andreas Färber9349b4f2012-03-14 01:38:32 +01001850int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
aliguorie22a25c2009-03-12 20:12:48 +00001851{
Andreas Färberf100f0b2012-05-03 14:58:47 +02001852 CPUState *cpu = ENV_GET_CPU(env);
Glauber Costa452e4752009-07-16 17:55:28 -04001853 struct kvm_set_guest_debug_data data;
aliguorie22a25c2009-03-12 20:12:48 +00001854
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001855 data.dbg.control = reinject_trap;
aliguorie22a25c2009-03-12 20:12:48 +00001856
Jan Kiszkab0b1d692010-03-01 19:10:29 +01001857 if (env->singlestep_enabled) {
1858 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1859 }
Andreas Färber20d695a2012-10-31 06:57:49 +01001860 kvm_arch_update_guest_debug(cpu, &data.dbg);
Andreas Färbera60f24b2012-12-01 05:35:08 +01001861 data.cpu = cpu;
aliguorie22a25c2009-03-12 20:12:48 +00001862
Andreas Färberf100f0b2012-05-03 14:58:47 +02001863 run_on_cpu(cpu, kvm_invoke_set_guest_debug, &data);
Glauber Costa452e4752009-07-16 17:55:28 -04001864 return data.err;
aliguorie22a25c2009-03-12 20:12:48 +00001865}
1866
Andreas Färber9349b4f2012-03-14 01:38:32 +01001867int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001868 target_ulong len, int type)
1869{
Andreas Färber20d695a2012-10-31 06:57:49 +01001870 CPUState *current_cpu = ENV_GET_CPU(current_env);
aliguorie22a25c2009-03-12 20:12:48 +00001871 struct kvm_sw_breakpoint *bp;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001872 CPUArchState *env;
aliguorie22a25c2009-03-12 20:12:48 +00001873 int err;
1874
1875 if (type == GDB_BREAKPOINT_SW) {
Andreas Färbera60f24b2012-12-01 05:35:08 +01001876 bp = kvm_find_sw_breakpoint(current_cpu, addr);
aliguorie22a25c2009-03-12 20:12:48 +00001877 if (bp) {
1878 bp->use_count++;
1879 return 0;
1880 }
1881
Anthony Liguori7267c092011-08-20 22:09:37 -05001882 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
Jan Kiszkaa426e122011-01-04 09:32:13 +01001883 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001884 return -ENOMEM;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001885 }
aliguorie22a25c2009-03-12 20:12:48 +00001886
1887 bp->pc = addr;
1888 bp->use_count = 1;
Andreas Färber20d695a2012-10-31 06:57:49 +01001889 err = kvm_arch_insert_sw_breakpoint(current_cpu, bp);
aliguorie22a25c2009-03-12 20:12:48 +00001890 if (err) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001891 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001892 return err;
1893 }
1894
Andreas Färbera60f24b2012-12-01 05:35:08 +01001895 QTAILQ_INSERT_HEAD(&current_cpu->kvm_state->kvm_sw_breakpoints,
aliguorie22a25c2009-03-12 20:12:48 +00001896 bp, entry);
1897 } else {
1898 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001899 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001900 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001901 }
aliguorie22a25c2009-03-12 20:12:48 +00001902 }
1903
1904 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1905 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001906 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001907 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001908 }
aliguorie22a25c2009-03-12 20:12:48 +00001909 }
1910 return 0;
1911}
1912
Andreas Färber9349b4f2012-03-14 01:38:32 +01001913int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001914 target_ulong len, int type)
1915{
Andreas Färber20d695a2012-10-31 06:57:49 +01001916 CPUState *current_cpu = ENV_GET_CPU(current_env);
aliguorie22a25c2009-03-12 20:12:48 +00001917 struct kvm_sw_breakpoint *bp;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001918 CPUArchState *env;
aliguorie22a25c2009-03-12 20:12:48 +00001919 int err;
1920
1921 if (type == GDB_BREAKPOINT_SW) {
Andreas Färbera60f24b2012-12-01 05:35:08 +01001922 bp = kvm_find_sw_breakpoint(current_cpu, addr);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001923 if (!bp) {
aliguorie22a25c2009-03-12 20:12:48 +00001924 return -ENOENT;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001925 }
aliguorie22a25c2009-03-12 20:12:48 +00001926
1927 if (bp->use_count > 1) {
1928 bp->use_count--;
1929 return 0;
1930 }
1931
Andreas Färber20d695a2012-10-31 06:57:49 +01001932 err = kvm_arch_remove_sw_breakpoint(current_cpu, bp);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001933 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001934 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001935 }
aliguorie22a25c2009-03-12 20:12:48 +00001936
Andreas Färbera60f24b2012-12-01 05:35:08 +01001937 QTAILQ_REMOVE(&current_cpu->kvm_state->kvm_sw_breakpoints, bp, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001938 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001939 } else {
1940 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001941 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001942 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001943 }
aliguorie22a25c2009-03-12 20:12:48 +00001944 }
1945
1946 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1947 err = kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001948 if (err) {
aliguorie22a25c2009-03-12 20:12:48 +00001949 return err;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001950 }
aliguorie22a25c2009-03-12 20:12:48 +00001951 }
1952 return 0;
1953}
1954
Andreas Färber9349b4f2012-03-14 01:38:32 +01001955void kvm_remove_all_breakpoints(CPUArchState *current_env)
aliguorie22a25c2009-03-12 20:12:48 +00001956{
Andreas Färber20d695a2012-10-31 06:57:49 +01001957 CPUState *current_cpu = ENV_GET_CPU(current_env);
aliguorie22a25c2009-03-12 20:12:48 +00001958 struct kvm_sw_breakpoint *bp, *next;
Andreas Färbera60f24b2012-12-01 05:35:08 +01001959 KVMState *s = current_cpu->kvm_state;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001960 CPUArchState *env;
Andreas Färber20d695a2012-10-31 06:57:49 +01001961 CPUState *cpu;
aliguorie22a25c2009-03-12 20:12:48 +00001962
Blue Swirl72cf2d42009-09-12 07:36:22 +00001963 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
Andreas Färber20d695a2012-10-31 06:57:49 +01001964 if (kvm_arch_remove_sw_breakpoint(current_cpu, bp) != 0) {
aliguorie22a25c2009-03-12 20:12:48 +00001965 /* Try harder to find a CPU that currently sees the breakpoint. */
1966 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Andreas Färber20d695a2012-10-31 06:57:49 +01001967 cpu = ENV_GET_CPU(env);
1968 if (kvm_arch_remove_sw_breakpoint(cpu, bp) == 0) {
aliguorie22a25c2009-03-12 20:12:48 +00001969 break;
Jan Kiszkaa426e122011-01-04 09:32:13 +01001970 }
aliguorie22a25c2009-03-12 20:12:48 +00001971 }
1972 }
Jan Kiszka78021d62012-11-12 15:04:35 +01001973 QTAILQ_REMOVE(&s->kvm_sw_breakpoints, bp, entry);
1974 g_free(bp);
aliguorie22a25c2009-03-12 20:12:48 +00001975 }
1976 kvm_arch_remove_all_hw_breakpoints();
1977
Jan Kiszkaa426e122011-01-04 09:32:13 +01001978 for (env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorie22a25c2009-03-12 20:12:48 +00001979 kvm_update_guest_debug(env, 0);
Jan Kiszkaa426e122011-01-04 09:32:13 +01001980 }
aliguorie22a25c2009-03-12 20:12:48 +00001981}
1982
1983#else /* !KVM_CAP_SET_GUEST_DEBUG */
1984
Andreas Färber9349b4f2012-03-14 01:38:32 +01001985int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
aliguorie22a25c2009-03-12 20:12:48 +00001986{
1987 return -EINVAL;
1988}
1989
Andreas Färber9349b4f2012-03-14 01:38:32 +01001990int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001991 target_ulong len, int type)
1992{
1993 return -EINVAL;
1994}
1995
Andreas Färber9349b4f2012-03-14 01:38:32 +01001996int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +00001997 target_ulong len, int type)
1998{
1999 return -EINVAL;
2000}
2001
Andreas Färber9349b4f2012-03-14 01:38:32 +01002002void kvm_remove_all_breakpoints(CPUArchState *current_env)
aliguorie22a25c2009-03-12 20:12:48 +00002003{
2004}
2005#endif /* !KVM_CAP_SET_GUEST_DEBUG */
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002006
Andreas Färber9349b4f2012-03-14 01:38:32 +01002007int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002008{
Andreas Färber1bc22652012-10-31 06:06:49 +01002009 CPUState *cpu = ENV_GET_CPU(env);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002010 struct kvm_signal_mask *sigmask;
2011 int r;
2012
Jan Kiszkaa426e122011-01-04 09:32:13 +01002013 if (!sigset) {
Andreas Färber1bc22652012-10-31 06:06:49 +01002014 return kvm_vcpu_ioctl(cpu, KVM_SET_SIGNAL_MASK, NULL);
Jan Kiszkaa426e122011-01-04 09:32:13 +01002015 }
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002016
Anthony Liguori7267c092011-08-20 22:09:37 -05002017 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002018
2019 sigmask->len = 8;
2020 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
Andreas Färber1bc22652012-10-31 06:06:49 +01002021 r = kvm_vcpu_ioctl(cpu, KVM_SET_SIGNAL_MASK, sigmask);
Anthony Liguori7267c092011-08-20 22:09:37 -05002022 g_free(sigmask);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002023
2024 return r;
2025}
Andreas Färber290adf32013-01-17 09:30:27 +01002026int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01002027{
Andreas Färber20d695a2012-10-31 06:57:49 +01002028 return kvm_arch_on_sigbus_vcpu(cpu, code, addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01002029}
2030
2031int kvm_on_sigbus(int code, void *addr)
2032{
2033 return kvm_arch_on_sigbus(code, addr);
2034}