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