]>
Commit | Line | Data |
---|---|---|
05330448 AL |
1 | /* |
2 | * QEMU KVM support | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5832d1f2 | 5 | * Red Hat, Inc. 2008 |
05330448 AL |
6 | * |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
5832d1f2 | 9 | * Glauber Costa <gcosta@redhat.com> |
05330448 AL |
10 | * |
11 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
12 | * See the COPYING file in the top-level directory. | |
13 | * | |
14 | */ | |
15 | ||
d38ea87a | 16 | #include "qemu/osdep.h" |
05330448 | 17 | #include <sys/ioctl.h> |
05330448 AL |
18 | |
19 | #include <linux/kvm.h> | |
20 | ||
1de7afc9 PB |
21 | #include "qemu/atomic.h" |
22 | #include "qemu/option.h" | |
23 | #include "qemu/config-file.h" | |
4b3cfe72 | 24 | #include "qemu/error-report.h" |
556969e9 | 25 | #include "qapi/error.h" |
a2cb15b0 | 26 | #include "hw/pci/msi.h" |
d1f6af6a | 27 | #include "hw/pci/msix.h" |
d426d9fb | 28 | #include "hw/s390x/adapter.h" |
022c62cb | 29 | #include "exec/gdbstub.h" |
8571ed35 | 30 | #include "sysemu/kvm_int.h" |
54d31236 | 31 | #include "sysemu/runstate.h" |
d2528bdc | 32 | #include "sysemu/cpus.h" |
1de7afc9 | 33 | #include "qemu/bswap.h" |
022c62cb | 34 | #include "exec/memory.h" |
747afd5b | 35 | #include "exec/ram_addr.h" |
1de7afc9 | 36 | #include "qemu/event_notifier.h" |
db725815 | 37 | #include "qemu/main-loop.h" |
92229a57 | 38 | #include "trace.h" |
197e3524 | 39 | #include "hw/irq.h" |
23b0898e | 40 | #include "qapi/visitor.h" |
11bc4a13 PB |
41 | #include "qapi/qapi-types-common.h" |
42 | #include "qapi/qapi-visit-common.h" | |
6b552b9b | 43 | #include "sysemu/reset.h" |
57038a92 CF |
44 | #include "qemu/guest-random.h" |
45 | #include "sysemu/hw_accel.h" | |
46 | #include "kvm-cpus.h" | |
05330448 | 47 | |
135a129a AK |
48 | #include "hw/boards.h" |
49 | ||
d2f2b8a7 SH |
50 | /* This check must be after config-host.h is included */ |
51 | #ifdef CONFIG_EVENTFD | |
52 | #include <sys/eventfd.h> | |
53 | #endif | |
54 | ||
bc92e4e9 AJ |
55 | /* KVM uses PAGE_SIZE in its definition of KVM_COALESCED_MMIO_MAX. We |
56 | * need to use the real host PAGE_SIZE, as that's what KVM will use. | |
57 | */ | |
eb8b1a79 JY |
58 | #ifdef PAGE_SIZE |
59 | #undef PAGE_SIZE | |
60 | #endif | |
038adc2f | 61 | #define PAGE_SIZE qemu_real_host_page_size |
f65ed4c1 | 62 | |
05330448 AL |
63 | //#define DEBUG_KVM |
64 | ||
65 | #ifdef DEBUG_KVM | |
8c0d577e | 66 | #define DPRINTF(fmt, ...) \ |
05330448 AL |
67 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
68 | #else | |
8c0d577e | 69 | #define DPRINTF(fmt, ...) \ |
05330448 AL |
70 | do { } while (0) |
71 | #endif | |
72 | ||
04fa27f5 JK |
73 | #define KVM_MSI_HASHTAB_SIZE 256 |
74 | ||
4c055ab5 GZ |
75 | struct KVMParkedVcpu { |
76 | unsigned long vcpu_id; | |
77 | int kvm_fd; | |
78 | QLIST_ENTRY(KVMParkedVcpu) node; | |
79 | }; | |
80 | ||
9d1c35df | 81 | struct KVMState |
05330448 | 82 | { |
fc02086b EH |
83 | AccelState parent_obj; |
84 | ||
fb541ca5 | 85 | int nr_slots; |
05330448 AL |
86 | int fd; |
87 | int vmfd; | |
f65ed4c1 | 88 | int coalesced_mmio; |
e6d34aee | 89 | int coalesced_pio; |
62a2744c | 90 | struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; |
1cae88b9 | 91 | bool coalesced_flush_in_progress; |
a0fb002c | 92 | int vcpu_events; |
b0b1d690 | 93 | int robust_singlestep; |
ff44f1a3 | 94 | int debugregs; |
e22a25c9 | 95 | #ifdef KVM_CAP_SET_GUEST_DEBUG |
b58deb34 | 96 | QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints; |
e22a25c9 | 97 | #endif |
ebbfef2f | 98 | int max_nested_state_len; |
d2f2b8a7 | 99 | int many_ioeventfds; |
3ab73842 | 100 | int intx_set_mask; |
23b0898e | 101 | int kvm_shadow_mem; |
11bc4a13 PB |
102 | bool kernel_irqchip_allowed; |
103 | bool kernel_irqchip_required; | |
d1972be1 | 104 | OnOffAuto kernel_irqchip_split; |
62dd4eda | 105 | bool sync_mmu; |
494cd11d | 106 | uint64_t manual_dirty_log_protect; |
92e4b519 DG |
107 | /* The man page (and posix) say ioctl numbers are signed int, but |
108 | * they're not. Linux, glibc and *BSD all treat ioctl numbers as | |
109 | * unsigned, and treating them as signed here can break things */ | |
e333cd69 | 110 | unsigned irq_set_ioctl; |
aed6efb9 | 111 | unsigned int sigmask_len; |
197e3524 | 112 | GHashTable *gsimap; |
84b058d7 JK |
113 | #ifdef KVM_CAP_IRQ_ROUTING |
114 | struct kvm_irq_routing *irq_routes; | |
115 | int nr_allocated_irq_routes; | |
8269fb70 | 116 | unsigned long *used_gsi_bitmap; |
4e2e4e63 | 117 | unsigned int gsi_count; |
b58deb34 | 118 | QTAILQ_HEAD(, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE]; |
84b058d7 | 119 | #endif |
7bbda04c | 120 | KVMMemoryListener memory_listener; |
4c055ab5 | 121 | QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus; |
b20e3780 | 122 | |
8072aae3 AK |
123 | /* For "info mtree -f" to tell if an MR is registered in KVM */ |
124 | int nr_as; | |
125 | struct KVMAs { | |
126 | KVMMemoryListener *ml; | |
127 | AddressSpace *as; | |
128 | } *as; | |
9d1c35df | 129 | }; |
05330448 | 130 | |
6a7af8cb | 131 | KVMState *kvm_state; |
3d4b2649 | 132 | bool kvm_kernel_irqchip; |
15eafc2e | 133 | bool kvm_split_irqchip; |
7ae26bd4 | 134 | bool kvm_async_interrupts_allowed; |
215e79c0 | 135 | bool kvm_halt_in_kernel_allowed; |
69e03ae6 | 136 | bool kvm_eventfds_allowed; |
cc7e0ddf | 137 | bool kvm_irqfds_allowed; |
f41389ae | 138 | bool kvm_resamplefds_allowed; |
614e41bc | 139 | bool kvm_msi_via_irqfd_allowed; |
f3e1bed8 | 140 | bool kvm_gsi_routing_allowed; |
76fe21de | 141 | bool kvm_gsi_direct_mapping; |
13eed94e | 142 | bool kvm_allowed; |
df9c8b75 | 143 | bool kvm_readonly_mem_allowed; |
d0a073a1 | 144 | bool kvm_vm_attributes_allowed; |
50bf31b9 | 145 | bool kvm_direct_msi_allowed; |
35108223 | 146 | bool kvm_ioeventfd_any_length_allowed; |
767a554a | 147 | bool kvm_msi_use_devid; |
cf0f7cf9 | 148 | static bool kvm_immediate_exit; |
023ae9a8 | 149 | static hwaddr kvm_max_slot_size = ~0; |
05330448 | 150 | |
94a8d39a JK |
151 | static const KVMCapabilityInfo kvm_required_capabilites[] = { |
152 | KVM_CAP_INFO(USER_MEMORY), | |
153 | KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS), | |
89de4b91 | 154 | KVM_CAP_INFO(JOIN_MEMORY_REGIONS_WORKS), |
94a8d39a JK |
155 | KVM_CAP_LAST_INFO |
156 | }; | |
157 | ||
3607715a DG |
158 | static NotifierList kvm_irqchip_change_notifiers = |
159 | NOTIFIER_LIST_INITIALIZER(kvm_irqchip_change_notifiers); | |
160 | ||
c82d9d43 PX |
161 | struct KVMResampleFd { |
162 | int gsi; | |
163 | EventNotifier *resample_event; | |
164 | QLIST_ENTRY(KVMResampleFd) node; | |
165 | }; | |
166 | typedef struct KVMResampleFd KVMResampleFd; | |
167 | ||
168 | /* | |
169 | * Only used with split irqchip where we need to do the resample fd | |
170 | * kick for the kernel from userspace. | |
171 | */ | |
172 | static QLIST_HEAD(, KVMResampleFd) kvm_resample_fd_list = | |
173 | QLIST_HEAD_INITIALIZER(kvm_resample_fd_list); | |
174 | ||
a2f77862 PX |
175 | static QemuMutex kml_slots_lock; |
176 | ||
177 | #define kvm_slots_lock() qemu_mutex_lock(&kml_slots_lock) | |
178 | #define kvm_slots_unlock() qemu_mutex_unlock(&kml_slots_lock) | |
36adac49 | 179 | |
ea776d15 PX |
180 | static void kvm_slot_init_dirty_bitmap(KVMSlot *mem); |
181 | ||
c82d9d43 PX |
182 | static inline void kvm_resample_fd_remove(int gsi) |
183 | { | |
184 | KVMResampleFd *rfd; | |
185 | ||
186 | QLIST_FOREACH(rfd, &kvm_resample_fd_list, node) { | |
187 | if (rfd->gsi == gsi) { | |
188 | QLIST_REMOVE(rfd, node); | |
189 | g_free(rfd); | |
190 | break; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | static inline void kvm_resample_fd_insert(int gsi, EventNotifier *event) | |
196 | { | |
197 | KVMResampleFd *rfd = g_new0(KVMResampleFd, 1); | |
198 | ||
199 | rfd->gsi = gsi; | |
200 | rfd->resample_event = event; | |
201 | ||
202 | QLIST_INSERT_HEAD(&kvm_resample_fd_list, rfd, node); | |
203 | } | |
204 | ||
205 | void kvm_resample_fd_notify(int gsi) | |
206 | { | |
207 | KVMResampleFd *rfd; | |
208 | ||
209 | QLIST_FOREACH(rfd, &kvm_resample_fd_list, node) { | |
210 | if (rfd->gsi == gsi) { | |
211 | event_notifier_set(rfd->resample_event); | |
212 | trace_kvm_resample_fd_notify(gsi); | |
213 | return; | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
44f2e6c1 BR |
218 | int kvm_get_max_memslots(void) |
219 | { | |
4f7f5893 | 220 | KVMState *s = KVM_STATE(current_accel()); |
44f2e6c1 BR |
221 | |
222 | return s->nr_slots; | |
223 | } | |
224 | ||
36adac49 | 225 | /* Called with KVMMemoryListener.slots_lock held */ |
7bbda04c | 226 | static KVMSlot *kvm_get_free_slot(KVMMemoryListener *kml) |
05330448 | 227 | { |
7bbda04c | 228 | KVMState *s = kvm_state; |
05330448 AL |
229 | int i; |
230 | ||
fb541ca5 | 231 | for (i = 0; i < s->nr_slots; i++) { |
7bbda04c PB |
232 | if (kml->slots[i].memory_size == 0) { |
233 | return &kml->slots[i]; | |
a426e122 | 234 | } |
05330448 AL |
235 | } |
236 | ||
b8865591 IM |
237 | return NULL; |
238 | } | |
239 | ||
240 | bool kvm_has_free_slot(MachineState *ms) | |
241 | { | |
7bbda04c | 242 | KVMState *s = KVM_STATE(ms->accelerator); |
36adac49 PX |
243 | bool result; |
244 | KVMMemoryListener *kml = &s->memory_listener; | |
245 | ||
a2f77862 | 246 | kvm_slots_lock(); |
36adac49 | 247 | result = !!kvm_get_free_slot(kml); |
a2f77862 | 248 | kvm_slots_unlock(); |
7bbda04c | 249 | |
36adac49 | 250 | return result; |
b8865591 IM |
251 | } |
252 | ||
36adac49 | 253 | /* Called with KVMMemoryListener.slots_lock held */ |
7bbda04c | 254 | static KVMSlot *kvm_alloc_slot(KVMMemoryListener *kml) |
b8865591 | 255 | { |
7bbda04c | 256 | KVMSlot *slot = kvm_get_free_slot(kml); |
b8865591 IM |
257 | |
258 | if (slot) { | |
259 | return slot; | |
260 | } | |
261 | ||
d3f8d37f AL |
262 | fprintf(stderr, "%s: no free slot available\n", __func__); |
263 | abort(); | |
264 | } | |
265 | ||
7bbda04c | 266 | static KVMSlot *kvm_lookup_matching_slot(KVMMemoryListener *kml, |
a8170e5e | 267 | hwaddr start_addr, |
2747e716 | 268 | hwaddr size) |
d3f8d37f | 269 | { |
7bbda04c | 270 | KVMState *s = kvm_state; |
d3f8d37f AL |
271 | int i; |
272 | ||
fb541ca5 | 273 | for (i = 0; i < s->nr_slots; i++) { |
7bbda04c | 274 | KVMSlot *mem = &kml->slots[i]; |
d3f8d37f | 275 | |
2747e716 | 276 | if (start_addr == mem->start_addr && size == mem->memory_size) { |
d3f8d37f AL |
277 | return mem; |
278 | } | |
279 | } | |
280 | ||
05330448 AL |
281 | return NULL; |
282 | } | |
283 | ||
5ea69c2e DH |
284 | /* |
285 | * Calculate and align the start address and the size of the section. | |
286 | * Return the size. If the size is 0, the aligned section is empty. | |
287 | */ | |
288 | static hwaddr kvm_align_section(MemoryRegionSection *section, | |
289 | hwaddr *start) | |
290 | { | |
291 | hwaddr size = int128_get64(section->size); | |
a6ffc423 | 292 | hwaddr delta, aligned; |
5ea69c2e DH |
293 | |
294 | /* kvm works in page size chunks, but the function may be called | |
295 | with sub-page size and unaligned start address. Pad the start | |
296 | address to next and truncate size to previous page boundary. */ | |
a6ffc423 DH |
297 | aligned = ROUND_UP(section->offset_within_address_space, |
298 | qemu_real_host_page_size); | |
299 | delta = aligned - section->offset_within_address_space; | |
300 | *start = aligned; | |
5ea69c2e DH |
301 | if (delta > size) { |
302 | return 0; | |
303 | } | |
5ea69c2e | 304 | |
a6ffc423 | 305 | return (size - delta) & qemu_real_host_page_mask; |
5ea69c2e DH |
306 | } |
307 | ||
9f213ed9 | 308 | int kvm_physical_memory_addr_from_host(KVMState *s, void *ram, |
a8170e5e | 309 | hwaddr *phys_addr) |
983dfc3b | 310 | { |
7bbda04c | 311 | KVMMemoryListener *kml = &s->memory_listener; |
36adac49 | 312 | int i, ret = 0; |
983dfc3b | 313 | |
a2f77862 | 314 | kvm_slots_lock(); |
fb541ca5 | 315 | for (i = 0; i < s->nr_slots; i++) { |
7bbda04c | 316 | KVMSlot *mem = &kml->slots[i]; |
983dfc3b | 317 | |
9f213ed9 AK |
318 | if (ram >= mem->ram && ram < mem->ram + mem->memory_size) { |
319 | *phys_addr = mem->start_addr + (ram - mem->ram); | |
36adac49 PX |
320 | ret = 1; |
321 | break; | |
983dfc3b HY |
322 | } |
323 | } | |
a2f77862 | 324 | kvm_slots_unlock(); |
983dfc3b | 325 | |
36adac49 | 326 | return ret; |
983dfc3b HY |
327 | } |
328 | ||
6c090d4a | 329 | static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, bool new) |
5832d1f2 | 330 | { |
7bbda04c | 331 | KVMState *s = kvm_state; |
5832d1f2 | 332 | struct kvm_userspace_memory_region mem; |
fe29141b | 333 | int ret; |
5832d1f2 | 334 | |
38bfe691 | 335 | mem.slot = slot->slot | (kml->as_id << 16); |
5832d1f2 | 336 | mem.guest_phys_addr = slot->start_addr; |
9f213ed9 | 337 | mem.userspace_addr = (unsigned long)slot->ram; |
5832d1f2 | 338 | mem.flags = slot->flags; |
651eb0f4 | 339 | |
6c090d4a | 340 | if (slot->memory_size && !new && (mem.flags ^ slot->old_flags) & KVM_MEM_READONLY) { |
235e8982 JJ |
341 | /* Set the slot size to 0 before setting the slot to the desired |
342 | * value. This is needed based on KVM commit 75d61fbc. */ | |
343 | mem.memory_size = 0; | |
88cd34ee PMD |
344 | ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); |
345 | if (ret < 0) { | |
346 | goto err; | |
347 | } | |
235e8982 JJ |
348 | } |
349 | mem.memory_size = slot->memory_size; | |
fe29141b | 350 | ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); |
6c090d4a | 351 | slot->old_flags = mem.flags; |
88cd34ee | 352 | err: |
fe29141b AK |
353 | trace_kvm_set_user_memory(mem.slot, mem.flags, mem.guest_phys_addr, |
354 | mem.memory_size, mem.userspace_addr, ret); | |
88cd34ee PMD |
355 | if (ret < 0) { |
356 | error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot=%d," | |
357 | " start=0x%" PRIx64 ", size=0x%" PRIx64 ": %s", | |
358 | __func__, mem.slot, slot->start_addr, | |
359 | (uint64_t)mem.memory_size, strerror(errno)); | |
360 | } | |
fe29141b | 361 | return ret; |
5832d1f2 AL |
362 | } |
363 | ||
57038a92 | 364 | static int do_kvm_destroy_vcpu(CPUState *cpu) |
4c055ab5 GZ |
365 | { |
366 | KVMState *s = kvm_state; | |
367 | long mmap_size; | |
368 | struct KVMParkedVcpu *vcpu = NULL; | |
369 | int ret = 0; | |
370 | ||
371 | DPRINTF("kvm_destroy_vcpu\n"); | |
372 | ||
b1115c99 LA |
373 | ret = kvm_arch_destroy_vcpu(cpu); |
374 | if (ret < 0) { | |
375 | goto err; | |
376 | } | |
377 | ||
4c055ab5 GZ |
378 | mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); |
379 | if (mmap_size < 0) { | |
380 | ret = mmap_size; | |
381 | DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n"); | |
382 | goto err; | |
383 | } | |
384 | ||
385 | ret = munmap(cpu->kvm_run, mmap_size); | |
386 | if (ret < 0) { | |
387 | goto err; | |
388 | } | |
389 | ||
390 | vcpu = g_malloc0(sizeof(*vcpu)); | |
391 | vcpu->vcpu_id = kvm_arch_vcpu_id(cpu); | |
392 | vcpu->kvm_fd = cpu->kvm_fd; | |
393 | QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node); | |
394 | err: | |
395 | return ret; | |
396 | } | |
397 | ||
57038a92 CF |
398 | void kvm_destroy_vcpu(CPUState *cpu) |
399 | { | |
400 | if (do_kvm_destroy_vcpu(cpu) < 0) { | |
401 | error_report("kvm_destroy_vcpu failed"); | |
402 | exit(EXIT_FAILURE); | |
403 | } | |
404 | } | |
405 | ||
4c055ab5 GZ |
406 | static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id) |
407 | { | |
408 | struct KVMParkedVcpu *cpu; | |
409 | ||
410 | QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) { | |
411 | if (cpu->vcpu_id == vcpu_id) { | |
412 | int kvm_fd; | |
413 | ||
414 | QLIST_REMOVE(cpu, node); | |
415 | kvm_fd = cpu->kvm_fd; | |
416 | g_free(cpu); | |
417 | return kvm_fd; | |
418 | } | |
419 | } | |
420 | ||
421 | return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id); | |
422 | } | |
423 | ||
d0a92b35 | 424 | int kvm_init_vcpu(CPUState *cpu, Error **errp) |
05330448 AL |
425 | { |
426 | KVMState *s = kvm_state; | |
427 | long mmap_size; | |
428 | int ret; | |
429 | ||
d0a92b35 | 430 | trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); |
05330448 | 431 | |
4c055ab5 | 432 | ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu)); |
05330448 | 433 | if (ret < 0) { |
d0a92b35 DDAG |
434 | error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu failed (%lu)", |
435 | kvm_arch_vcpu_id(cpu)); | |
05330448 AL |
436 | goto err; |
437 | } | |
438 | ||
8737c51c | 439 | cpu->kvm_fd = ret; |
a60f24b5 | 440 | cpu->kvm_state = s; |
99f31832 | 441 | cpu->vcpu_dirty = true; |
05330448 AL |
442 | |
443 | mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); | |
444 | if (mmap_size < 0) { | |
748a680b | 445 | ret = mmap_size; |
d0a92b35 DDAG |
446 | error_setg_errno(errp, -mmap_size, |
447 | "kvm_init_vcpu: KVM_GET_VCPU_MMAP_SIZE failed"); | |
05330448 AL |
448 | goto err; |
449 | } | |
450 | ||
f7575c96 | 451 | cpu->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
8737c51c | 452 | cpu->kvm_fd, 0); |
f7575c96 | 453 | if (cpu->kvm_run == MAP_FAILED) { |
05330448 | 454 | ret = -errno; |
d0a92b35 DDAG |
455 | error_setg_errno(errp, ret, |
456 | "kvm_init_vcpu: mmap'ing vcpu state failed (%lu)", | |
457 | kvm_arch_vcpu_id(cpu)); | |
05330448 AL |
458 | goto err; |
459 | } | |
460 | ||
a426e122 JK |
461 | if (s->coalesced_mmio && !s->coalesced_mmio_ring) { |
462 | s->coalesced_mmio_ring = | |
f7575c96 | 463 | (void *)cpu->kvm_run + s->coalesced_mmio * PAGE_SIZE; |
a426e122 | 464 | } |
62a2744c | 465 | |
20d695a9 | 466 | ret = kvm_arch_init_vcpu(cpu); |
d0a92b35 DDAG |
467 | if (ret < 0) { |
468 | error_setg_errno(errp, -ret, | |
469 | "kvm_init_vcpu: kvm_arch_init_vcpu failed (%lu)", | |
470 | kvm_arch_vcpu_id(cpu)); | |
471 | } | |
05330448 AL |
472 | err: |
473 | return ret; | |
474 | } | |
475 | ||
5832d1f2 AL |
476 | /* |
477 | * dirty pages logging control | |
478 | */ | |
25254bbc | 479 | |
d6ff5cbc | 480 | static int kvm_mem_flags(MemoryRegion *mr) |
25254bbc | 481 | { |
d6ff5cbc | 482 | bool readonly = mr->readonly || memory_region_is_romd(mr); |
235e8982 | 483 | int flags = 0; |
d6ff5cbc AJ |
484 | |
485 | if (memory_region_get_dirty_log_mask(mr) != 0) { | |
486 | flags |= KVM_MEM_LOG_DIRTY_PAGES; | |
487 | } | |
235e8982 JJ |
488 | if (readonly && kvm_readonly_mem_allowed) { |
489 | flags |= KVM_MEM_READONLY; | |
490 | } | |
491 | return flags; | |
25254bbc MT |
492 | } |
493 | ||
36adac49 | 494 | /* Called with KVMMemoryListener.slots_lock held */ |
7bbda04c PB |
495 | static int kvm_slot_update_flags(KVMMemoryListener *kml, KVMSlot *mem, |
496 | MemoryRegion *mr) | |
5832d1f2 | 497 | { |
d6ff5cbc | 498 | mem->flags = kvm_mem_flags(mr); |
5832d1f2 | 499 | |
4495d6a7 | 500 | /* If nothing changed effectively, no need to issue ioctl */ |
6c090d4a | 501 | if (mem->flags == mem->old_flags) { |
25254bbc | 502 | return 0; |
4495d6a7 JK |
503 | } |
504 | ||
ea776d15 | 505 | kvm_slot_init_dirty_bitmap(mem); |
6c090d4a | 506 | return kvm_set_user_memory_region(kml, mem, false); |
5832d1f2 AL |
507 | } |
508 | ||
7bbda04c PB |
509 | static int kvm_section_update_flags(KVMMemoryListener *kml, |
510 | MemoryRegionSection *section) | |
25254bbc | 511 | { |
023ae9a8 | 512 | hwaddr start_addr, size, slot_size; |
343562e8 | 513 | KVMSlot *mem; |
36adac49 | 514 | int ret = 0; |
25254bbc | 515 | |
343562e8 DH |
516 | size = kvm_align_section(section, &start_addr); |
517 | if (!size) { | |
ea8cb1a8 | 518 | return 0; |
25254bbc | 519 | } |
343562e8 | 520 | |
a2f77862 | 521 | kvm_slots_lock(); |
36adac49 | 522 | |
023ae9a8 IM |
523 | while (size && !ret) { |
524 | slot_size = MIN(kvm_max_slot_size, size); | |
525 | mem = kvm_lookup_matching_slot(kml, start_addr, slot_size); | |
526 | if (!mem) { | |
527 | /* We don't have a slot if we want to trap every access. */ | |
528 | goto out; | |
529 | } | |
343562e8 | 530 | |
023ae9a8 IM |
531 | ret = kvm_slot_update_flags(kml, mem, section->mr); |
532 | start_addr += slot_size; | |
533 | size -= slot_size; | |
534 | } | |
36adac49 PX |
535 | |
536 | out: | |
a2f77862 | 537 | kvm_slots_unlock(); |
36adac49 | 538 | return ret; |
25254bbc MT |
539 | } |
540 | ||
a01672d3 | 541 | static void kvm_log_start(MemoryListener *listener, |
b2dfd71c PB |
542 | MemoryRegionSection *section, |
543 | int old, int new) | |
5832d1f2 | 544 | { |
7bbda04c | 545 | KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); |
a01672d3 AK |
546 | int r; |
547 | ||
b2dfd71c PB |
548 | if (old != 0) { |
549 | return; | |
550 | } | |
551 | ||
7bbda04c | 552 | r = kvm_section_update_flags(kml, section); |
a01672d3 AK |
553 | if (r < 0) { |
554 | abort(); | |
555 | } | |
5832d1f2 AL |
556 | } |
557 | ||
a01672d3 | 558 | static void kvm_log_stop(MemoryListener *listener, |
b2dfd71c PB |
559 | MemoryRegionSection *section, |
560 | int old, int new) | |
5832d1f2 | 561 | { |
7bbda04c | 562 | KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); |
a01672d3 AK |
563 | int r; |
564 | ||
b2dfd71c PB |
565 | if (new != 0) { |
566 | return; | |
567 | } | |
568 | ||
7bbda04c | 569 | r = kvm_section_update_flags(kml, section); |
a01672d3 AK |
570 | if (r < 0) { |
571 | abort(); | |
572 | } | |
5832d1f2 AL |
573 | } |
574 | ||
8369e01c | 575 | /* get kvm's dirty pages bitmap and update qemu's */ |
2c20b27e | 576 | static void kvm_slot_sync_dirty_pages(KVMSlot *slot) |
96c1606b | 577 | { |
2c20b27e PX |
578 | ram_addr_t start = slot->ram_start_offset; |
579 | ram_addr_t pages = slot->memory_size / qemu_real_host_page_size; | |
5ff7fb77 | 580 | |
2c20b27e | 581 | cpu_physical_memory_set_dirty_lebitmap(slot->dirty_bmap, start, pages); |
96c1606b AG |
582 | } |
583 | ||
8369e01c MT |
584 | #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) |
585 | ||
9b3a31c7 | 586 | /* Allocate the dirty bitmap for a slot */ |
ea776d15 | 587 | static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) |
9b3a31c7 | 588 | { |
ea776d15 PX |
589 | if (!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) || mem->dirty_bmap) { |
590 | return; | |
591 | } | |
592 | ||
9b3a31c7 DDAG |
593 | /* |
594 | * XXX bad kernel interface alert | |
595 | * For dirty bitmap, kernel allocates array of size aligned to | |
596 | * bits-per-long. But for case when the kernel is 64bits and | |
597 | * the userspace is 32bits, userspace can't align to the same | |
598 | * bits-per-long, since sizeof(long) is different between kernel | |
599 | * and user space. This way, userspace will provide buffer which | |
600 | * may be 4 bytes less than the kernel will use, resulting in | |
601 | * userspace memory corruption (which is not detectable by valgrind | |
602 | * too, in most cases). | |
603 | * So for now, let's align to 64 instead of HOST_LONG_BITS here, in | |
604 | * a hope that sizeof(long) won't become >8 any time soon. | |
e0a8f993 KZ |
605 | * |
606 | * Note: the granule of kvm dirty log is qemu_real_host_page_size. | |
607 | * And mem->memory_size is aligned to it (otherwise this mem can't | |
608 | * be registered to KVM). | |
9b3a31c7 | 609 | */ |
e0a8f993 | 610 | hwaddr bitmap_size = ALIGN(mem->memory_size / qemu_real_host_page_size, |
9b3a31c7 DDAG |
611 | /*HOST_LONG_BITS*/ 64) / 8; |
612 | mem->dirty_bmap = g_malloc0(bitmap_size); | |
613 | } | |
614 | ||
e65e5f50 PX |
615 | /* |
616 | * Sync dirty bitmap from kernel to KVMSlot.dirty_bmap, return true if | |
617 | * succeeded, false otherwise | |
618 | */ | |
619 | static bool kvm_slot_get_dirty_log(KVMState *s, KVMSlot *slot) | |
620 | { | |
621 | struct kvm_dirty_log d = {}; | |
622 | int ret; | |
623 | ||
624 | d.dirty_bitmap = slot->dirty_bmap; | |
625 | d.slot = slot->slot | (slot->as_id << 16); | |
626 | ret = kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); | |
627 | ||
628 | if (ret == -ENOENT) { | |
629 | /* kernel does not have dirty bitmap in this slot */ | |
630 | ret = 0; | |
631 | } | |
632 | if (ret) { | |
633 | error_report_once("%s: KVM_GET_DIRTY_LOG failed with %d", | |
634 | __func__, ret); | |
635 | } | |
636 | return ret == 0; | |
637 | } | |
638 | ||
5832d1f2 | 639 | /** |
4a12a11a | 640 | * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space |
5832d1f2 | 641 | * |
4a12a11a PX |
642 | * This function will first try to fetch dirty bitmap from the kernel, |
643 | * and then updates qemu's dirty bitmap. | |
644 | * | |
36adac49 PX |
645 | * NOTE: caller must be with kml->slots_lock held. |
646 | * | |
4a12a11a PX |
647 | * @kml: the KVM memory listener object |
648 | * @section: the memory section to sync the dirty bitmap with | |
5832d1f2 | 649 | */ |
e65e5f50 PX |
650 | static void kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, |
651 | MemoryRegionSection *section) | |
5832d1f2 AL |
652 | { |
653 | KVMState *s = kvm_state; | |
151f7749 | 654 | KVMSlot *mem; |
67548f09 | 655 | hwaddr start_addr, size; |
2c20b27e | 656 | hwaddr slot_size; |
67548f09 DH |
657 | |
658 | size = kvm_align_section(section, &start_addr); | |
023ae9a8 | 659 | while (size) { |
023ae9a8 IM |
660 | slot_size = MIN(kvm_max_slot_size, size); |
661 | mem = kvm_lookup_matching_slot(kml, start_addr, slot_size); | |
67548f09 | 662 | if (!mem) { |
e377e87c | 663 | /* We don't have a slot if we want to trap every access. */ |
e65e5f50 | 664 | return; |
151f7749 | 665 | } |
e65e5f50 | 666 | if (kvm_slot_get_dirty_log(s, mem)) { |
2c20b27e | 667 | kvm_slot_sync_dirty_pages(mem); |
151f7749 | 668 | } |
023ae9a8 IM |
669 | start_addr += slot_size; |
670 | size -= slot_size; | |
5832d1f2 | 671 | } |
5832d1f2 AL |
672 | } |
673 | ||
ff4aa114 PX |
674 | /* Alignment requirement for KVM_CLEAR_DIRTY_LOG - 64 pages */ |
675 | #define KVM_CLEAR_LOG_SHIFT 6 | |
676 | #define KVM_CLEAR_LOG_ALIGN (qemu_real_host_page_size << KVM_CLEAR_LOG_SHIFT) | |
677 | #define KVM_CLEAR_LOG_MASK (-KVM_CLEAR_LOG_ALIGN) | |
678 | ||
4222147d PB |
679 | static int kvm_log_clear_one_slot(KVMSlot *mem, int as_id, uint64_t start, |
680 | uint64_t size) | |
ff4aa114 PX |
681 | { |
682 | KVMState *s = kvm_state; | |
4222147d | 683 | uint64_t end, bmap_start, start_delta, bmap_npages; |
ff4aa114 | 684 | struct kvm_clear_dirty_log d; |
ff4aa114 | 685 | unsigned long *bmap_clear = NULL, psize = qemu_real_host_page_size; |
4222147d | 686 | int ret; |
ff4aa114 PX |
687 | |
688 | /* | |
689 | * We need to extend either the start or the size or both to | |
690 | * satisfy the KVM interface requirement. Firstly, do the start | |
691 | * page alignment on 64 host pages | |
692 | */ | |
84516e5b PB |
693 | bmap_start = start & KVM_CLEAR_LOG_MASK; |
694 | start_delta = start - bmap_start; | |
ff4aa114 PX |
695 | bmap_start /= psize; |
696 | ||
697 | /* | |
698 | * The kernel interface has restriction on the size too, that either: | |
699 | * | |
700 | * (1) the size is 64 host pages aligned (just like the start), or | |
701 | * (2) the size fills up until the end of the KVM memslot. | |
702 | */ | |
703 | bmap_npages = DIV_ROUND_UP(size + start_delta, KVM_CLEAR_LOG_ALIGN) | |
704 | << KVM_CLEAR_LOG_SHIFT; | |
705 | end = mem->memory_size / psize; | |
706 | if (bmap_npages > end - bmap_start) { | |
707 | bmap_npages = end - bmap_start; | |
708 | } | |
709 | start_delta /= psize; | |
710 | ||
711 | /* | |
712 | * Prepare the bitmap to clear dirty bits. Here we must guarantee | |
713 | * that we won't clear any unknown dirty bits otherwise we might | |
714 | * accidentally clear some set bits which are not yet synced from | |
715 | * the kernel into QEMU's bitmap, then we'll lose track of the | |
716 | * guest modifications upon those pages (which can directly lead | |
717 | * to guest data loss or panic after migration). | |
718 | * | |
719 | * Layout of the KVMSlot.dirty_bmap: | |
720 | * | |
721 | * |<-------- bmap_npages -----------..>| | |
722 | * [1] | |
723 | * start_delta size | |
724 | * |----------------|-------------|------------------|------------| | |
725 | * ^ ^ ^ ^ | |
726 | * | | | | | |
727 | * start bmap_start (start) end | |
728 | * of memslot of memslot | |
729 | * | |
730 | * [1] bmap_npages can be aligned to either 64 pages or the end of slot | |
731 | */ | |
732 | ||
733 | assert(bmap_start % BITS_PER_LONG == 0); | |
734 | /* We should never do log_clear before log_sync */ | |
735 | assert(mem->dirty_bmap); | |
4054adbd | 736 | if (start_delta || bmap_npages - size / psize) { |
ff4aa114 PX |
737 | /* Slow path - we need to manipulate a temp bitmap */ |
738 | bmap_clear = bitmap_new(bmap_npages); | |
739 | bitmap_copy_with_src_offset(bmap_clear, mem->dirty_bmap, | |
740 | bmap_start, start_delta + size / psize); | |
741 | /* | |
742 | * We need to fill the holes at start because that was not | |
743 | * specified by the caller and we extended the bitmap only for | |
744 | * 64 pages alignment | |
745 | */ | |
746 | bitmap_clear(bmap_clear, 0, start_delta); | |
747 | d.dirty_bitmap = bmap_clear; | |
748 | } else { | |
4054adbd ZY |
749 | /* |
750 | * Fast path - both start and size align well with BITS_PER_LONG | |
751 | * (or the end of memory slot) | |
752 | */ | |
ff4aa114 PX |
753 | d.dirty_bitmap = mem->dirty_bmap + BIT_WORD(bmap_start); |
754 | } | |
755 | ||
756 | d.first_page = bmap_start; | |
757 | /* It should never overflow. If it happens, say something */ | |
758 | assert(bmap_npages <= UINT32_MAX); | |
759 | d.num_pages = bmap_npages; | |
4222147d | 760 | d.slot = mem->slot | (as_id << 16); |
ff4aa114 | 761 | |
38e0b790 TH |
762 | ret = kvm_vm_ioctl(s, KVM_CLEAR_DIRTY_LOG, &d); |
763 | if (ret < 0 && ret != -ENOENT) { | |
ff4aa114 PX |
764 | error_report("%s: KVM_CLEAR_DIRTY_LOG failed, slot=%d, " |
765 | "start=0x%"PRIx64", size=0x%"PRIx32", errno=%d", | |
766 | __func__, d.slot, (uint64_t)d.first_page, | |
767 | (uint32_t)d.num_pages, ret); | |
768 | } else { | |
769 | ret = 0; | |
770 | trace_kvm_clear_dirty_log(d.slot, d.first_page, d.num_pages); | |
771 | } | |
772 | ||
773 | /* | |
774 | * After we have updated the remote dirty bitmap, we update the | |
775 | * cached bitmap as well for the memslot, then if another user | |
776 | * clears the same region we know we shouldn't clear it again on | |
777 | * the remote otherwise it's data loss as well. | |
778 | */ | |
779 | bitmap_clear(mem->dirty_bmap, bmap_start + start_delta, | |
780 | size / psize); | |
781 | /* This handles the NULL case well */ | |
782 | g_free(bmap_clear); | |
4222147d PB |
783 | return ret; |
784 | } | |
785 | ||
786 | ||
787 | /** | |
788 | * kvm_physical_log_clear - Clear the kernel's dirty bitmap for range | |
789 | * | |
790 | * NOTE: this will be a no-op if we haven't enabled manual dirty log | |
791 | * protection in the host kernel because in that case this operation | |
792 | * will be done within log_sync(). | |
793 | * | |
794 | * @kml: the kvm memory listener | |
795 | * @section: the memory range to clear dirty bitmap | |
796 | */ | |
797 | static int kvm_physical_log_clear(KVMMemoryListener *kml, | |
798 | MemoryRegionSection *section) | |
799 | { | |
800 | KVMState *s = kvm_state; | |
84516e5b PB |
801 | uint64_t start, size, offset, count; |
802 | KVMSlot *mem; | |
87287ac0 | 803 | int ret = 0, i; |
4222147d PB |
804 | |
805 | if (!s->manual_dirty_log_protect) { | |
806 | /* No need to do explicit clear */ | |
87287ac0 | 807 | return ret; |
4222147d PB |
808 | } |
809 | ||
810 | start = section->offset_within_address_space; | |
811 | size = int128_get64(section->size); | |
812 | ||
813 | if (!size) { | |
814 | /* Nothing more we can do... */ | |
87287ac0 | 815 | return ret; |
4222147d PB |
816 | } |
817 | ||
a2f77862 | 818 | kvm_slots_lock(); |
4222147d | 819 | |
4222147d PB |
820 | for (i = 0; i < s->nr_slots; i++) { |
821 | mem = &kml->slots[i]; | |
84516e5b PB |
822 | /* Discard slots that are empty or do not overlap the section */ |
823 | if (!mem->memory_size || | |
824 | mem->start_addr > start + size - 1 || | |
825 | start > mem->start_addr + mem->memory_size - 1) { | |
826 | continue; | |
827 | } | |
828 | ||
829 | if (start >= mem->start_addr) { | |
830 | /* The slot starts before section or is aligned to it. */ | |
831 | offset = start - mem->start_addr; | |
832 | count = MIN(mem->memory_size - offset, size); | |
833 | } else { | |
834 | /* The slot starts after section. */ | |
835 | offset = 0; | |
836 | count = MIN(mem->memory_size, size - (mem->start_addr - start)); | |
837 | } | |
838 | ret = kvm_log_clear_one_slot(mem, kml->as_id, offset, count); | |
839 | if (ret < 0) { | |
4222147d PB |
840 | break; |
841 | } | |
842 | } | |
843 | ||
a2f77862 | 844 | kvm_slots_unlock(); |
ff4aa114 PX |
845 | |
846 | return ret; | |
847 | } | |
848 | ||
95d2994a AK |
849 | static void kvm_coalesce_mmio_region(MemoryListener *listener, |
850 | MemoryRegionSection *secion, | |
a8170e5e | 851 | hwaddr start, hwaddr size) |
f65ed4c1 | 852 | { |
f65ed4c1 AL |
853 | KVMState *s = kvm_state; |
854 | ||
855 | if (s->coalesced_mmio) { | |
856 | struct kvm_coalesced_mmio_zone zone; | |
857 | ||
858 | zone.addr = start; | |
859 | zone.size = size; | |
7e680753 | 860 | zone.pad = 0; |
f65ed4c1 | 861 | |
95d2994a | 862 | (void)kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone); |
f65ed4c1 | 863 | } |
f65ed4c1 AL |
864 | } |
865 | ||
95d2994a AK |
866 | static void kvm_uncoalesce_mmio_region(MemoryListener *listener, |
867 | MemoryRegionSection *secion, | |
a8170e5e | 868 | hwaddr start, hwaddr size) |
f65ed4c1 | 869 | { |
f65ed4c1 AL |
870 | KVMState *s = kvm_state; |
871 | ||
872 | if (s->coalesced_mmio) { | |
873 | struct kvm_coalesced_mmio_zone zone; | |
874 | ||
875 | zone.addr = start; | |
876 | zone.size = size; | |
7e680753 | 877 | zone.pad = 0; |
f65ed4c1 | 878 | |
95d2994a | 879 | (void)kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone); |
f65ed4c1 | 880 | } |
f65ed4c1 AL |
881 | } |
882 | ||
e6d34aee PH |
883 | static void kvm_coalesce_pio_add(MemoryListener *listener, |
884 | MemoryRegionSection *section, | |
885 | hwaddr start, hwaddr size) | |
886 | { | |
887 | KVMState *s = kvm_state; | |
888 | ||
889 | if (s->coalesced_pio) { | |
890 | struct kvm_coalesced_mmio_zone zone; | |
891 | ||
892 | zone.addr = start; | |
893 | zone.size = size; | |
894 | zone.pio = 1; | |
895 | ||
896 | (void)kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone); | |
897 | } | |
898 | } | |
899 | ||
900 | static void kvm_coalesce_pio_del(MemoryListener *listener, | |
901 | MemoryRegionSection *section, | |
902 | hwaddr start, hwaddr size) | |
903 | { | |
904 | KVMState *s = kvm_state; | |
905 | ||
906 | if (s->coalesced_pio) { | |
907 | struct kvm_coalesced_mmio_zone zone; | |
908 | ||
909 | zone.addr = start; | |
910 | zone.size = size; | |
911 | zone.pio = 1; | |
912 | ||
913 | (void)kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone); | |
914 | } | |
915 | } | |
916 | ||
917 | static MemoryListener kvm_coalesced_pio_listener = { | |
918 | .coalesced_io_add = kvm_coalesce_pio_add, | |
919 | .coalesced_io_del = kvm_coalesce_pio_del, | |
920 | }; | |
921 | ||
ad7b8b33 AL |
922 | int kvm_check_extension(KVMState *s, unsigned int extension) |
923 | { | |
924 | int ret; | |
925 | ||
926 | ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension); | |
927 | if (ret < 0) { | |
928 | ret = 0; | |
929 | } | |
930 | ||
931 | return ret; | |
932 | } | |
933 | ||
7d0a07fa AG |
934 | int kvm_vm_check_extension(KVMState *s, unsigned int extension) |
935 | { | |
936 | int ret; | |
937 | ||
938 | ret = kvm_vm_ioctl(s, KVM_CHECK_EXTENSION, extension); | |
939 | if (ret < 0) { | |
940 | /* VM wide version not implemented, use global one instead */ | |
941 | ret = kvm_check_extension(s, extension); | |
942 | } | |
943 | ||
944 | return ret; | |
945 | } | |
946 | ||
6b552b9b DG |
947 | typedef struct HWPoisonPage { |
948 | ram_addr_t ram_addr; | |
949 | QLIST_ENTRY(HWPoisonPage) list; | |
950 | } HWPoisonPage; | |
951 | ||
952 | static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list = | |
953 | QLIST_HEAD_INITIALIZER(hwpoison_page_list); | |
954 | ||
955 | static void kvm_unpoison_all(void *param) | |
956 | { | |
957 | HWPoisonPage *page, *next_page; | |
958 | ||
959 | QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) { | |
960 | QLIST_REMOVE(page, list); | |
961 | qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE); | |
962 | g_free(page); | |
963 | } | |
964 | } | |
965 | ||
966 | void kvm_hwpoison_page_add(ram_addr_t ram_addr) | |
967 | { | |
968 | HWPoisonPage *page; | |
969 | ||
970 | QLIST_FOREACH(page, &hwpoison_page_list, list) { | |
971 | if (page->ram_addr == ram_addr) { | |
972 | return; | |
973 | } | |
974 | } | |
975 | page = g_new(HWPoisonPage, 1); | |
976 | page->ram_addr = ram_addr; | |
977 | QLIST_INSERT_HEAD(&hwpoison_page_list, page, list); | |
978 | } | |
979 | ||
b680c5ba GK |
980 | static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size) |
981 | { | |
982 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) | |
983 | /* The kernel expects ioeventfd values in HOST_WORDS_BIGENDIAN | |
984 | * endianness, but the memory core hands them in target endianness. | |
985 | * For example, PPC is always treated as big-endian even if running | |
986 | * on KVM and on PPC64LE. Correct here. | |
987 | */ | |
988 | switch (size) { | |
989 | case 2: | |
990 | val = bswap16(val); | |
991 | break; | |
992 | case 4: | |
993 | val = bswap32(val); | |
994 | break; | |
995 | } | |
996 | #endif | |
997 | return val; | |
998 | } | |
999 | ||
584f2be7 | 1000 | static int kvm_set_ioeventfd_mmio(int fd, hwaddr addr, uint32_t val, |
41cb62c2 | 1001 | bool assign, uint32_t size, bool datamatch) |
500ffd4a MT |
1002 | { |
1003 | int ret; | |
03a96b83 TH |
1004 | struct kvm_ioeventfd iofd = { |
1005 | .datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0, | |
1006 | .addr = addr, | |
1007 | .len = size, | |
1008 | .flags = 0, | |
1009 | .fd = fd, | |
1010 | }; | |
500ffd4a | 1011 | |
876d16cd DDAG |
1012 | trace_kvm_set_ioeventfd_mmio(fd, (uint64_t)addr, val, assign, size, |
1013 | datamatch); | |
500ffd4a MT |
1014 | if (!kvm_enabled()) { |
1015 | return -ENOSYS; | |
1016 | } | |
1017 | ||
41cb62c2 MT |
1018 | if (datamatch) { |
1019 | iofd.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH; | |
1020 | } | |
500ffd4a MT |
1021 | if (!assign) { |
1022 | iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN; | |
1023 | } | |
1024 | ||
1025 | ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd); | |
1026 | ||
1027 | if (ret < 0) { | |
1028 | return -errno; | |
1029 | } | |
1030 | ||
1031 | return 0; | |
1032 | } | |
1033 | ||
44c3f8f7 | 1034 | static int kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint16_t val, |
41cb62c2 | 1035 | bool assign, uint32_t size, bool datamatch) |
500ffd4a MT |
1036 | { |
1037 | struct kvm_ioeventfd kick = { | |
b680c5ba | 1038 | .datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0, |
500ffd4a | 1039 | .addr = addr, |
41cb62c2 | 1040 | .flags = KVM_IOEVENTFD_FLAG_PIO, |
44c3f8f7 | 1041 | .len = size, |
500ffd4a MT |
1042 | .fd = fd, |
1043 | }; | |
1044 | int r; | |
876d16cd | 1045 | trace_kvm_set_ioeventfd_pio(fd, addr, val, assign, size, datamatch); |
500ffd4a MT |
1046 | if (!kvm_enabled()) { |
1047 | return -ENOSYS; | |
1048 | } | |
41cb62c2 MT |
1049 | if (datamatch) { |
1050 | kick.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH; | |
1051 | } | |
500ffd4a MT |
1052 | if (!assign) { |
1053 | kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN; | |
1054 | } | |
1055 | r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick); | |
1056 | if (r < 0) { | |
1057 | return r; | |
1058 | } | |
1059 | return 0; | |
1060 | } | |
1061 | ||
1062 | ||
d2f2b8a7 SH |
1063 | static int kvm_check_many_ioeventfds(void) |
1064 | { | |
d0dcac83 SH |
1065 | /* Userspace can use ioeventfd for io notification. This requires a host |
1066 | * that supports eventfd(2) and an I/O thread; since eventfd does not | |
1067 | * support SIGIO it cannot interrupt the vcpu. | |
1068 | * | |
1069 | * Older kernels have a 6 device limit on the KVM io bus. Find out so we | |
d2f2b8a7 SH |
1070 | * can avoid creating too many ioeventfds. |
1071 | */ | |
12d4536f | 1072 | #if defined(CONFIG_EVENTFD) |
d2f2b8a7 SH |
1073 | int ioeventfds[7]; |
1074 | int i, ret = 0; | |
1075 | for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) { | |
1076 | ioeventfds[i] = eventfd(0, EFD_CLOEXEC); | |
1077 | if (ioeventfds[i] < 0) { | |
1078 | break; | |
1079 | } | |
41cb62c2 | 1080 | ret = kvm_set_ioeventfd_pio(ioeventfds[i], 0, i, true, 2, true); |
d2f2b8a7 SH |
1081 | if (ret < 0) { |
1082 | close(ioeventfds[i]); | |
1083 | break; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | /* Decide whether many devices are supported or not */ | |
1088 | ret = i == ARRAY_SIZE(ioeventfds); | |
1089 | ||
1090 | while (i-- > 0) { | |
41cb62c2 | 1091 | kvm_set_ioeventfd_pio(ioeventfds[i], 0, i, false, 2, true); |
d2f2b8a7 SH |
1092 | close(ioeventfds[i]); |
1093 | } | |
1094 | return ret; | |
1095 | #else | |
1096 | return 0; | |
1097 | #endif | |
1098 | } | |
1099 | ||
94a8d39a JK |
1100 | static const KVMCapabilityInfo * |
1101 | kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list) | |
1102 | { | |
1103 | while (list->name) { | |
1104 | if (!kvm_check_extension(s, list->value)) { | |
1105 | return list; | |
1106 | } | |
1107 | list++; | |
1108 | } | |
1109 | return NULL; | |
1110 | } | |
1111 | ||
023ae9a8 IM |
1112 | void kvm_set_max_memslot_size(hwaddr max_slot_size) |
1113 | { | |
1114 | g_assert( | |
1115 | ROUND_UP(max_slot_size, qemu_real_host_page_size) == max_slot_size | |
1116 | ); | |
1117 | kvm_max_slot_size = max_slot_size; | |
1118 | } | |
1119 | ||
7bbda04c PB |
1120 | static void kvm_set_phys_mem(KVMMemoryListener *kml, |
1121 | MemoryRegionSection *section, bool add) | |
46dbef6a | 1122 | { |
f357f564 | 1123 | KVMSlot *mem; |
46dbef6a | 1124 | int err; |
a01672d3 | 1125 | MemoryRegion *mr = section->mr; |
235e8982 | 1126 | bool writeable = !mr->readonly && !mr->rom_device; |
2c20b27e PX |
1127 | hwaddr start_addr, size, slot_size, mr_offset; |
1128 | ram_addr_t ram_start_offset; | |
5ea69c2e | 1129 | void *ram; |
46dbef6a | 1130 | |
a01672d3 | 1131 | if (!memory_region_is_ram(mr)) { |
235e8982 JJ |
1132 | if (writeable || !kvm_readonly_mem_allowed) { |
1133 | return; | |
1134 | } else if (!mr->romd_mode) { | |
1135 | /* If the memory device is not in romd_mode, then we actually want | |
1136 | * to remove the kvm memory slot so all accesses will trap. */ | |
1137 | add = false; | |
1138 | } | |
9f213ed9 AK |
1139 | } |
1140 | ||
5ea69c2e DH |
1141 | size = kvm_align_section(section, &start_addr); |
1142 | if (!size) { | |
1143 | return; | |
1144 | } | |
1145 | ||
2c20b27e PX |
1146 | /* The offset of the kvmslot within the memory region */ |
1147 | mr_offset = section->offset_within_region + start_addr - | |
1148 | section->offset_within_address_space; | |
1149 | ||
1150 | /* use aligned delta to align the ram address and offset */ | |
1151 | ram = memory_region_get_ram_ptr(mr) + mr_offset; | |
1152 | ram_start_offset = memory_region_get_ram_addr(mr) + mr_offset; | |
a01672d3 | 1153 | |
a2f77862 | 1154 | kvm_slots_lock(); |
36adac49 | 1155 | |
f357f564 | 1156 | if (!add) { |
023ae9a8 IM |
1157 | do { |
1158 | slot_size = MIN(kvm_max_slot_size, size); | |
1159 | mem = kvm_lookup_matching_slot(kml, start_addr, slot_size); | |
1160 | if (!mem) { | |
1161 | goto out; | |
1162 | } | |
1163 | if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { | |
1164 | kvm_physical_sync_dirty_bitmap(kml, section); | |
1165 | } | |
3fbffb62 | 1166 | |
023ae9a8 IM |
1167 | /* unregister the slot */ |
1168 | g_free(mem->dirty_bmap); | |
1169 | mem->dirty_bmap = NULL; | |
1170 | mem->memory_size = 0; | |
1171 | mem->flags = 0; | |
1172 | err = kvm_set_user_memory_region(kml, mem, false); | |
1173 | if (err) { | |
1174 | fprintf(stderr, "%s: error unregistering slot: %s\n", | |
1175 | __func__, strerror(-err)); | |
1176 | abort(); | |
1177 | } | |
1178 | start_addr += slot_size; | |
1179 | size -= slot_size; | |
1180 | } while (size); | |
36adac49 | 1181 | goto out; |
46dbef6a MT |
1182 | } |
1183 | ||
f357f564 | 1184 | /* register the new slot */ |
023ae9a8 IM |
1185 | do { |
1186 | slot_size = MIN(kvm_max_slot_size, size); | |
1187 | mem = kvm_alloc_slot(kml); | |
e65e5f50 | 1188 | mem->as_id = kml->as_id; |
023ae9a8 IM |
1189 | mem->memory_size = slot_size; |
1190 | mem->start_addr = start_addr; | |
2c20b27e | 1191 | mem->ram_start_offset = ram_start_offset; |
023ae9a8 IM |
1192 | mem->ram = ram; |
1193 | mem->flags = kvm_mem_flags(mr); | |
ea776d15 | 1194 | kvm_slot_init_dirty_bitmap(mem); |
023ae9a8 IM |
1195 | err = kvm_set_user_memory_region(kml, mem, true); |
1196 | if (err) { | |
1197 | fprintf(stderr, "%s: error registering slot: %s\n", __func__, | |
1198 | strerror(-err)); | |
1199 | abort(); | |
1200 | } | |
1201 | start_addr += slot_size; | |
2c20b27e | 1202 | ram_start_offset += slot_size; |
023ae9a8 IM |
1203 | ram += slot_size; |
1204 | size -= slot_size; | |
1205 | } while (size); | |
36adac49 PX |
1206 | |
1207 | out: | |
a2f77862 | 1208 | kvm_slots_unlock(); |
46dbef6a MT |
1209 | } |
1210 | ||
a01672d3 AK |
1211 | static void kvm_region_add(MemoryListener *listener, |
1212 | MemoryRegionSection *section) | |
1213 | { | |
7bbda04c PB |
1214 | KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); |
1215 | ||
dfde4e6e | 1216 | memory_region_ref(section->mr); |
7bbda04c | 1217 | kvm_set_phys_mem(kml, section, true); |
a01672d3 AK |
1218 | } |
1219 | ||
1220 | static void kvm_region_del(MemoryListener *listener, | |
1221 | MemoryRegionSection *section) | |
1222 | { | |
7bbda04c PB |
1223 | KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); |
1224 | ||
1225 | kvm_set_phys_mem(kml, section, false); | |
dfde4e6e | 1226 | memory_region_unref(section->mr); |
a01672d3 AK |
1227 | } |
1228 | ||
1229 | static void kvm_log_sync(MemoryListener *listener, | |
1230 | MemoryRegionSection *section) | |
7b8f3b78 | 1231 | { |
7bbda04c | 1232 | KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); |
a01672d3 | 1233 | |
a2f77862 | 1234 | kvm_slots_lock(); |
e65e5f50 | 1235 | kvm_physical_sync_dirty_bitmap(kml, section); |
a2f77862 | 1236 | kvm_slots_unlock(); |
7b8f3b78 MT |
1237 | } |
1238 | ||
ff4aa114 PX |
1239 | static void kvm_log_clear(MemoryListener *listener, |
1240 | MemoryRegionSection *section) | |
1241 | { | |
1242 | KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); | |
1243 | int r; | |
1244 | ||
1245 | r = kvm_physical_log_clear(kml, section); | |
1246 | if (r < 0) { | |
1247 | error_report_once("%s: kvm log clear failed: mr=%s " | |
1248 | "offset=%"HWADDR_PRIx" size=%"PRIx64, __func__, | |
1249 | section->mr->name, section->offset_within_region, | |
1250 | int128_get64(section->size)); | |
1251 | abort(); | |
1252 | } | |
1253 | } | |
1254 | ||
d22b096e AK |
1255 | static void kvm_mem_ioeventfd_add(MemoryListener *listener, |
1256 | MemoryRegionSection *section, | |
1257 | bool match_data, uint64_t data, | |
1258 | EventNotifier *e) | |
1259 | { | |
1260 | int fd = event_notifier_get_fd(e); | |
80a1ea37 AK |
1261 | int r; |
1262 | ||
4b8f1c88 | 1263 | r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space, |
052e87b0 PB |
1264 | data, true, int128_get64(section->size), |
1265 | match_data); | |
80a1ea37 | 1266 | if (r < 0) { |
e346bcbf YK |
1267 | fprintf(stderr, "%s: error adding ioeventfd: %s (%d)\n", |
1268 | __func__, strerror(-r), -r); | |
80a1ea37 AK |
1269 | abort(); |
1270 | } | |
1271 | } | |
1272 | ||
d22b096e AK |
1273 | static void kvm_mem_ioeventfd_del(MemoryListener *listener, |
1274 | MemoryRegionSection *section, | |
1275 | bool match_data, uint64_t data, | |
1276 | EventNotifier *e) | |
80a1ea37 | 1277 | { |
d22b096e | 1278 | int fd = event_notifier_get_fd(e); |
80a1ea37 AK |
1279 | int r; |
1280 | ||
4b8f1c88 | 1281 | r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space, |
052e87b0 PB |
1282 | data, false, int128_get64(section->size), |
1283 | match_data); | |
80a1ea37 | 1284 | if (r < 0) { |
e346bcbf YK |
1285 | fprintf(stderr, "%s: error deleting ioeventfd: %s (%d)\n", |
1286 | __func__, strerror(-r), -r); | |
80a1ea37 AK |
1287 | abort(); |
1288 | } | |
1289 | } | |
1290 | ||
d22b096e AK |
1291 | static void kvm_io_ioeventfd_add(MemoryListener *listener, |
1292 | MemoryRegionSection *section, | |
1293 | bool match_data, uint64_t data, | |
1294 | EventNotifier *e) | |
80a1ea37 | 1295 | { |
d22b096e | 1296 | int fd = event_notifier_get_fd(e); |
80a1ea37 AK |
1297 | int r; |
1298 | ||
44c3f8f7 | 1299 | r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space, |
052e87b0 PB |
1300 | data, true, int128_get64(section->size), |
1301 | match_data); | |
80a1ea37 | 1302 | if (r < 0) { |
e346bcbf YK |
1303 | fprintf(stderr, "%s: error adding ioeventfd: %s (%d)\n", |
1304 | __func__, strerror(-r), -r); | |
80a1ea37 AK |
1305 | abort(); |
1306 | } | |
1307 | } | |
1308 | ||
d22b096e AK |
1309 | static void kvm_io_ioeventfd_del(MemoryListener *listener, |
1310 | MemoryRegionSection *section, | |
1311 | bool match_data, uint64_t data, | |
1312 | EventNotifier *e) | |
80a1ea37 AK |
1313 | |
1314 | { | |
d22b096e | 1315 | int fd = event_notifier_get_fd(e); |
80a1ea37 AK |
1316 | int r; |
1317 | ||
44c3f8f7 | 1318 | r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space, |
052e87b0 PB |
1319 | data, false, int128_get64(section->size), |
1320 | match_data); | |
80a1ea37 | 1321 | if (r < 0) { |
e346bcbf YK |
1322 | fprintf(stderr, "%s: error deleting ioeventfd: %s (%d)\n", |
1323 | __func__, strerror(-r), -r); | |
80a1ea37 AK |
1324 | abort(); |
1325 | } | |
1326 | } | |
1327 | ||
38bfe691 PB |
1328 | void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml, |
1329 | AddressSpace *as, int as_id) | |
7bbda04c PB |
1330 | { |
1331 | int i; | |
1332 | ||
1333 | kml->slots = g_malloc0(s->nr_slots * sizeof(KVMSlot)); | |
38bfe691 | 1334 | kml->as_id = as_id; |
7bbda04c PB |
1335 | |
1336 | for (i = 0; i < s->nr_slots; i++) { | |
1337 | kml->slots[i].slot = i; | |
1338 | } | |
1339 | ||
1340 | kml->listener.region_add = kvm_region_add; | |
1341 | kml->listener.region_del = kvm_region_del; | |
1342 | kml->listener.log_start = kvm_log_start; | |
1343 | kml->listener.log_stop = kvm_log_stop; | |
1344 | kml->listener.log_sync = kvm_log_sync; | |
ff4aa114 | 1345 | kml->listener.log_clear = kvm_log_clear; |
7bbda04c PB |
1346 | kml->listener.priority = 10; |
1347 | ||
1348 | memory_listener_register(&kml->listener, as); | |
8072aae3 AK |
1349 | |
1350 | for (i = 0; i < s->nr_as; ++i) { | |
1351 | if (!s->as[i].as) { | |
1352 | s->as[i].as = as; | |
1353 | s->as[i].ml = kml; | |
1354 | break; | |
1355 | } | |
1356 | } | |
7bbda04c | 1357 | } |
d22b096e AK |
1358 | |
1359 | static MemoryListener kvm_io_listener = { | |
d22b096e AK |
1360 | .eventfd_add = kvm_io_ioeventfd_add, |
1361 | .eventfd_del = kvm_io_ioeventfd_del, | |
72e22d2f | 1362 | .priority = 10, |
7b8f3b78 MT |
1363 | }; |
1364 | ||
3889c3fa | 1365 | int kvm_set_irq(KVMState *s, int irq, int level) |
84b058d7 JK |
1366 | { |
1367 | struct kvm_irq_level event; | |
1368 | int ret; | |
1369 | ||
7ae26bd4 | 1370 | assert(kvm_async_interrupts_enabled()); |
84b058d7 JK |
1371 | |
1372 | event.level = level; | |
1373 | event.irq = irq; | |
e333cd69 | 1374 | ret = kvm_vm_ioctl(s, s->irq_set_ioctl, &event); |
84b058d7 | 1375 | if (ret < 0) { |
3889c3fa | 1376 | perror("kvm_set_irq"); |
84b058d7 JK |
1377 | abort(); |
1378 | } | |
1379 | ||
e333cd69 | 1380 | return (s->irq_set_ioctl == KVM_IRQ_LINE) ? 1 : event.status; |
84b058d7 JK |
1381 | } |
1382 | ||
1383 | #ifdef KVM_CAP_IRQ_ROUTING | |
d3d3bef0 JK |
1384 | typedef struct KVMMSIRoute { |
1385 | struct kvm_irq_routing_entry kroute; | |
1386 | QTAILQ_ENTRY(KVMMSIRoute) entry; | |
1387 | } KVMMSIRoute; | |
1388 | ||
84b058d7 JK |
1389 | static void set_gsi(KVMState *s, unsigned int gsi) |
1390 | { | |
8269fb70 | 1391 | set_bit(gsi, s->used_gsi_bitmap); |
84b058d7 JK |
1392 | } |
1393 | ||
04fa27f5 JK |
1394 | static void clear_gsi(KVMState *s, unsigned int gsi) |
1395 | { | |
8269fb70 | 1396 | clear_bit(gsi, s->used_gsi_bitmap); |
04fa27f5 JK |
1397 | } |
1398 | ||
7b774593 | 1399 | void kvm_init_irq_routing(KVMState *s) |
84b058d7 | 1400 | { |
04fa27f5 | 1401 | int gsi_count, i; |
84b058d7 | 1402 | |
00008418 | 1403 | gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1; |
84b058d7 | 1404 | if (gsi_count > 0) { |
84b058d7 | 1405 | /* Round up so we can search ints using ffs */ |
8269fb70 | 1406 | s->used_gsi_bitmap = bitmap_new(gsi_count); |
4e2e4e63 | 1407 | s->gsi_count = gsi_count; |
84b058d7 JK |
1408 | } |
1409 | ||
1410 | s->irq_routes = g_malloc0(sizeof(*s->irq_routes)); | |
1411 | s->nr_allocated_irq_routes = 0; | |
1412 | ||
50bf31b9 | 1413 | if (!kvm_direct_msi_allowed) { |
4a3adebb JK |
1414 | for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) { |
1415 | QTAILQ_INIT(&s->msi_hashtab[i]); | |
1416 | } | |
04fa27f5 JK |
1417 | } |
1418 | ||
84b058d7 JK |
1419 | kvm_arch_init_irq_routing(s); |
1420 | } | |
1421 | ||
cb925cf9 | 1422 | void kvm_irqchip_commit_routes(KVMState *s) |
e7b20308 JK |
1423 | { |
1424 | int ret; | |
1425 | ||
7005f7f8 PX |
1426 | if (kvm_gsi_direct_mapping()) { |
1427 | return; | |
1428 | } | |
1429 | ||
1430 | if (!kvm_gsi_routing_enabled()) { | |
1431 | return; | |
1432 | } | |
1433 | ||
e7b20308 | 1434 | s->irq_routes->flags = 0; |
54a6c11b | 1435 | trace_kvm_irqchip_commit_routes(); |
e7b20308 JK |
1436 | ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes); |
1437 | assert(ret == 0); | |
1438 | } | |
1439 | ||
84b058d7 JK |
1440 | static void kvm_add_routing_entry(KVMState *s, |
1441 | struct kvm_irq_routing_entry *entry) | |
1442 | { | |
1443 | struct kvm_irq_routing_entry *new; | |
1444 | int n, size; | |
1445 | ||
1446 | if (s->irq_routes->nr == s->nr_allocated_irq_routes) { | |
1447 | n = s->nr_allocated_irq_routes * 2; | |
1448 | if (n < 64) { | |
1449 | n = 64; | |
1450 | } | |
1451 | size = sizeof(struct kvm_irq_routing); | |
1452 | size += n * sizeof(*new); | |
1453 | s->irq_routes = g_realloc(s->irq_routes, size); | |
1454 | s->nr_allocated_irq_routes = n; | |
1455 | } | |
1456 | n = s->irq_routes->nr++; | |
1457 | new = &s->irq_routes->entries[n]; | |
0fbc2074 MT |
1458 | |
1459 | *new = *entry; | |
84b058d7 JK |
1460 | |
1461 | set_gsi(s, entry->gsi); | |
1462 | } | |
1463 | ||
cc57407e JK |
1464 | static int kvm_update_routing_entry(KVMState *s, |
1465 | struct kvm_irq_routing_entry *new_entry) | |
1466 | { | |
1467 | struct kvm_irq_routing_entry *entry; | |
1468 | int n; | |
1469 | ||
1470 | for (n = 0; n < s->irq_routes->nr; n++) { | |
1471 | entry = &s->irq_routes->entries[n]; | |
1472 | if (entry->gsi != new_entry->gsi) { | |
1473 | continue; | |
1474 | } | |
1475 | ||
40509f7f MT |
1476 | if(!memcmp(entry, new_entry, sizeof *entry)) { |
1477 | return 0; | |
1478 | } | |
1479 | ||
0fbc2074 | 1480 | *entry = *new_entry; |
cc57407e | 1481 | |
cc57407e JK |
1482 | return 0; |
1483 | } | |
1484 | ||
1485 | return -ESRCH; | |
1486 | } | |
1487 | ||
1df186df | 1488 | void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin) |
84b058d7 | 1489 | { |
0fbc2074 | 1490 | struct kvm_irq_routing_entry e = {}; |
84b058d7 | 1491 | |
4e2e4e63 JK |
1492 | assert(pin < s->gsi_count); |
1493 | ||
84b058d7 JK |
1494 | e.gsi = irq; |
1495 | e.type = KVM_IRQ_ROUTING_IRQCHIP; | |
1496 | e.flags = 0; | |
1497 | e.u.irqchip.irqchip = irqchip; | |
1498 | e.u.irqchip.pin = pin; | |
1499 | kvm_add_routing_entry(s, &e); | |
1500 | } | |
1501 | ||
1e2aa8be | 1502 | void kvm_irqchip_release_virq(KVMState *s, int virq) |
04fa27f5 JK |
1503 | { |
1504 | struct kvm_irq_routing_entry *e; | |
1505 | int i; | |
1506 | ||
76fe21de AK |
1507 | if (kvm_gsi_direct_mapping()) { |
1508 | return; | |
1509 | } | |
1510 | ||
04fa27f5 JK |
1511 | for (i = 0; i < s->irq_routes->nr; i++) { |
1512 | e = &s->irq_routes->entries[i]; | |
1513 | if (e->gsi == virq) { | |
1514 | s->irq_routes->nr--; | |
1515 | *e = s->irq_routes->entries[s->irq_routes->nr]; | |
1516 | } | |
1517 | } | |
1518 | clear_gsi(s, virq); | |
38d87493 | 1519 | kvm_arch_release_virq_post(virq); |
9ba35d0b | 1520 | trace_kvm_irqchip_release_virq(virq); |
04fa27f5 JK |
1521 | } |
1522 | ||
3607715a DG |
1523 | void kvm_irqchip_add_change_notifier(Notifier *n) |
1524 | { | |
1525 | notifier_list_add(&kvm_irqchip_change_notifiers, n); | |
1526 | } | |
1527 | ||
1528 | void kvm_irqchip_remove_change_notifier(Notifier *n) | |
1529 | { | |
1530 | notifier_remove(n); | |
1531 | } | |
1532 | ||
1533 | void kvm_irqchip_change_notify(void) | |
1534 | { | |
1535 | notifier_list_notify(&kvm_irqchip_change_notifiers, NULL); | |
1536 | } | |
1537 | ||
04fa27f5 JK |
1538 | static unsigned int kvm_hash_msi(uint32_t data) |
1539 | { | |
1540 | /* This is optimized for IA32 MSI layout. However, no other arch shall | |
1541 | * repeat the mistake of not providing a direct MSI injection API. */ | |
1542 | return data & 0xff; | |
1543 | } | |
1544 | ||
1545 | static void kvm_flush_dynamic_msi_routes(KVMState *s) | |
1546 | { | |
1547 | KVMMSIRoute *route, *next; | |
1548 | unsigned int hash; | |
1549 | ||
1550 | for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) { | |
1551 | QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) { | |
1552 | kvm_irqchip_release_virq(s, route->kroute.gsi); | |
1553 | QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry); | |
1554 | g_free(route); | |
1555 | } | |
1556 | } | |
1557 | } | |
1558 | ||
1559 | static int kvm_irqchip_get_virq(KVMState *s) | |
1560 | { | |
8269fb70 | 1561 | int next_virq; |
04fa27f5 | 1562 | |
bdf02631 WM |
1563 | /* |
1564 | * PIC and IOAPIC share the first 16 GSI numbers, thus the available | |
1565 | * GSI numbers are more than the number of IRQ route. Allocating a GSI | |
1566 | * number can succeed even though a new route entry cannot be added. | |
1567 | * When this happens, flush dynamic MSI entries to free IRQ route entries. | |
1568 | */ | |
50bf31b9 | 1569 | if (!kvm_direct_msi_allowed && s->irq_routes->nr == s->gsi_count) { |
bdf02631 WM |
1570 | kvm_flush_dynamic_msi_routes(s); |
1571 | } | |
1572 | ||
04fa27f5 | 1573 | /* Return the lowest unused GSI in the bitmap */ |
8269fb70 WY |
1574 | next_virq = find_first_zero_bit(s->used_gsi_bitmap, s->gsi_count); |
1575 | if (next_virq >= s->gsi_count) { | |
1576 | return -ENOSPC; | |
1577 | } else { | |
1578 | return next_virq; | |
04fa27f5 | 1579 | } |
04fa27f5 JK |
1580 | } |
1581 | ||
1582 | static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg) | |
1583 | { | |
1584 | unsigned int hash = kvm_hash_msi(msg.data); | |
1585 | KVMMSIRoute *route; | |
1586 | ||
1587 | QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) { | |
1588 | if (route->kroute.u.msi.address_lo == (uint32_t)msg.address && | |
1589 | route->kroute.u.msi.address_hi == (msg.address >> 32) && | |
d07cc1f1 | 1590 | route->kroute.u.msi.data == le32_to_cpu(msg.data)) { |
04fa27f5 JK |
1591 | return route; |
1592 | } | |
1593 | } | |
1594 | return NULL; | |
1595 | } | |
1596 | ||
1597 | int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) | |
1598 | { | |
4a3adebb | 1599 | struct kvm_msi msi; |
04fa27f5 JK |
1600 | KVMMSIRoute *route; |
1601 | ||
50bf31b9 | 1602 | if (kvm_direct_msi_allowed) { |
4a3adebb JK |
1603 | msi.address_lo = (uint32_t)msg.address; |
1604 | msi.address_hi = msg.address >> 32; | |
d07cc1f1 | 1605 | msi.data = le32_to_cpu(msg.data); |
4a3adebb JK |
1606 | msi.flags = 0; |
1607 | memset(msi.pad, 0, sizeof(msi.pad)); | |
1608 | ||
1609 | return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi); | |
1610 | } | |
1611 | ||
04fa27f5 JK |
1612 | route = kvm_lookup_msi_route(s, msg); |
1613 | if (!route) { | |
e7b20308 | 1614 | int virq; |
04fa27f5 JK |
1615 | |
1616 | virq = kvm_irqchip_get_virq(s); | |
1617 | if (virq < 0) { | |
1618 | return virq; | |
1619 | } | |
1620 | ||
0fbc2074 | 1621 | route = g_malloc0(sizeof(KVMMSIRoute)); |
04fa27f5 JK |
1622 | route->kroute.gsi = virq; |
1623 | route->kroute.type = KVM_IRQ_ROUTING_MSI; | |
1624 | route->kroute.flags = 0; | |
1625 | route->kroute.u.msi.address_lo = (uint32_t)msg.address; | |
1626 | route->kroute.u.msi.address_hi = msg.address >> 32; | |
d07cc1f1 | 1627 | route->kroute.u.msi.data = le32_to_cpu(msg.data); |
04fa27f5 JK |
1628 | |
1629 | kvm_add_routing_entry(s, &route->kroute); | |
cb925cf9 | 1630 | kvm_irqchip_commit_routes(s); |
04fa27f5 JK |
1631 | |
1632 | QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route, | |
1633 | entry); | |
04fa27f5 JK |
1634 | } |
1635 | ||
1636 | assert(route->kroute.type == KVM_IRQ_ROUTING_MSI); | |
1637 | ||
3889c3fa | 1638 | return kvm_set_irq(s, route->kroute.gsi, 1); |
04fa27f5 JK |
1639 | } |
1640 | ||
d1f6af6a | 1641 | int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev) |
92b4e489 | 1642 | { |
0fbc2074 | 1643 | struct kvm_irq_routing_entry kroute = {}; |
92b4e489 | 1644 | int virq; |
d1f6af6a PX |
1645 | MSIMessage msg = {0, 0}; |
1646 | ||
88c725c7 | 1647 | if (pci_available && dev) { |
e1d4fb2d | 1648 | msg = pci_get_msi_message(dev, vector); |
d1f6af6a | 1649 | } |
92b4e489 | 1650 | |
76fe21de | 1651 | if (kvm_gsi_direct_mapping()) { |
1850b6b7 | 1652 | return kvm_arch_msi_data_to_gsi(msg.data); |
76fe21de AK |
1653 | } |
1654 | ||
f3e1bed8 | 1655 | if (!kvm_gsi_routing_enabled()) { |
92b4e489 JK |
1656 | return -ENOSYS; |
1657 | } | |
1658 | ||
1659 | virq = kvm_irqchip_get_virq(s); | |
1660 | if (virq < 0) { | |
1661 | return virq; | |
1662 | } | |
1663 | ||
1664 | kroute.gsi = virq; | |
1665 | kroute.type = KVM_IRQ_ROUTING_MSI; | |
1666 | kroute.flags = 0; | |
1667 | kroute.u.msi.address_lo = (uint32_t)msg.address; | |
1668 | kroute.u.msi.address_hi = msg.address >> 32; | |
d07cc1f1 | 1669 | kroute.u.msi.data = le32_to_cpu(msg.data); |
88c725c7 | 1670 | if (pci_available && kvm_msi_devid_required()) { |
767a554a PF |
1671 | kroute.flags = KVM_MSI_VALID_DEVID; |
1672 | kroute.u.msi.devid = pci_requester_id(dev); | |
1673 | } | |
dc9f06ca | 1674 | if (kvm_arch_fixup_msi_route(&kroute, msg.address, msg.data, dev)) { |
9e03a040 FB |
1675 | kvm_irqchip_release_virq(s, virq); |
1676 | return -EINVAL; | |
1677 | } | |
92b4e489 | 1678 | |
9ba35d0b PX |
1679 | trace_kvm_irqchip_add_msi_route(dev ? dev->name : (char *)"N/A", |
1680 | vector, virq); | |
54a6c11b | 1681 | |
92b4e489 | 1682 | kvm_add_routing_entry(s, &kroute); |
38d87493 | 1683 | kvm_arch_add_msi_route_post(&kroute, vector, dev); |
cb925cf9 | 1684 | kvm_irqchip_commit_routes(s); |
92b4e489 JK |
1685 | |
1686 | return virq; | |
1687 | } | |
1688 | ||
dc9f06ca PF |
1689 | int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg, |
1690 | PCIDevice *dev) | |
cc57407e | 1691 | { |
0fbc2074 | 1692 | struct kvm_irq_routing_entry kroute = {}; |
cc57407e | 1693 | |
76fe21de AK |
1694 | if (kvm_gsi_direct_mapping()) { |
1695 | return 0; | |
1696 | } | |
1697 | ||
cc57407e JK |
1698 | if (!kvm_irqchip_in_kernel()) { |
1699 | return -ENOSYS; | |
1700 | } | |
1701 | ||
1702 | kroute.gsi = virq; | |
1703 | kroute.type = KVM_IRQ_ROUTING_MSI; | |
1704 | kroute.flags = 0; | |
1705 | kroute.u.msi.address_lo = (uint32_t)msg.address; | |
1706 | kroute.u.msi.address_hi = msg.address >> 32; | |
d07cc1f1 | 1707 | kroute.u.msi.data = le32_to_cpu(msg.data); |
88c725c7 | 1708 | if (pci_available && kvm_msi_devid_required()) { |
767a554a PF |
1709 | kroute.flags = KVM_MSI_VALID_DEVID; |
1710 | kroute.u.msi.devid = pci_requester_id(dev); | |
1711 | } | |
dc9f06ca | 1712 | if (kvm_arch_fixup_msi_route(&kroute, msg.address, msg.data, dev)) { |
9e03a040 FB |
1713 | return -EINVAL; |
1714 | } | |
cc57407e | 1715 | |
54a6c11b PX |
1716 | trace_kvm_irqchip_update_msi_route(virq); |
1717 | ||
cc57407e JK |
1718 | return kvm_update_routing_entry(s, &kroute); |
1719 | } | |
1720 | ||
ff66ba87 PX |
1721 | static int kvm_irqchip_assign_irqfd(KVMState *s, EventNotifier *event, |
1722 | EventNotifier *resample, int virq, | |
ca916d37 | 1723 | bool assign) |
39853bbc | 1724 | { |
ff66ba87 PX |
1725 | int fd = event_notifier_get_fd(event); |
1726 | int rfd = resample ? event_notifier_get_fd(resample) : -1; | |
1727 | ||
39853bbc JK |
1728 | struct kvm_irqfd irqfd = { |
1729 | .fd = fd, | |
1730 | .gsi = virq, | |
1731 | .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN, | |
1732 | }; | |
1733 | ||
ca916d37 | 1734 | if (rfd != -1) { |
c82d9d43 PX |
1735 | assert(assign); |
1736 | if (kvm_irqchip_is_split()) { | |
1737 | /* | |
1738 | * When the slow irqchip (e.g. IOAPIC) is in the | |
1739 | * userspace, KVM kernel resamplefd will not work because | |
1740 | * the EOI of the interrupt will be delivered to userspace | |
1741 | * instead, so the KVM kernel resamplefd kick will be | |
1742 | * skipped. The userspace here mimics what the kernel | |
1743 | * provides with resamplefd, remember the resamplefd and | |
1744 | * kick it when we receive EOI of this IRQ. | |
1745 | * | |
1746 | * This is hackery because IOAPIC is mostly bypassed | |
1747 | * (except EOI broadcasts) when irqfd is used. However | |
1748 | * this can bring much performance back for split irqchip | |
1749 | * with INTx IRQs (for VFIO, this gives 93% perf of the | |
1750 | * full fast path, which is 46% perf boost comparing to | |
1751 | * the INTx slow path). | |
1752 | */ | |
1753 | kvm_resample_fd_insert(virq, resample); | |
1754 | } else { | |
1755 | irqfd.flags |= KVM_IRQFD_FLAG_RESAMPLE; | |
1756 | irqfd.resamplefd = rfd; | |
1757 | } | |
1758 | } else if (!assign) { | |
1759 | if (kvm_irqchip_is_split()) { | |
1760 | kvm_resample_fd_remove(virq); | |
1761 | } | |
ca916d37 VM |
1762 | } |
1763 | ||
cc7e0ddf | 1764 | if (!kvm_irqfds_enabled()) { |
39853bbc JK |
1765 | return -ENOSYS; |
1766 | } | |
1767 | ||
1768 | return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd); | |
1769 | } | |
1770 | ||
d426d9fb CH |
1771 | int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter) |
1772 | { | |
e9af2fef | 1773 | struct kvm_irq_routing_entry kroute = {}; |
d426d9fb CH |
1774 | int virq; |
1775 | ||
1776 | if (!kvm_gsi_routing_enabled()) { | |
1777 | return -ENOSYS; | |
1778 | } | |
1779 | ||
1780 | virq = kvm_irqchip_get_virq(s); | |
1781 | if (virq < 0) { | |
1782 | return virq; | |
1783 | } | |
1784 | ||
1785 | kroute.gsi = virq; | |
1786 | kroute.type = KVM_IRQ_ROUTING_S390_ADAPTER; | |
1787 | kroute.flags = 0; | |
1788 | kroute.u.adapter.summary_addr = adapter->summary_addr; | |
1789 | kroute.u.adapter.ind_addr = adapter->ind_addr; | |
1790 | kroute.u.adapter.summary_offset = adapter->summary_offset; | |
1791 | kroute.u.adapter.ind_offset = adapter->ind_offset; | |
1792 | kroute.u.adapter.adapter_id = adapter->adapter_id; | |
1793 | ||
1794 | kvm_add_routing_entry(s, &kroute); | |
d426d9fb CH |
1795 | |
1796 | return virq; | |
1797 | } | |
1798 | ||
977a8d9c AS |
1799 | int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint) |
1800 | { | |
1801 | struct kvm_irq_routing_entry kroute = {}; | |
1802 | int virq; | |
1803 | ||
1804 | if (!kvm_gsi_routing_enabled()) { | |
1805 | return -ENOSYS; | |
1806 | } | |
1807 | if (!kvm_check_extension(s, KVM_CAP_HYPERV_SYNIC)) { | |
1808 | return -ENOSYS; | |
1809 | } | |
1810 | virq = kvm_irqchip_get_virq(s); | |
1811 | if (virq < 0) { | |
1812 | return virq; | |
1813 | } | |
1814 | ||
1815 | kroute.gsi = virq; | |
1816 | kroute.type = KVM_IRQ_ROUTING_HV_SINT; | |
1817 | kroute.flags = 0; | |
1818 | kroute.u.hv_sint.vcpu = vcpu; | |
1819 | kroute.u.hv_sint.sint = sint; | |
1820 | ||
1821 | kvm_add_routing_entry(s, &kroute); | |
1822 | kvm_irqchip_commit_routes(s); | |
1823 | ||
1824 | return virq; | |
1825 | } | |
1826 | ||
84b058d7 JK |
1827 | #else /* !KVM_CAP_IRQ_ROUTING */ |
1828 | ||
7b774593 | 1829 | void kvm_init_irq_routing(KVMState *s) |
84b058d7 JK |
1830 | { |
1831 | } | |
04fa27f5 | 1832 | |
d3d3bef0 JK |
1833 | void kvm_irqchip_release_virq(KVMState *s, int virq) |
1834 | { | |
1835 | } | |
1836 | ||
04fa27f5 JK |
1837 | int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) |
1838 | { | |
1839 | abort(); | |
1840 | } | |
92b4e489 | 1841 | |
d1f6af6a | 1842 | int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev) |
92b4e489 | 1843 | { |
df410675 | 1844 | return -ENOSYS; |
92b4e489 | 1845 | } |
39853bbc | 1846 | |
d426d9fb CH |
1847 | int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter) |
1848 | { | |
1849 | return -ENOSYS; | |
1850 | } | |
1851 | ||
977a8d9c AS |
1852 | int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint) |
1853 | { | |
1854 | return -ENOSYS; | |
1855 | } | |
1856 | ||
ff66ba87 PX |
1857 | static int kvm_irqchip_assign_irqfd(KVMState *s, EventNotifier *event, |
1858 | EventNotifier *resample, int virq, | |
1859 | bool assign) | |
39853bbc JK |
1860 | { |
1861 | abort(); | |
1862 | } | |
dabe3143 MT |
1863 | |
1864 | int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg) | |
1865 | { | |
1866 | return -ENOSYS; | |
1867 | } | |
84b058d7 JK |
1868 | #endif /* !KVM_CAP_IRQ_ROUTING */ |
1869 | ||
1c9b71a7 EA |
1870 | int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, |
1871 | EventNotifier *rn, int virq) | |
39853bbc | 1872 | { |
ff66ba87 | 1873 | return kvm_irqchip_assign_irqfd(s, n, rn, virq, true); |
39853bbc JK |
1874 | } |
1875 | ||
1c9b71a7 EA |
1876 | int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, |
1877 | int virq) | |
15b2bd18 | 1878 | { |
ff66ba87 | 1879 | return kvm_irqchip_assign_irqfd(s, n, NULL, virq, false); |
15b2bd18 PB |
1880 | } |
1881 | ||
197e3524 EA |
1882 | int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, |
1883 | EventNotifier *rn, qemu_irq irq) | |
1884 | { | |
1885 | gpointer key, gsi; | |
1886 | gboolean found = g_hash_table_lookup_extended(s->gsimap, irq, &key, &gsi); | |
1887 | ||
1888 | if (!found) { | |
1889 | return -ENXIO; | |
1890 | } | |
1891 | return kvm_irqchip_add_irqfd_notifier_gsi(s, n, rn, GPOINTER_TO_INT(gsi)); | |
1892 | } | |
1893 | ||
1894 | int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, | |
1895 | qemu_irq irq) | |
1896 | { | |
1897 | gpointer key, gsi; | |
1898 | gboolean found = g_hash_table_lookup_extended(s->gsimap, irq, &key, &gsi); | |
1899 | ||
1900 | if (!found) { | |
1901 | return -ENXIO; | |
1902 | } | |
1903 | return kvm_irqchip_remove_irqfd_notifier_gsi(s, n, GPOINTER_TO_INT(gsi)); | |
1904 | } | |
1905 | ||
1906 | void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi) | |
1907 | { | |
1908 | g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi)); | |
1909 | } | |
1910 | ||
4376c40d | 1911 | static void kvm_irqchip_create(KVMState *s) |
84b058d7 | 1912 | { |
84b058d7 JK |
1913 | int ret; |
1914 | ||
d1972be1 | 1915 | assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO); |
8db4936b PB |
1916 | if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) { |
1917 | ; | |
1918 | } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) { | |
1919 | ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0); | |
1920 | if (ret < 0) { | |
1921 | fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret)); | |
1922 | exit(1); | |
1923 | } | |
1924 | } else { | |
1925 | return; | |
84b058d7 JK |
1926 | } |
1927 | ||
d6032e06 CD |
1928 | /* First probe and see if there's a arch-specific hook to create the |
1929 | * in-kernel irqchip for us */ | |
4376c40d | 1930 | ret = kvm_arch_irqchip_create(s); |
8db4936b | 1931 | if (ret == 0) { |
d1972be1 | 1932 | if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) { |
15eafc2e PB |
1933 | perror("Split IRQ chip mode not supported."); |
1934 | exit(1); | |
1935 | } else { | |
1936 | ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP); | |
1937 | } | |
8db4936b PB |
1938 | } |
1939 | if (ret < 0) { | |
1940 | fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret)); | |
1941 | exit(1); | |
84b058d7 JK |
1942 | } |
1943 | ||
3d4b2649 | 1944 | kvm_kernel_irqchip = true; |
7ae26bd4 PM |
1945 | /* If we have an in-kernel IRQ chip then we must have asynchronous |
1946 | * interrupt delivery (though the reverse is not necessarily true) | |
1947 | */ | |
1948 | kvm_async_interrupts_allowed = true; | |
215e79c0 | 1949 | kvm_halt_in_kernel_allowed = true; |
84b058d7 JK |
1950 | |
1951 | kvm_init_irq_routing(s); | |
1952 | ||
197e3524 | 1953 | s->gsimap = g_hash_table_new(g_direct_hash, g_direct_equal); |
84b058d7 JK |
1954 | } |
1955 | ||
670436ce AJ |
1956 | /* Find number of supported CPUs using the recommended |
1957 | * procedure from the kernel API documentation to cope with | |
1958 | * older kernels that may be missing capabilities. | |
1959 | */ | |
1960 | static int kvm_recommended_vcpus(KVMState *s) | |
3ed444e9 | 1961 | { |
11748ba7 | 1962 | int ret = kvm_vm_check_extension(s, KVM_CAP_NR_VCPUS); |
670436ce AJ |
1963 | return (ret) ? ret : 4; |
1964 | } | |
3ed444e9 | 1965 | |
670436ce AJ |
1966 | static int kvm_max_vcpus(KVMState *s) |
1967 | { | |
1968 | int ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS); | |
1969 | return (ret) ? ret : kvm_recommended_vcpus(s); | |
3ed444e9 DH |
1970 | } |
1971 | ||
f31e3266 GK |
1972 | static int kvm_max_vcpu_id(KVMState *s) |
1973 | { | |
1974 | int ret = kvm_check_extension(s, KVM_CAP_MAX_VCPU_ID); | |
1975 | return (ret) ? ret : kvm_max_vcpus(s); | |
1976 | } | |
1977 | ||
41264b38 GK |
1978 | bool kvm_vcpu_id_is_valid(int vcpu_id) |
1979 | { | |
4f7f5893 | 1980 | KVMState *s = KVM_STATE(current_accel()); |
f31e3266 | 1981 | return vcpu_id >= 0 && vcpu_id < kvm_max_vcpu_id(s); |
41264b38 GK |
1982 | } |
1983 | ||
f6a1ef64 | 1984 | static int kvm_init(MachineState *ms) |
05330448 | 1985 | { |
f6a1ef64 | 1986 | MachineClass *mc = MACHINE_GET_CLASS(ms); |
168ccc11 JK |
1987 | static const char upgrade_note[] = |
1988 | "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n" | |
1989 | "(see http://sourceforge.net/projects/kvm).\n"; | |
670436ce AJ |
1990 | struct { |
1991 | const char *name; | |
1992 | int num; | |
1993 | } num_cpus[] = { | |
5cc8767d LX |
1994 | { "SMP", ms->smp.cpus }, |
1995 | { "hotpluggable", ms->smp.max_cpus }, | |
670436ce AJ |
1996 | { NULL, } |
1997 | }, *nc = num_cpus; | |
1998 | int soft_vcpus_limit, hard_vcpus_limit; | |
05330448 | 1999 | KVMState *s; |
94a8d39a | 2000 | const KVMCapabilityInfo *missing_cap; |
05330448 | 2001 | int ret; |
7bbda04c | 2002 | int type = 0; |
494cd11d | 2003 | uint64_t dirty_log_manual_caps; |
05330448 | 2004 | |
a2f77862 PX |
2005 | qemu_mutex_init(&kml_slots_lock); |
2006 | ||
fc02086b | 2007 | s = KVM_STATE(ms->accelerator); |
05330448 | 2008 | |
3145fcb6 DG |
2009 | /* |
2010 | * On systems where the kernel can support different base page | |
2011 | * sizes, host page size may be different from TARGET_PAGE_SIZE, | |
2012 | * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum | |
2013 | * page size for the system though. | |
2014 | */ | |
038adc2f | 2015 | assert(TARGET_PAGE_SIZE <= qemu_real_host_page_size); |
3145fcb6 | 2016 | |
aed6efb9 JH |
2017 | s->sigmask_len = 8; |
2018 | ||
e22a25c9 | 2019 | #ifdef KVM_CAP_SET_GUEST_DEBUG |
72cf2d4f | 2020 | QTAILQ_INIT(&s->kvm_sw_breakpoints); |
e22a25c9 | 2021 | #endif |
4c055ab5 | 2022 | QLIST_INIT(&s->kvm_parked_vcpus); |
448058aa | 2023 | s->fd = qemu_open_old("/dev/kvm", O_RDWR); |
05330448 AL |
2024 | if (s->fd == -1) { |
2025 | fprintf(stderr, "Could not access KVM kernel module: %m\n"); | |
2026 | ret = -errno; | |
2027 | goto err; | |
2028 | } | |
2029 | ||
2030 | ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0); | |
2031 | if (ret < KVM_API_VERSION) { | |
0e1dac6c | 2032 | if (ret >= 0) { |
05330448 | 2033 | ret = -EINVAL; |
a426e122 | 2034 | } |
05330448 AL |
2035 | fprintf(stderr, "kvm version too old\n"); |
2036 | goto err; | |
2037 | } | |
2038 | ||
2039 | if (ret > KVM_API_VERSION) { | |
2040 | ret = -EINVAL; | |
2041 | fprintf(stderr, "kvm version not supported\n"); | |
2042 | goto err; | |
2043 | } | |
2044 | ||
cf0f7cf9 | 2045 | kvm_immediate_exit = kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT); |
fb541ca5 AW |
2046 | s->nr_slots = kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS); |
2047 | ||
2048 | /* If unspecified, use the default value */ | |
2049 | if (!s->nr_slots) { | |
2050 | s->nr_slots = 32; | |
2051 | } | |
2052 | ||
8072aae3 AK |
2053 | s->nr_as = kvm_check_extension(s, KVM_CAP_MULTI_ADDRESS_SPACE); |
2054 | if (s->nr_as <= 1) { | |
2055 | s->nr_as = 1; | |
2056 | } | |
2057 | s->as = g_new0(struct KVMAs, s->nr_as); | |
2058 | ||
f2ce39b4 PB |
2059 | if (object_property_find(OBJECT(current_machine), "kvm-type")) { |
2060 | g_autofree char *kvm_type = object_property_get_str(OBJECT(current_machine), | |
2061 | "kvm-type", | |
2062 | &error_abort); | |
dc0ca80e | 2063 | type = mc->kvm_type(ms, kvm_type); |
516fc0a0 AJ |
2064 | } else if (mc->kvm_type) { |
2065 | type = mc->kvm_type(ms, NULL); | |
135a129a AK |
2066 | } |
2067 | ||
94ccff13 | 2068 | do { |
135a129a | 2069 | ret = kvm_ioctl(s, KVM_CREATE_VM, type); |
94ccff13 TK |
2070 | } while (ret == -EINTR); |
2071 | ||
2072 | if (ret < 0) { | |
521f438e | 2073 | fprintf(stderr, "ioctl(KVM_CREATE_VM) failed: %d %s\n", -ret, |
94ccff13 TK |
2074 | strerror(-ret)); |
2075 | ||
0104dcac | 2076 | #ifdef TARGET_S390X |
2c80e996 CH |
2077 | if (ret == -EINVAL) { |
2078 | fprintf(stderr, | |
2079 | "Host kernel setup problem detected. Please verify:\n"); | |
2080 | fprintf(stderr, "- for kernels supporting the switch_amode or" | |
2081 | " user_mode parameters, whether\n"); | |
2082 | fprintf(stderr, | |
2083 | " user space is running in primary address space\n"); | |
2084 | fprintf(stderr, | |
2085 | "- for kernels supporting the vm.allocate_pgste sysctl, " | |
2086 | "whether it is enabled\n"); | |
2087 | } | |
0104dcac | 2088 | #endif |
05330448 | 2089 | goto err; |
0104dcac | 2090 | } |
05330448 | 2091 | |
94ccff13 | 2092 | s->vmfd = ret; |
11748ba7 GK |
2093 | |
2094 | /* check the vcpu limits */ | |
2095 | soft_vcpus_limit = kvm_recommended_vcpus(s); | |
2096 | hard_vcpus_limit = kvm_max_vcpus(s); | |
2097 | ||
2098 | while (nc->name) { | |
2099 | if (nc->num > soft_vcpus_limit) { | |
2100 | warn_report("Number of %s cpus requested (%d) exceeds " | |
2101 | "the recommended cpus supported by KVM (%d)", | |
2102 | nc->name, nc->num, soft_vcpus_limit); | |
2103 | ||
2104 | if (nc->num > hard_vcpus_limit) { | |
2105 | fprintf(stderr, "Number of %s cpus requested (%d) exceeds " | |
2106 | "the maximum cpus supported by KVM (%d)\n", | |
2107 | nc->name, nc->num, hard_vcpus_limit); | |
2108 | exit(1); | |
2109 | } | |
2110 | } | |
2111 | nc++; | |
2112 | } | |
2113 | ||
94a8d39a JK |
2114 | missing_cap = kvm_check_extension_list(s, kvm_required_capabilites); |
2115 | if (!missing_cap) { | |
2116 | missing_cap = | |
2117 | kvm_check_extension_list(s, kvm_arch_required_capabilities); | |
05330448 | 2118 | } |
94a8d39a | 2119 | if (missing_cap) { |
ad7b8b33 | 2120 | ret = -EINVAL; |
94a8d39a JK |
2121 | fprintf(stderr, "kvm does not support %s\n%s", |
2122 | missing_cap->name, upgrade_note); | |
d85dc283 AL |
2123 | goto err; |
2124 | } | |
2125 | ||
ad7b8b33 | 2126 | s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO); |
e6d34aee PH |
2127 | s->coalesced_pio = s->coalesced_mmio && |
2128 | kvm_check_extension(s, KVM_CAP_COALESCED_PIO); | |
f65ed4c1 | 2129 | |
494cd11d | 2130 | dirty_log_manual_caps = |
ff4aa114 | 2131 | kvm_check_extension(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); |
494cd11d JZ |
2132 | dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | |
2133 | KVM_DIRTY_LOG_INITIALLY_SET); | |
2134 | s->manual_dirty_log_protect = dirty_log_manual_caps; | |
2135 | if (dirty_log_manual_caps) { | |
2136 | ret = kvm_vm_enable_cap(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, 0, | |
2137 | dirty_log_manual_caps); | |
ff4aa114 | 2138 | if (ret) { |
494cd11d JZ |
2139 | warn_report("Trying to enable capability %"PRIu64" of " |
2140 | "KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed. " | |
2141 | "Falling back to the legacy mode. ", | |
2142 | dirty_log_manual_caps); | |
2143 | s->manual_dirty_log_protect = 0; | |
ff4aa114 PX |
2144 | } |
2145 | } | |
2146 | ||
a0fb002c JK |
2147 | #ifdef KVM_CAP_VCPU_EVENTS |
2148 | s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS); | |
2149 | #endif | |
2150 | ||
b0b1d690 JK |
2151 | s->robust_singlestep = |
2152 | kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP); | |
b0b1d690 | 2153 | |
ff44f1a3 JK |
2154 | #ifdef KVM_CAP_DEBUGREGS |
2155 | s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS); | |
2156 | #endif | |
2157 | ||
ebbfef2f LA |
2158 | s->max_nested_state_len = kvm_check_extension(s, KVM_CAP_NESTED_STATE); |
2159 | ||
d3d3bef0 | 2160 | #ifdef KVM_CAP_IRQ_ROUTING |
50bf31b9 | 2161 | kvm_direct_msi_allowed = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0); |
d3d3bef0 | 2162 | #endif |
4a3adebb | 2163 | |
3ab73842 JK |
2164 | s->intx_set_mask = kvm_check_extension(s, KVM_CAP_PCI_2_3); |
2165 | ||
e333cd69 | 2166 | s->irq_set_ioctl = KVM_IRQ_LINE; |
8732fbd2 | 2167 | if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) { |
e333cd69 | 2168 | s->irq_set_ioctl = KVM_IRQ_LINE_STATUS; |
8732fbd2 PM |
2169 | } |
2170 | ||
df9c8b75 JJ |
2171 | kvm_readonly_mem_allowed = |
2172 | (kvm_check_extension(s, KVM_CAP_READONLY_MEM) > 0); | |
df9c8b75 | 2173 | |
69e03ae6 NN |
2174 | kvm_eventfds_allowed = |
2175 | (kvm_check_extension(s, KVM_CAP_IOEVENTFD) > 0); | |
2176 | ||
f41389ae EA |
2177 | kvm_irqfds_allowed = |
2178 | (kvm_check_extension(s, KVM_CAP_IRQFD) > 0); | |
2179 | ||
2180 | kvm_resamplefds_allowed = | |
2181 | (kvm_check_extension(s, KVM_CAP_IRQFD_RESAMPLE) > 0); | |
2182 | ||
d0a073a1 DD |
2183 | kvm_vm_attributes_allowed = |
2184 | (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES) > 0); | |
2185 | ||
35108223 JW |
2186 | kvm_ioeventfd_any_length_allowed = |
2187 | (kvm_check_extension(s, KVM_CAP_IOEVENTFD_ANY_LENGTH) > 0); | |
2188 | ||
d870cfde GA |
2189 | kvm_state = s; |
2190 | ||
b16565b3 | 2191 | ret = kvm_arch_init(ms, s); |
a426e122 | 2192 | if (ret < 0) { |
05330448 | 2193 | goto err; |
a426e122 | 2194 | } |
05330448 | 2195 | |
d1972be1 XL |
2196 | if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) { |
2197 | s->kernel_irqchip_split = mc->default_kernel_irqchip_split ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; | |
2198 | } | |
2199 | ||
6b552b9b DG |
2200 | qemu_register_reset(kvm_unpoison_all, NULL); |
2201 | ||
11bc4a13 | 2202 | if (s->kernel_irqchip_allowed) { |
4376c40d | 2203 | kvm_irqchip_create(s); |
84b058d7 JK |
2204 | } |
2205 | ||
8c56c1a5 PF |
2206 | if (kvm_eventfds_allowed) { |
2207 | s->memory_listener.listener.eventfd_add = kvm_mem_ioeventfd_add; | |
2208 | s->memory_listener.listener.eventfd_del = kvm_mem_ioeventfd_del; | |
2209 | } | |
e6d34aee PH |
2210 | s->memory_listener.listener.coalesced_io_add = kvm_coalesce_mmio_region; |
2211 | s->memory_listener.listener.coalesced_io_del = kvm_uncoalesce_mmio_region; | |
7bbda04c PB |
2212 | |
2213 | kvm_memory_listener_register(s, &s->memory_listener, | |
38bfe691 | 2214 | &address_space_memory, 0); |
f9b49088 EA |
2215 | if (kvm_eventfds_allowed) { |
2216 | memory_listener_register(&kvm_io_listener, | |
2217 | &address_space_io); | |
2218 | } | |
e6d34aee PH |
2219 | memory_listener_register(&kvm_coalesced_pio_listener, |
2220 | &address_space_io); | |
05330448 | 2221 | |
d2f2b8a7 SH |
2222 | s->many_ioeventfds = kvm_check_many_ioeventfds(); |
2223 | ||
62dd4eda | 2224 | s->sync_mmu = !!kvm_vm_check_extension(kvm_state, KVM_CAP_SYNC_MMU); |
f5948942 | 2225 | if (!s->sync_mmu) { |
956b109f DH |
2226 | ret = ram_block_discard_disable(true); |
2227 | assert(!ret); | |
f5948942 | 2228 | } |
05330448 AL |
2229 | return 0; |
2230 | ||
2231 | err: | |
0e1dac6c | 2232 | assert(ret < 0); |
6d1cc321 SW |
2233 | if (s->vmfd >= 0) { |
2234 | close(s->vmfd); | |
2235 | } | |
2236 | if (s->fd != -1) { | |
2237 | close(s->fd); | |
05330448 | 2238 | } |
7bbda04c | 2239 | g_free(s->memory_listener.slots); |
05330448 AL |
2240 | |
2241 | return ret; | |
2242 | } | |
2243 | ||
aed6efb9 JH |
2244 | void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len) |
2245 | { | |
2246 | s->sigmask_len = sigmask_len; | |
2247 | } | |
2248 | ||
4c663752 PB |
2249 | static void kvm_handle_io(uint16_t port, MemTxAttrs attrs, void *data, int direction, |
2250 | int size, uint32_t count) | |
05330448 AL |
2251 | { |
2252 | int i; | |
2253 | uint8_t *ptr = data; | |
2254 | ||
2255 | for (i = 0; i < count; i++) { | |
4c663752 | 2256 | address_space_rw(&address_space_io, port, attrs, |
5c9eb028 | 2257 | ptr, size, |
354678c5 | 2258 | direction == KVM_EXIT_IO_OUT); |
05330448 AL |
2259 | ptr += size; |
2260 | } | |
05330448 AL |
2261 | } |
2262 | ||
5326ab55 | 2263 | static int kvm_handle_internal_error(CPUState *cpu, struct kvm_run *run) |
7c80eef8 | 2264 | { |
977c7b6d RK |
2265 | fprintf(stderr, "KVM internal error. Suberror: %d\n", |
2266 | run->internal.suberror); | |
2267 | ||
7c80eef8 MT |
2268 | if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) { |
2269 | int i; | |
2270 | ||
7c80eef8 | 2271 | for (i = 0; i < run->internal.ndata; ++i) { |
56567da3 | 2272 | fprintf(stderr, "extra data[%d]: 0x%016"PRIx64"\n", |
7c80eef8 MT |
2273 | i, (uint64_t)run->internal.data[i]); |
2274 | } | |
2275 | } | |
7c80eef8 MT |
2276 | if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) { |
2277 | fprintf(stderr, "emulation failure\n"); | |
20d695a9 | 2278 | if (!kvm_arch_stop_on_emulation_error(cpu)) { |
90c84c56 | 2279 | cpu_dump_state(cpu, stderr, CPU_DUMP_CODE); |
d73cd8f4 | 2280 | return EXCP_INTERRUPT; |
a426e122 | 2281 | } |
7c80eef8 MT |
2282 | } |
2283 | /* FIXME: Should trigger a qmp message to let management know | |
2284 | * something went wrong. | |
2285 | */ | |
73aaec4a | 2286 | return -1; |
7c80eef8 | 2287 | } |
7c80eef8 | 2288 | |
62a2744c | 2289 | void kvm_flush_coalesced_mmio_buffer(void) |
f65ed4c1 | 2290 | { |
f65ed4c1 | 2291 | KVMState *s = kvm_state; |
1cae88b9 AK |
2292 | |
2293 | if (s->coalesced_flush_in_progress) { | |
2294 | return; | |
2295 | } | |
2296 | ||
2297 | s->coalesced_flush_in_progress = true; | |
2298 | ||
62a2744c SY |
2299 | if (s->coalesced_mmio_ring) { |
2300 | struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring; | |
f65ed4c1 AL |
2301 | while (ring->first != ring->last) { |
2302 | struct kvm_coalesced_mmio *ent; | |
2303 | ||
2304 | ent = &ring->coalesced_mmio[ring->first]; | |
2305 | ||
e6d34aee | 2306 | if (ent->pio == 1) { |
19f70347 PM |
2307 | address_space_write(&address_space_io, ent->phys_addr, |
2308 | MEMTXATTRS_UNSPECIFIED, ent->data, | |
2309 | ent->len); | |
e6d34aee PH |
2310 | } else { |
2311 | cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len); | |
2312 | } | |
85199474 | 2313 | smp_wmb(); |
f65ed4c1 AL |
2314 | ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX; |
2315 | } | |
2316 | } | |
1cae88b9 AK |
2317 | |
2318 | s->coalesced_flush_in_progress = false; | |
f65ed4c1 AL |
2319 | } |
2320 | ||
92a5199b TL |
2321 | bool kvm_cpu_check_are_resettable(void) |
2322 | { | |
2323 | return kvm_arch_cpu_check_are_resettable(); | |
2324 | } | |
2325 | ||
14e6fe12 | 2326 | static void do_kvm_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg) |
4c0960c0 | 2327 | { |
99f31832 | 2328 | if (!cpu->vcpu_dirty) { |
20d695a9 | 2329 | kvm_arch_get_registers(cpu); |
99f31832 | 2330 | cpu->vcpu_dirty = true; |
4c0960c0 AK |
2331 | } |
2332 | } | |
2333 | ||
dd1750d7 | 2334 | void kvm_cpu_synchronize_state(CPUState *cpu) |
2705d56a | 2335 | { |
99f31832 | 2336 | if (!cpu->vcpu_dirty) { |
14e6fe12 | 2337 | run_on_cpu(cpu, do_kvm_cpu_synchronize_state, RUN_ON_CPU_NULL); |
a426e122 | 2338 | } |
2705d56a JK |
2339 | } |
2340 | ||
14e6fe12 | 2341 | static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg) |
ea375f9a | 2342 | { |
20d695a9 | 2343 | kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE); |
99f31832 | 2344 | cpu->vcpu_dirty = false; |
ea375f9a JK |
2345 | } |
2346 | ||
c8e2085d DH |
2347 | void kvm_cpu_synchronize_post_reset(CPUState *cpu) |
2348 | { | |
14e6fe12 | 2349 | run_on_cpu(cpu, do_kvm_cpu_synchronize_post_reset, RUN_ON_CPU_NULL); |
c8e2085d DH |
2350 | } |
2351 | ||
14e6fe12 | 2352 | static void do_kvm_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg) |
ea375f9a | 2353 | { |
20d695a9 | 2354 | kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE); |
99f31832 | 2355 | cpu->vcpu_dirty = false; |
ea375f9a JK |
2356 | } |
2357 | ||
c8e2085d DH |
2358 | void kvm_cpu_synchronize_post_init(CPUState *cpu) |
2359 | { | |
14e6fe12 | 2360 | run_on_cpu(cpu, do_kvm_cpu_synchronize_post_init, RUN_ON_CPU_NULL); |
c8e2085d DH |
2361 | } |
2362 | ||
75e972da DG |
2363 | static void do_kvm_cpu_synchronize_pre_loadvm(CPUState *cpu, run_on_cpu_data arg) |
2364 | { | |
99f31832 | 2365 | cpu->vcpu_dirty = true; |
75e972da DG |
2366 | } |
2367 | ||
2368 | void kvm_cpu_synchronize_pre_loadvm(CPUState *cpu) | |
2369 | { | |
2370 | run_on_cpu(cpu, do_kvm_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL); | |
2371 | } | |
2372 | ||
2ae41db2 PB |
2373 | #ifdef KVM_HAVE_MCE_INJECTION |
2374 | static __thread void *pending_sigbus_addr; | |
2375 | static __thread int pending_sigbus_code; | |
2376 | static __thread bool have_sigbus_pending; | |
2377 | #endif | |
2378 | ||
cf0f7cf9 PB |
2379 | static void kvm_cpu_kick(CPUState *cpu) |
2380 | { | |
d73415a3 | 2381 | qatomic_set(&cpu->kvm_run->immediate_exit, 1); |
cf0f7cf9 PB |
2382 | } |
2383 | ||
2384 | static void kvm_cpu_kick_self(void) | |
2385 | { | |
2386 | if (kvm_immediate_exit) { | |
2387 | kvm_cpu_kick(current_cpu); | |
2388 | } else { | |
2389 | qemu_cpu_kick_self(); | |
2390 | } | |
2391 | } | |
2392 | ||
18268b60 PB |
2393 | static void kvm_eat_signals(CPUState *cpu) |
2394 | { | |
2395 | struct timespec ts = { 0, 0 }; | |
2396 | siginfo_t siginfo; | |
2397 | sigset_t waitset; | |
2398 | sigset_t chkset; | |
2399 | int r; | |
2400 | ||
cf0f7cf9 | 2401 | if (kvm_immediate_exit) { |
d73415a3 | 2402 | qatomic_set(&cpu->kvm_run->immediate_exit, 0); |
cf0f7cf9 PB |
2403 | /* Write kvm_run->immediate_exit before the cpu->exit_request |
2404 | * write in kvm_cpu_exec. | |
2405 | */ | |
2406 | smp_wmb(); | |
2407 | return; | |
2408 | } | |
2409 | ||
18268b60 PB |
2410 | sigemptyset(&waitset); |
2411 | sigaddset(&waitset, SIG_IPI); | |
2412 | ||
2413 | do { | |
2414 | r = sigtimedwait(&waitset, &siginfo, &ts); | |
2415 | if (r == -1 && !(errno == EAGAIN || errno == EINTR)) { | |
2416 | perror("sigtimedwait"); | |
2417 | exit(1); | |
2418 | } | |
2419 | ||
2420 | r = sigpending(&chkset); | |
2421 | if (r == -1) { | |
2422 | perror("sigpending"); | |
2423 | exit(1); | |
2424 | } | |
2425 | } while (sigismember(&chkset, SIG_IPI)); | |
2426 | } | |
2427 | ||
1458c363 | 2428 | int kvm_cpu_exec(CPUState *cpu) |
05330448 | 2429 | { |
f7575c96 | 2430 | struct kvm_run *run = cpu->kvm_run; |
7cbb533f | 2431 | int ret, run_ret; |
05330448 | 2432 | |
8c0d577e | 2433 | DPRINTF("kvm_cpu_exec()\n"); |
05330448 | 2434 | |
20d695a9 | 2435 | if (kvm_arch_process_async_events(cpu)) { |
d73415a3 | 2436 | qatomic_set(&cpu->exit_request, 0); |
6792a57b | 2437 | return EXCP_HLT; |
9ccfac9e | 2438 | } |
0af691d7 | 2439 | |
4b8523ee | 2440 | qemu_mutex_unlock_iothread(); |
1d78a3c3 | 2441 | cpu_exec_start(cpu); |
4b8523ee | 2442 | |
9ccfac9e | 2443 | do { |
4c663752 PB |
2444 | MemTxAttrs attrs; |
2445 | ||
99f31832 | 2446 | if (cpu->vcpu_dirty) { |
20d695a9 | 2447 | kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE); |
99f31832 | 2448 | cpu->vcpu_dirty = false; |
4c0960c0 AK |
2449 | } |
2450 | ||
20d695a9 | 2451 | kvm_arch_pre_run(cpu, run); |
d73415a3 | 2452 | if (qatomic_read(&cpu->exit_request)) { |
9ccfac9e JK |
2453 | DPRINTF("interrupt exit requested\n"); |
2454 | /* | |
2455 | * KVM requires us to reenter the kernel after IO exits to complete | |
2456 | * instruction emulation. This self-signal will ensure that we | |
2457 | * leave ASAP again. | |
2458 | */ | |
cf0f7cf9 | 2459 | kvm_cpu_kick_self(); |
9ccfac9e | 2460 | } |
9ccfac9e | 2461 | |
cf0f7cf9 PB |
2462 | /* Read cpu->exit_request before KVM_RUN reads run->immediate_exit. |
2463 | * Matching barrier in kvm_eat_signals. | |
2464 | */ | |
2465 | smp_rmb(); | |
2466 | ||
1bc22652 | 2467 | run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0); |
9ccfac9e | 2468 | |
4c663752 | 2469 | attrs = kvm_arch_post_run(cpu, run); |
05330448 | 2470 | |
2ae41db2 PB |
2471 | #ifdef KVM_HAVE_MCE_INJECTION |
2472 | if (unlikely(have_sigbus_pending)) { | |
2473 | qemu_mutex_lock_iothread(); | |
2474 | kvm_arch_on_sigbus_vcpu(cpu, pending_sigbus_code, | |
2475 | pending_sigbus_addr); | |
2476 | have_sigbus_pending = false; | |
2477 | qemu_mutex_unlock_iothread(); | |
2478 | } | |
2479 | #endif | |
2480 | ||
7cbb533f | 2481 | if (run_ret < 0) { |
dc77d341 JK |
2482 | if (run_ret == -EINTR || run_ret == -EAGAIN) { |
2483 | DPRINTF("io window exit\n"); | |
18268b60 | 2484 | kvm_eat_signals(cpu); |
d73cd8f4 | 2485 | ret = EXCP_INTERRUPT; |
dc77d341 JK |
2486 | break; |
2487 | } | |
7b011fbc ME |
2488 | fprintf(stderr, "error: kvm run failed %s\n", |
2489 | strerror(-run_ret)); | |
dae02ba5 LV |
2490 | #ifdef TARGET_PPC |
2491 | if (run_ret == -EBUSY) { | |
2492 | fprintf(stderr, | |
2493 | "This is probably because your SMT is enabled.\n" | |
2494 | "VCPU can only run on primary threads with all " | |
2495 | "secondary threads offline.\n"); | |
2496 | } | |
2497 | #endif | |
a85e130e PB |
2498 | ret = -1; |
2499 | break; | |
05330448 AL |
2500 | } |
2501 | ||
b76ac80a | 2502 | trace_kvm_run_exit(cpu->cpu_index, run->exit_reason); |
05330448 AL |
2503 | switch (run->exit_reason) { |
2504 | case KVM_EXIT_IO: | |
8c0d577e | 2505 | DPRINTF("handle_io\n"); |
80b7d2ef | 2506 | /* Called outside BQL */ |
4c663752 | 2507 | kvm_handle_io(run->io.port, attrs, |
b30e93e9 JK |
2508 | (uint8_t *)run + run->io.data_offset, |
2509 | run->io.direction, | |
2510 | run->io.size, | |
2511 | run->io.count); | |
d73cd8f4 | 2512 | ret = 0; |
05330448 AL |
2513 | break; |
2514 | case KVM_EXIT_MMIO: | |
8c0d577e | 2515 | DPRINTF("handle_mmio\n"); |
de7ea885 | 2516 | /* Called outside BQL */ |
4c663752 PB |
2517 | address_space_rw(&address_space_memory, |
2518 | run->mmio.phys_addr, attrs, | |
2519 | run->mmio.data, | |
2520 | run->mmio.len, | |
2521 | run->mmio.is_write); | |
d73cd8f4 | 2522 | ret = 0; |
05330448 AL |
2523 | break; |
2524 | case KVM_EXIT_IRQ_WINDOW_OPEN: | |
8c0d577e | 2525 | DPRINTF("irq_window_open\n"); |
d73cd8f4 | 2526 | ret = EXCP_INTERRUPT; |
05330448 AL |
2527 | break; |
2528 | case KVM_EXIT_SHUTDOWN: | |
8c0d577e | 2529 | DPRINTF("shutdown\n"); |
cf83f140 | 2530 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
d73cd8f4 | 2531 | ret = EXCP_INTERRUPT; |
05330448 AL |
2532 | break; |
2533 | case KVM_EXIT_UNKNOWN: | |
bb44e0d1 JK |
2534 | fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n", |
2535 | (uint64_t)run->hw.hardware_exit_reason); | |
73aaec4a | 2536 | ret = -1; |
05330448 | 2537 | break; |
7c80eef8 | 2538 | case KVM_EXIT_INTERNAL_ERROR: |
5326ab55 | 2539 | ret = kvm_handle_internal_error(cpu, run); |
7c80eef8 | 2540 | break; |
99040447 PS |
2541 | case KVM_EXIT_SYSTEM_EVENT: |
2542 | switch (run->system_event.type) { | |
2543 | case KVM_SYSTEM_EVENT_SHUTDOWN: | |
cf83f140 | 2544 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); |
99040447 PS |
2545 | ret = EXCP_INTERRUPT; |
2546 | break; | |
2547 | case KVM_SYSTEM_EVENT_RESET: | |
cf83f140 | 2548 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
99040447 PS |
2549 | ret = EXCP_INTERRUPT; |
2550 | break; | |
7c207b90 | 2551 | case KVM_SYSTEM_EVENT_CRASH: |
d187e08d | 2552 | kvm_cpu_synchronize_state(cpu); |
7c207b90 | 2553 | qemu_mutex_lock_iothread(); |
c86f106b | 2554 | qemu_system_guest_panicked(cpu_get_crash_info(cpu)); |
7c207b90 AS |
2555 | qemu_mutex_unlock_iothread(); |
2556 | ret = 0; | |
2557 | break; | |
99040447 PS |
2558 | default: |
2559 | DPRINTF("kvm_arch_handle_exit\n"); | |
2560 | ret = kvm_arch_handle_exit(cpu, run); | |
2561 | break; | |
2562 | } | |
2563 | break; | |
05330448 | 2564 | default: |
8c0d577e | 2565 | DPRINTF("kvm_arch_handle_exit\n"); |
20d695a9 | 2566 | ret = kvm_arch_handle_exit(cpu, run); |
05330448 AL |
2567 | break; |
2568 | } | |
d73cd8f4 | 2569 | } while (ret == 0); |
05330448 | 2570 | |
1d78a3c3 | 2571 | cpu_exec_end(cpu); |
4b8523ee JK |
2572 | qemu_mutex_lock_iothread(); |
2573 | ||
73aaec4a | 2574 | if (ret < 0) { |
90c84c56 | 2575 | cpu_dump_state(cpu, stderr, CPU_DUMP_CODE); |
0461d5a6 | 2576 | vm_stop(RUN_STATE_INTERNAL_ERROR); |
becfc390 AL |
2577 | } |
2578 | ||
d73415a3 | 2579 | qatomic_set(&cpu->exit_request, 0); |
05330448 AL |
2580 | return ret; |
2581 | } | |
2582 | ||
984b5181 | 2583 | int kvm_ioctl(KVMState *s, int type, ...) |
05330448 AL |
2584 | { |
2585 | int ret; | |
984b5181 AL |
2586 | void *arg; |
2587 | va_list ap; | |
05330448 | 2588 | |
984b5181 AL |
2589 | va_start(ap, type); |
2590 | arg = va_arg(ap, void *); | |
2591 | va_end(ap); | |
2592 | ||
9c775729 | 2593 | trace_kvm_ioctl(type, arg); |
984b5181 | 2594 | ret = ioctl(s->fd, type, arg); |
a426e122 | 2595 | if (ret == -1) { |
05330448 | 2596 | ret = -errno; |
a426e122 | 2597 | } |
05330448 AL |
2598 | return ret; |
2599 | } | |
2600 | ||
984b5181 | 2601 | int kvm_vm_ioctl(KVMState *s, int type, ...) |
05330448 AL |
2602 | { |
2603 | int ret; | |
984b5181 AL |
2604 | void *arg; |
2605 | va_list ap; | |
2606 | ||
2607 | va_start(ap, type); | |
2608 | arg = va_arg(ap, void *); | |
2609 | va_end(ap); | |
05330448 | 2610 | |
9c775729 | 2611 | trace_kvm_vm_ioctl(type, arg); |
984b5181 | 2612 | ret = ioctl(s->vmfd, type, arg); |
a426e122 | 2613 | if (ret == -1) { |
05330448 | 2614 | ret = -errno; |
a426e122 | 2615 | } |
05330448 AL |
2616 | return ret; |
2617 | } | |
2618 | ||
1bc22652 | 2619 | int kvm_vcpu_ioctl(CPUState *cpu, int type, ...) |
05330448 AL |
2620 | { |
2621 | int ret; | |
984b5181 AL |
2622 | void *arg; |
2623 | va_list ap; | |
2624 | ||
2625 | va_start(ap, type); | |
2626 | arg = va_arg(ap, void *); | |
2627 | va_end(ap); | |
05330448 | 2628 | |
9c775729 | 2629 | trace_kvm_vcpu_ioctl(cpu->cpu_index, type, arg); |
8737c51c | 2630 | ret = ioctl(cpu->kvm_fd, type, arg); |
a426e122 | 2631 | if (ret == -1) { |
05330448 | 2632 | ret = -errno; |
a426e122 | 2633 | } |
05330448 AL |
2634 | return ret; |
2635 | } | |
bd322087 | 2636 | |
0a6a7cca CD |
2637 | int kvm_device_ioctl(int fd, int type, ...) |
2638 | { | |
2639 | int ret; | |
2640 | void *arg; | |
2641 | va_list ap; | |
2642 | ||
2643 | va_start(ap, type); | |
2644 | arg = va_arg(ap, void *); | |
2645 | va_end(ap); | |
2646 | ||
2647 | trace_kvm_device_ioctl(fd, type, arg); | |
2648 | ret = ioctl(fd, type, arg); | |
2649 | if (ret == -1) { | |
2650 | ret = -errno; | |
2651 | } | |
2652 | return ret; | |
2653 | } | |
2654 | ||
d0a073a1 DD |
2655 | int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr) |
2656 | { | |
2657 | int ret; | |
2658 | struct kvm_device_attr attribute = { | |
2659 | .group = group, | |
2660 | .attr = attr, | |
2661 | }; | |
2662 | ||
2663 | if (!kvm_vm_attributes_allowed) { | |
2664 | return 0; | |
2665 | } | |
2666 | ||
2667 | ret = kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attribute); | |
2668 | /* kvm returns 0 on success for HAS_DEVICE_ATTR */ | |
2669 | return ret ? 0 : 1; | |
2670 | } | |
2671 | ||
4b3cfe72 PF |
2672 | int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr) |
2673 | { | |
2674 | struct kvm_device_attr attribute = { | |
2675 | .group = group, | |
2676 | .attr = attr, | |
2677 | .flags = 0, | |
2678 | }; | |
2679 | ||
2680 | return kvm_device_ioctl(dev_fd, KVM_HAS_DEVICE_ATTR, &attribute) ? 0 : 1; | |
2681 | } | |
2682 | ||
556969e9 EA |
2683 | int kvm_device_access(int fd, int group, uint64_t attr, |
2684 | void *val, bool write, Error **errp) | |
4b3cfe72 PF |
2685 | { |
2686 | struct kvm_device_attr kvmattr; | |
2687 | int err; | |
2688 | ||
2689 | kvmattr.flags = 0; | |
2690 | kvmattr.group = group; | |
2691 | kvmattr.attr = attr; | |
2692 | kvmattr.addr = (uintptr_t)val; | |
2693 | ||
2694 | err = kvm_device_ioctl(fd, | |
2695 | write ? KVM_SET_DEVICE_ATTR : KVM_GET_DEVICE_ATTR, | |
2696 | &kvmattr); | |
2697 | if (err < 0) { | |
556969e9 EA |
2698 | error_setg_errno(errp, -err, |
2699 | "KVM_%s_DEVICE_ATTR failed: Group %d " | |
2700 | "attr 0x%016" PRIx64, | |
2701 | write ? "SET" : "GET", group, attr); | |
4b3cfe72 | 2702 | } |
556969e9 | 2703 | return err; |
4b3cfe72 PF |
2704 | } |
2705 | ||
62dd4eda | 2706 | bool kvm_has_sync_mmu(void) |
bd322087 | 2707 | { |
62dd4eda | 2708 | return kvm_state->sync_mmu; |
bd322087 | 2709 | } |
e22a25c9 | 2710 | |
a0fb002c JK |
2711 | int kvm_has_vcpu_events(void) |
2712 | { | |
2713 | return kvm_state->vcpu_events; | |
2714 | } | |
2715 | ||
b0b1d690 JK |
2716 | int kvm_has_robust_singlestep(void) |
2717 | { | |
2718 | return kvm_state->robust_singlestep; | |
2719 | } | |
2720 | ||
ff44f1a3 JK |
2721 | int kvm_has_debugregs(void) |
2722 | { | |
2723 | return kvm_state->debugregs; | |
2724 | } | |
2725 | ||
ebbfef2f LA |
2726 | int kvm_max_nested_state_length(void) |
2727 | { | |
2728 | return kvm_state->max_nested_state_len; | |
2729 | } | |
2730 | ||
d2f2b8a7 SH |
2731 | int kvm_has_many_ioeventfds(void) |
2732 | { | |
2733 | if (!kvm_enabled()) { | |
2734 | return 0; | |
2735 | } | |
2736 | return kvm_state->many_ioeventfds; | |
2737 | } | |
2738 | ||
84b058d7 JK |
2739 | int kvm_has_gsi_routing(void) |
2740 | { | |
a9c5eb0d | 2741 | #ifdef KVM_CAP_IRQ_ROUTING |
84b058d7 | 2742 | return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING); |
a9c5eb0d AG |
2743 | #else |
2744 | return false; | |
2745 | #endif | |
84b058d7 JK |
2746 | } |
2747 | ||
3ab73842 JK |
2748 | int kvm_has_intx_set_mask(void) |
2749 | { | |
2750 | return kvm_state->intx_set_mask; | |
2751 | } | |
2752 | ||
5d721b78 AG |
2753 | bool kvm_arm_supports_user_irq(void) |
2754 | { | |
2755 | return kvm_check_extension(kvm_state, KVM_CAP_ARM_USER_IRQ); | |
2756 | } | |
2757 | ||
e22a25c9 | 2758 | #ifdef KVM_CAP_SET_GUEST_DEBUG |
a60f24b5 | 2759 | struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu, |
e22a25c9 AL |
2760 | target_ulong pc) |
2761 | { | |
2762 | struct kvm_sw_breakpoint *bp; | |
2763 | ||
a60f24b5 | 2764 | QTAILQ_FOREACH(bp, &cpu->kvm_state->kvm_sw_breakpoints, entry) { |
a426e122 | 2765 | if (bp->pc == pc) { |
e22a25c9 | 2766 | return bp; |
a426e122 | 2767 | } |
e22a25c9 AL |
2768 | } |
2769 | return NULL; | |
2770 | } | |
2771 | ||
a60f24b5 | 2772 | int kvm_sw_breakpoints_active(CPUState *cpu) |
e22a25c9 | 2773 | { |
a60f24b5 | 2774 | return !QTAILQ_EMPTY(&cpu->kvm_state->kvm_sw_breakpoints); |
e22a25c9 AL |
2775 | } |
2776 | ||
452e4751 GC |
2777 | struct kvm_set_guest_debug_data { |
2778 | struct kvm_guest_debug dbg; | |
452e4751 GC |
2779 | int err; |
2780 | }; | |
2781 | ||
14e6fe12 | 2782 | static void kvm_invoke_set_guest_debug(CPUState *cpu, run_on_cpu_data data) |
452e4751 | 2783 | { |
14e6fe12 PB |
2784 | struct kvm_set_guest_debug_data *dbg_data = |
2785 | (struct kvm_set_guest_debug_data *) data.host_ptr; | |
b3807725 | 2786 | |
3c0ed2a3 | 2787 | dbg_data->err = kvm_vcpu_ioctl(cpu, KVM_SET_GUEST_DEBUG, |
a60f24b5 | 2788 | &dbg_data->dbg); |
452e4751 GC |
2789 | } |
2790 | ||
38e478ec | 2791 | int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap) |
e22a25c9 | 2792 | { |
452e4751 | 2793 | struct kvm_set_guest_debug_data data; |
e22a25c9 | 2794 | |
b0b1d690 | 2795 | data.dbg.control = reinject_trap; |
e22a25c9 | 2796 | |
ed2803da | 2797 | if (cpu->singlestep_enabled) { |
b0b1d690 JK |
2798 | data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP; |
2799 | } | |
20d695a9 | 2800 | kvm_arch_update_guest_debug(cpu, &data.dbg); |
e22a25c9 | 2801 | |
14e6fe12 PB |
2802 | run_on_cpu(cpu, kvm_invoke_set_guest_debug, |
2803 | RUN_ON_CPU_HOST_PTR(&data)); | |
452e4751 | 2804 | return data.err; |
e22a25c9 AL |
2805 | } |
2806 | ||
62278814 | 2807 | int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, |
e22a25c9 AL |
2808 | target_ulong len, int type) |
2809 | { | |
2810 | struct kvm_sw_breakpoint *bp; | |
e22a25c9 AL |
2811 | int err; |
2812 | ||
2813 | if (type == GDB_BREAKPOINT_SW) { | |
80b7cd73 | 2814 | bp = kvm_find_sw_breakpoint(cpu, addr); |
e22a25c9 AL |
2815 | if (bp) { |
2816 | bp->use_count++; | |
2817 | return 0; | |
2818 | } | |
2819 | ||
7267c094 | 2820 | bp = g_malloc(sizeof(struct kvm_sw_breakpoint)); |
e22a25c9 AL |
2821 | bp->pc = addr; |
2822 | bp->use_count = 1; | |
80b7cd73 | 2823 | err = kvm_arch_insert_sw_breakpoint(cpu, bp); |
e22a25c9 | 2824 | if (err) { |
7267c094 | 2825 | g_free(bp); |
e22a25c9 AL |
2826 | return err; |
2827 | } | |
2828 | ||
80b7cd73 | 2829 | QTAILQ_INSERT_HEAD(&cpu->kvm_state->kvm_sw_breakpoints, bp, entry); |
e22a25c9 AL |
2830 | } else { |
2831 | err = kvm_arch_insert_hw_breakpoint(addr, len, type); | |
a426e122 | 2832 | if (err) { |
e22a25c9 | 2833 | return err; |
a426e122 | 2834 | } |
e22a25c9 AL |
2835 | } |
2836 | ||
bdc44640 | 2837 | CPU_FOREACH(cpu) { |
38e478ec | 2838 | err = kvm_update_guest_debug(cpu, 0); |
a426e122 | 2839 | if (err) { |
e22a25c9 | 2840 | return err; |
a426e122 | 2841 | } |
e22a25c9 AL |
2842 | } |
2843 | return 0; | |
2844 | } | |
2845 | ||
62278814 | 2846 | int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr, |
e22a25c9 AL |
2847 | target_ulong len, int type) |
2848 | { | |
2849 | struct kvm_sw_breakpoint *bp; | |
e22a25c9 AL |
2850 | int err; |
2851 | ||
2852 | if (type == GDB_BREAKPOINT_SW) { | |
80b7cd73 | 2853 | bp = kvm_find_sw_breakpoint(cpu, addr); |
a426e122 | 2854 | if (!bp) { |
e22a25c9 | 2855 | return -ENOENT; |
a426e122 | 2856 | } |
e22a25c9 AL |
2857 | |
2858 | if (bp->use_count > 1) { | |
2859 | bp->use_count--; | |
2860 | return 0; | |
2861 | } | |
2862 | ||
80b7cd73 | 2863 | err = kvm_arch_remove_sw_breakpoint(cpu, bp); |
a426e122 | 2864 | if (err) { |
e22a25c9 | 2865 | return err; |
a426e122 | 2866 | } |
e22a25c9 | 2867 | |
80b7cd73 | 2868 | QTAILQ_REMOVE(&cpu->kvm_state->kvm_sw_breakpoints, bp, entry); |
7267c094 | 2869 | g_free(bp); |
e22a25c9 AL |
2870 | } else { |
2871 | err = kvm_arch_remove_hw_breakpoint(addr, len, type); | |
a426e122 | 2872 | if (err) { |
e22a25c9 | 2873 | return err; |
a426e122 | 2874 | } |
e22a25c9 AL |
2875 | } |
2876 | ||
bdc44640 | 2877 | CPU_FOREACH(cpu) { |
38e478ec | 2878 | err = kvm_update_guest_debug(cpu, 0); |
a426e122 | 2879 | if (err) { |
e22a25c9 | 2880 | return err; |
a426e122 | 2881 | } |
e22a25c9 AL |
2882 | } |
2883 | return 0; | |
2884 | } | |
2885 | ||
1d5791f4 | 2886 | void kvm_remove_all_breakpoints(CPUState *cpu) |
e22a25c9 AL |
2887 | { |
2888 | struct kvm_sw_breakpoint *bp, *next; | |
80b7cd73 | 2889 | KVMState *s = cpu->kvm_state; |
dc54e252 | 2890 | CPUState *tmpcpu; |
e22a25c9 | 2891 | |
72cf2d4f | 2892 | QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) { |
80b7cd73 | 2893 | if (kvm_arch_remove_sw_breakpoint(cpu, bp) != 0) { |
e22a25c9 | 2894 | /* Try harder to find a CPU that currently sees the breakpoint. */ |
dc54e252 CG |
2895 | CPU_FOREACH(tmpcpu) { |
2896 | if (kvm_arch_remove_sw_breakpoint(tmpcpu, bp) == 0) { | |
e22a25c9 | 2897 | break; |
a426e122 | 2898 | } |
e22a25c9 AL |
2899 | } |
2900 | } | |
78021d6d JK |
2901 | QTAILQ_REMOVE(&s->kvm_sw_breakpoints, bp, entry); |
2902 | g_free(bp); | |
e22a25c9 AL |
2903 | } |
2904 | kvm_arch_remove_all_hw_breakpoints(); | |
2905 | ||
bdc44640 | 2906 | CPU_FOREACH(cpu) { |
38e478ec | 2907 | kvm_update_guest_debug(cpu, 0); |
a426e122 | 2908 | } |
e22a25c9 AL |
2909 | } |
2910 | ||
2911 | #else /* !KVM_CAP_SET_GUEST_DEBUG */ | |
2912 | ||
38e478ec | 2913 | int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap) |
e22a25c9 AL |
2914 | { |
2915 | return -EINVAL; | |
2916 | } | |
2917 | ||
62278814 | 2918 | int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, |
e22a25c9 AL |
2919 | target_ulong len, int type) |
2920 | { | |
2921 | return -EINVAL; | |
2922 | } | |
2923 | ||
62278814 | 2924 | int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr, |
e22a25c9 AL |
2925 | target_ulong len, int type) |
2926 | { | |
2927 | return -EINVAL; | |
2928 | } | |
2929 | ||
1d5791f4 | 2930 | void kvm_remove_all_breakpoints(CPUState *cpu) |
e22a25c9 AL |
2931 | { |
2932 | } | |
2933 | #endif /* !KVM_CAP_SET_GUEST_DEBUG */ | |
cc84de95 | 2934 | |
18268b60 | 2935 | static int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset) |
cc84de95 | 2936 | { |
aed6efb9 | 2937 | KVMState *s = kvm_state; |
cc84de95 MT |
2938 | struct kvm_signal_mask *sigmask; |
2939 | int r; | |
2940 | ||
7267c094 | 2941 | sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset)); |
cc84de95 | 2942 | |
aed6efb9 | 2943 | sigmask->len = s->sigmask_len; |
cc84de95 | 2944 | memcpy(sigmask->sigset, sigset, sizeof(*sigset)); |
1bc22652 | 2945 | r = kvm_vcpu_ioctl(cpu, KVM_SET_SIGNAL_MASK, sigmask); |
7267c094 | 2946 | g_free(sigmask); |
cc84de95 MT |
2947 | |
2948 | return r; | |
2949 | } | |
4d39892c | 2950 | |
cf0f7cf9 | 2951 | static void kvm_ipi_signal(int sig) |
18268b60 | 2952 | { |
cf0f7cf9 PB |
2953 | if (current_cpu) { |
2954 | assert(kvm_immediate_exit); | |
2955 | kvm_cpu_kick(current_cpu); | |
2956 | } | |
18268b60 PB |
2957 | } |
2958 | ||
2959 | void kvm_init_cpu_signals(CPUState *cpu) | |
2960 | { | |
2961 | int r; | |
2962 | sigset_t set; | |
2963 | struct sigaction sigact; | |
2964 | ||
2965 | memset(&sigact, 0, sizeof(sigact)); | |
cf0f7cf9 | 2966 | sigact.sa_handler = kvm_ipi_signal; |
18268b60 PB |
2967 | sigaction(SIG_IPI, &sigact, NULL); |
2968 | ||
2969 | pthread_sigmask(SIG_BLOCK, NULL, &set); | |
2970 | #if defined KVM_HAVE_MCE_INJECTION | |
2971 | sigdelset(&set, SIGBUS); | |
2972 | pthread_sigmask(SIG_SETMASK, &set, NULL); | |
2973 | #endif | |
2974 | sigdelset(&set, SIG_IPI); | |
cf0f7cf9 PB |
2975 | if (kvm_immediate_exit) { |
2976 | r = pthread_sigmask(SIG_SETMASK, &set, NULL); | |
2977 | } else { | |
2978 | r = kvm_set_signal_mask(cpu, &set); | |
2979 | } | |
18268b60 PB |
2980 | if (r) { |
2981 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); | |
2982 | exit(1); | |
2983 | } | |
2984 | } | |
2985 | ||
2ae41db2 | 2986 | /* Called asynchronously in VCPU thread. */ |
290adf38 | 2987 | int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) |
a1b87fe0 | 2988 | { |
2ae41db2 PB |
2989 | #ifdef KVM_HAVE_MCE_INJECTION |
2990 | if (have_sigbus_pending) { | |
2991 | return 1; | |
2992 | } | |
2993 | have_sigbus_pending = true; | |
2994 | pending_sigbus_addr = addr; | |
2995 | pending_sigbus_code = code; | |
d73415a3 | 2996 | qatomic_set(&cpu->exit_request, 1); |
2ae41db2 PB |
2997 | return 0; |
2998 | #else | |
2999 | return 1; | |
3000 | #endif | |
a1b87fe0 JK |
3001 | } |
3002 | ||
2ae41db2 | 3003 | /* Called synchronously (via signalfd) in main thread. */ |
a1b87fe0 JK |
3004 | int kvm_on_sigbus(int code, void *addr) |
3005 | { | |
2ae41db2 | 3006 | #ifdef KVM_HAVE_MCE_INJECTION |
4d39892c PB |
3007 | /* Action required MCE kills the process if SIGBUS is blocked. Because |
3008 | * that's what happens in the I/O thread, where we handle MCE via signalfd, | |
3009 | * we can only get action optional here. | |
3010 | */ | |
3011 | assert(code != BUS_MCEERR_AR); | |
3012 | kvm_arch_on_sigbus_vcpu(first_cpu, code, addr); | |
3013 | return 0; | |
2ae41db2 PB |
3014 | #else |
3015 | return 1; | |
3016 | #endif | |
a1b87fe0 | 3017 | } |
0a6a7cca CD |
3018 | |
3019 | int kvm_create_device(KVMState *s, uint64_t type, bool test) | |
3020 | { | |
3021 | int ret; | |
3022 | struct kvm_create_device create_dev; | |
3023 | ||
3024 | create_dev.type = type; | |
3025 | create_dev.fd = -1; | |
3026 | create_dev.flags = test ? KVM_CREATE_DEVICE_TEST : 0; | |
3027 | ||
3028 | if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) { | |
3029 | return -ENOTSUP; | |
3030 | } | |
3031 | ||
3032 | ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &create_dev); | |
3033 | if (ret) { | |
3034 | return ret; | |
3035 | } | |
3036 | ||
3037 | return test ? 0 : create_dev.fd; | |
3038 | } | |
ada4135f | 3039 | |
29039acf PX |
3040 | bool kvm_device_supported(int vmfd, uint64_t type) |
3041 | { | |
3042 | struct kvm_create_device create_dev = { | |
3043 | .type = type, | |
3044 | .fd = -1, | |
3045 | .flags = KVM_CREATE_DEVICE_TEST, | |
3046 | }; | |
3047 | ||
3048 | if (ioctl(vmfd, KVM_CHECK_EXTENSION, KVM_CAP_DEVICE_CTRL) <= 0) { | |
3049 | return false; | |
3050 | } | |
3051 | ||
3052 | return (ioctl(vmfd, KVM_CREATE_DEVICE, &create_dev) >= 0); | |
3053 | } | |
3054 | ||
ada4135f CH |
3055 | int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source) |
3056 | { | |
3057 | struct kvm_one_reg reg; | |
3058 | int r; | |
3059 | ||
3060 | reg.id = id; | |
3061 | reg.addr = (uintptr_t) source; | |
3062 | r = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
3063 | if (r) { | |
844a3d34 | 3064 | trace_kvm_failed_reg_set(id, strerror(-r)); |
ada4135f CH |
3065 | } |
3066 | return r; | |
3067 | } | |
3068 | ||
3069 | int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target) | |
3070 | { | |
3071 | struct kvm_one_reg reg; | |
3072 | int r; | |
3073 | ||
3074 | reg.id = id; | |
3075 | reg.addr = (uintptr_t) target; | |
3076 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
3077 | if (r) { | |
844a3d34 | 3078 | trace_kvm_failed_reg_get(id, strerror(-r)); |
ada4135f CH |
3079 | } |
3080 | return r; | |
3081 | } | |
782c3f29 | 3082 | |
8072aae3 AK |
3083 | static bool kvm_accel_has_memory(MachineState *ms, AddressSpace *as, |
3084 | hwaddr start_addr, hwaddr size) | |
3085 | { | |
3086 | KVMState *kvm = KVM_STATE(ms->accelerator); | |
3087 | int i; | |
3088 | ||
3089 | for (i = 0; i < kvm->nr_as; ++i) { | |
3090 | if (kvm->as[i].as == as && kvm->as[i].ml) { | |
023ae9a8 | 3091 | size = MIN(kvm_max_slot_size, size); |
8072aae3 AK |
3092 | return NULL != kvm_lookup_matching_slot(kvm->as[i].ml, |
3093 | start_addr, size); | |
3094 | } | |
3095 | } | |
3096 | ||
3097 | return false; | |
3098 | } | |
3099 | ||
23b0898e PB |
3100 | static void kvm_get_kvm_shadow_mem(Object *obj, Visitor *v, |
3101 | const char *name, void *opaque, | |
3102 | Error **errp) | |
3103 | { | |
3104 | KVMState *s = KVM_STATE(obj); | |
3105 | int64_t value = s->kvm_shadow_mem; | |
3106 | ||
3107 | visit_type_int(v, name, &value, errp); | |
3108 | } | |
3109 | ||
3110 | static void kvm_set_kvm_shadow_mem(Object *obj, Visitor *v, | |
3111 | const char *name, void *opaque, | |
3112 | Error **errp) | |
3113 | { | |
3114 | KVMState *s = KVM_STATE(obj); | |
23b0898e PB |
3115 | int64_t value; |
3116 | ||
70cbae42 PB |
3117 | if (s->fd != -1) { |
3118 | error_setg(errp, "Cannot set properties after the accelerator has been initialized"); | |
3119 | return; | |
3120 | } | |
3121 | ||
668f62ec | 3122 | if (!visit_type_int(v, name, &value, errp)) { |
23b0898e PB |
3123 | return; |
3124 | } | |
3125 | ||
3126 | s->kvm_shadow_mem = value; | |
3127 | } | |
3128 | ||
11bc4a13 PB |
3129 | static void kvm_set_kernel_irqchip(Object *obj, Visitor *v, |
3130 | const char *name, void *opaque, | |
3131 | Error **errp) | |
3132 | { | |
11bc4a13 PB |
3133 | KVMState *s = KVM_STATE(obj); |
3134 | OnOffSplit mode; | |
3135 | ||
70cbae42 PB |
3136 | if (s->fd != -1) { |
3137 | error_setg(errp, "Cannot set properties after the accelerator has been initialized"); | |
3138 | return; | |
3139 | } | |
3140 | ||
14217038 | 3141 | if (!visit_type_OnOffSplit(v, name, &mode, errp)) { |
11bc4a13 | 3142 | return; |
14217038 MA |
3143 | } |
3144 | switch (mode) { | |
3145 | case ON_OFF_SPLIT_ON: | |
3146 | s->kernel_irqchip_allowed = true; | |
3147 | s->kernel_irqchip_required = true; | |
3148 | s->kernel_irqchip_split = ON_OFF_AUTO_OFF; | |
3149 | break; | |
3150 | case ON_OFF_SPLIT_OFF: | |
3151 | s->kernel_irqchip_allowed = false; | |
3152 | s->kernel_irqchip_required = false; | |
3153 | s->kernel_irqchip_split = ON_OFF_AUTO_OFF; | |
3154 | break; | |
3155 | case ON_OFF_SPLIT_SPLIT: | |
3156 | s->kernel_irqchip_allowed = true; | |
3157 | s->kernel_irqchip_required = true; | |
3158 | s->kernel_irqchip_split = ON_OFF_AUTO_ON; | |
3159 | break; | |
3160 | default: | |
3161 | /* The value was checked in visit_type_OnOffSplit() above. If | |
3162 | * we get here, then something is wrong in QEMU. | |
3163 | */ | |
3164 | abort(); | |
11bc4a13 PB |
3165 | } |
3166 | } | |
3167 | ||
4376c40d PB |
3168 | bool kvm_kernel_irqchip_allowed(void) |
3169 | { | |
11bc4a13 | 3170 | return kvm_state->kernel_irqchip_allowed; |
4376c40d PB |
3171 | } |
3172 | ||
3173 | bool kvm_kernel_irqchip_required(void) | |
3174 | { | |
11bc4a13 | 3175 | return kvm_state->kernel_irqchip_required; |
4376c40d PB |
3176 | } |
3177 | ||
3178 | bool kvm_kernel_irqchip_split(void) | |
3179 | { | |
d1972be1 | 3180 | return kvm_state->kernel_irqchip_split == ON_OFF_AUTO_ON; |
4376c40d PB |
3181 | } |
3182 | ||
23b0898e PB |
3183 | static void kvm_accel_instance_init(Object *obj) |
3184 | { | |
3185 | KVMState *s = KVM_STATE(obj); | |
3186 | ||
70cbae42 PB |
3187 | s->fd = -1; |
3188 | s->vmfd = -1; | |
23b0898e | 3189 | s->kvm_shadow_mem = -1; |
d1972be1 XL |
3190 | s->kernel_irqchip_allowed = true; |
3191 | s->kernel_irqchip_split = ON_OFF_AUTO_AUTO; | |
23b0898e PB |
3192 | } |
3193 | ||
782c3f29 EH |
3194 | static void kvm_accel_class_init(ObjectClass *oc, void *data) |
3195 | { | |
3196 | AccelClass *ac = ACCEL_CLASS(oc); | |
3197 | ac->name = "KVM"; | |
0d15da8e | 3198 | ac->init_machine = kvm_init; |
8072aae3 | 3199 | ac->has_memory = kvm_accel_has_memory; |
782c3f29 | 3200 | ac->allowed = &kvm_allowed; |
23b0898e | 3201 | |
11bc4a13 PB |
3202 | object_class_property_add(oc, "kernel-irqchip", "on|off|split", |
3203 | NULL, kvm_set_kernel_irqchip, | |
d2623129 | 3204 | NULL, NULL); |
11bc4a13 | 3205 | object_class_property_set_description(oc, "kernel-irqchip", |
7eecec7d | 3206 | "Configure KVM in-kernel irqchip"); |
11bc4a13 | 3207 | |
23b0898e PB |
3208 | object_class_property_add(oc, "kvm-shadow-mem", "int", |
3209 | kvm_get_kvm_shadow_mem, kvm_set_kvm_shadow_mem, | |
d2623129 | 3210 | NULL, NULL); |
23b0898e | 3211 | object_class_property_set_description(oc, "kvm-shadow-mem", |
7eecec7d | 3212 | "KVM shadow MMU size"); |
782c3f29 EH |
3213 | } |
3214 | ||
3215 | static const TypeInfo kvm_accel_type = { | |
3216 | .name = TYPE_KVM_ACCEL, | |
3217 | .parent = TYPE_ACCEL, | |
23b0898e | 3218 | .instance_init = kvm_accel_instance_init, |
782c3f29 | 3219 | .class_init = kvm_accel_class_init, |
fc02086b | 3220 | .instance_size = sizeof(KVMState), |
782c3f29 EH |
3221 | }; |
3222 | ||
3223 | static void kvm_type_init(void) | |
3224 | { | |
3225 | type_register_static(&kvm_accel_type); | |
3226 | } | |
3227 | ||
3228 | type_init(kvm_type_init); |