| 1 | #ifndef __KVM_HOST_H |
| 2 | #define __KVM_HOST_H |
| 3 | |
| 4 | /* |
| 5 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 6 | * the COPYING file in the top-level directory. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/hardirq.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/signal.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/bug.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/mmu_notifier.h> |
| 19 | #include <linux/preempt.h> |
| 20 | #include <linux/msi.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/rcupdate.h> |
| 23 | #include <linux/ratelimit.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/irqflags.h> |
| 26 | #include <linux/context_tracking.h> |
| 27 | #include <asm/signal.h> |
| 28 | |
| 29 | #include <linux/kvm.h> |
| 30 | #include <linux/kvm_para.h> |
| 31 | |
| 32 | #include <linux/kvm_types.h> |
| 33 | |
| 34 | #include <asm/kvm_host.h> |
| 35 | |
| 36 | /* |
| 37 | * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used |
| 38 | * in kvm, other bits are visible for userspace which are defined in |
| 39 | * include/linux/kvm_h. |
| 40 | */ |
| 41 | #define KVM_MEMSLOT_INVALID (1UL << 16) |
| 42 | #define KVM_MEMSLOT_INCOHERENT (1UL << 17) |
| 43 | |
| 44 | /* Two fragments for cross MMIO pages. */ |
| 45 | #define KVM_MAX_MMIO_FRAGMENTS 2 |
| 46 | |
| 47 | /* |
| 48 | * For the normal pfn, the highest 12 bits should be zero, |
| 49 | * so we can mask bit 62 ~ bit 52 to indicate the error pfn, |
| 50 | * mask bit 63 to indicate the noslot pfn. |
| 51 | */ |
| 52 | #define KVM_PFN_ERR_MASK (0x7ffULL << 52) |
| 53 | #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52) |
| 54 | #define KVM_PFN_NOSLOT (0x1ULL << 63) |
| 55 | |
| 56 | #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK) |
| 57 | #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1) |
| 58 | #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2) |
| 59 | |
| 60 | /* |
| 61 | * error pfns indicate that the gfn is in slot but faild to |
| 62 | * translate it to pfn on host. |
| 63 | */ |
| 64 | static inline bool is_error_pfn(pfn_t pfn) |
| 65 | { |
| 66 | return !!(pfn & KVM_PFN_ERR_MASK); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * error_noslot pfns indicate that the gfn can not be |
| 71 | * translated to pfn - it is not in slot or failed to |
| 72 | * translate it to pfn. |
| 73 | */ |
| 74 | static inline bool is_error_noslot_pfn(pfn_t pfn) |
| 75 | { |
| 76 | return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK); |
| 77 | } |
| 78 | |
| 79 | /* noslot pfn indicates that the gfn is not in slot. */ |
| 80 | static inline bool is_noslot_pfn(pfn_t pfn) |
| 81 | { |
| 82 | return pfn == KVM_PFN_NOSLOT; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * architectures with KVM_HVA_ERR_BAD other than PAGE_OFFSET (e.g. s390) |
| 87 | * provide own defines and kvm_is_error_hva |
| 88 | */ |
| 89 | #ifndef KVM_HVA_ERR_BAD |
| 90 | |
| 91 | #define KVM_HVA_ERR_BAD (PAGE_OFFSET) |
| 92 | #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE) |
| 93 | |
| 94 | static inline bool kvm_is_error_hva(unsigned long addr) |
| 95 | { |
| 96 | return addr >= PAGE_OFFSET; |
| 97 | } |
| 98 | |
| 99 | #endif |
| 100 | |
| 101 | #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT)) |
| 102 | |
| 103 | static inline bool is_error_page(struct page *page) |
| 104 | { |
| 105 | return IS_ERR(page); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * vcpu->requests bit members |
| 110 | */ |
| 111 | #define KVM_REQ_TLB_FLUSH 0 |
| 112 | #define KVM_REQ_MIGRATE_TIMER 1 |
| 113 | #define KVM_REQ_REPORT_TPR_ACCESS 2 |
| 114 | #define KVM_REQ_MMU_RELOAD 3 |
| 115 | #define KVM_REQ_TRIPLE_FAULT 4 |
| 116 | #define KVM_REQ_PENDING_TIMER 5 |
| 117 | #define KVM_REQ_UNHALT 6 |
| 118 | #define KVM_REQ_MMU_SYNC 7 |
| 119 | #define KVM_REQ_CLOCK_UPDATE 8 |
| 120 | #define KVM_REQ_KICK 9 |
| 121 | #define KVM_REQ_DEACTIVATE_FPU 10 |
| 122 | #define KVM_REQ_EVENT 11 |
| 123 | #define KVM_REQ_APF_HALT 12 |
| 124 | #define KVM_REQ_STEAL_UPDATE 13 |
| 125 | #define KVM_REQ_NMI 14 |
| 126 | #define KVM_REQ_PMU 15 |
| 127 | #define KVM_REQ_PMI 16 |
| 128 | #define KVM_REQ_WATCHDOG 17 |
| 129 | #define KVM_REQ_MASTERCLOCK_UPDATE 18 |
| 130 | #define KVM_REQ_MCLOCK_INPROGRESS 19 |
| 131 | #define KVM_REQ_EPR_EXIT 20 |
| 132 | #define KVM_REQ_SCAN_IOAPIC 21 |
| 133 | #define KVM_REQ_GLOBAL_CLOCK_UPDATE 22 |
| 134 | #define KVM_REQ_ENABLE_IBS 23 |
| 135 | #define KVM_REQ_DISABLE_IBS 24 |
| 136 | #define KVM_REQ_APIC_PAGE_RELOAD 25 |
| 137 | #define KVM_REQ_SMI 26 |
| 138 | |
| 139 | #define KVM_USERSPACE_IRQ_SOURCE_ID 0 |
| 140 | #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1 |
| 141 | |
| 142 | extern struct kmem_cache *kvm_vcpu_cache; |
| 143 | |
| 144 | extern spinlock_t kvm_lock; |
| 145 | extern struct list_head vm_list; |
| 146 | |
| 147 | struct kvm_io_range { |
| 148 | gpa_t addr; |
| 149 | int len; |
| 150 | struct kvm_io_device *dev; |
| 151 | }; |
| 152 | |
| 153 | #define NR_IOBUS_DEVS 1000 |
| 154 | |
| 155 | struct kvm_io_bus { |
| 156 | int dev_count; |
| 157 | int ioeventfd_count; |
| 158 | struct kvm_io_range range[]; |
| 159 | }; |
| 160 | |
| 161 | enum kvm_bus { |
| 162 | KVM_MMIO_BUS, |
| 163 | KVM_PIO_BUS, |
| 164 | KVM_VIRTIO_CCW_NOTIFY_BUS, |
| 165 | KVM_FAST_MMIO_BUS, |
| 166 | KVM_NR_BUSES |
| 167 | }; |
| 168 | |
| 169 | int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
| 170 | int len, const void *val); |
| 171 | int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, |
| 172 | gpa_t addr, int len, const void *val, long cookie); |
| 173 | int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
| 174 | int len, void *val); |
| 175 | int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
| 176 | int len, struct kvm_io_device *dev); |
| 177 | int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
| 178 | struct kvm_io_device *dev); |
| 179 | |
| 180 | #ifdef CONFIG_KVM_ASYNC_PF |
| 181 | struct kvm_async_pf { |
| 182 | struct work_struct work; |
| 183 | struct list_head link; |
| 184 | struct list_head queue; |
| 185 | struct kvm_vcpu *vcpu; |
| 186 | struct mm_struct *mm; |
| 187 | gva_t gva; |
| 188 | unsigned long addr; |
| 189 | struct kvm_arch_async_pf arch; |
| 190 | bool wakeup_all; |
| 191 | }; |
| 192 | |
| 193 | void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu); |
| 194 | void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu); |
| 195 | int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva, |
| 196 | struct kvm_arch_async_pf *arch); |
| 197 | int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); |
| 198 | #endif |
| 199 | |
| 200 | enum { |
| 201 | OUTSIDE_GUEST_MODE, |
| 202 | IN_GUEST_MODE, |
| 203 | EXITING_GUEST_MODE, |
| 204 | READING_SHADOW_PAGE_TABLES, |
| 205 | }; |
| 206 | |
| 207 | /* |
| 208 | * Sometimes a large or cross-page mmio needs to be broken up into separate |
| 209 | * exits for userspace servicing. |
| 210 | */ |
| 211 | struct kvm_mmio_fragment { |
| 212 | gpa_t gpa; |
| 213 | void *data; |
| 214 | unsigned len; |
| 215 | }; |
| 216 | |
| 217 | struct kvm_vcpu { |
| 218 | struct kvm *kvm; |
| 219 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 220 | struct preempt_notifier preempt_notifier; |
| 221 | #endif |
| 222 | int cpu; |
| 223 | int vcpu_id; |
| 224 | int srcu_idx; |
| 225 | int mode; |
| 226 | unsigned long requests; |
| 227 | unsigned long guest_debug; |
| 228 | |
| 229 | struct mutex mutex; |
| 230 | struct kvm_run *run; |
| 231 | |
| 232 | int fpu_active; |
| 233 | int guest_fpu_loaded, guest_xcr0_loaded; |
| 234 | unsigned char fpu_counter; |
| 235 | wait_queue_head_t wq; |
| 236 | struct pid *pid; |
| 237 | int sigset_active; |
| 238 | sigset_t sigset; |
| 239 | struct kvm_vcpu_stat stat; |
| 240 | |
| 241 | #ifdef CONFIG_HAS_IOMEM |
| 242 | int mmio_needed; |
| 243 | int mmio_read_completed; |
| 244 | int mmio_is_write; |
| 245 | int mmio_cur_fragment; |
| 246 | int mmio_nr_fragments; |
| 247 | struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS]; |
| 248 | #endif |
| 249 | |
| 250 | #ifdef CONFIG_KVM_ASYNC_PF |
| 251 | struct { |
| 252 | u32 queued; |
| 253 | struct list_head queue; |
| 254 | struct list_head done; |
| 255 | spinlock_t lock; |
| 256 | } async_pf; |
| 257 | #endif |
| 258 | |
| 259 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
| 260 | /* |
| 261 | * Cpu relax intercept or pause loop exit optimization |
| 262 | * in_spin_loop: set when a vcpu does a pause loop exit |
| 263 | * or cpu relax intercepted. |
| 264 | * dy_eligible: indicates whether vcpu is eligible for directed yield. |
| 265 | */ |
| 266 | struct { |
| 267 | bool in_spin_loop; |
| 268 | bool dy_eligible; |
| 269 | } spin_loop; |
| 270 | #endif |
| 271 | bool preempted; |
| 272 | struct kvm_vcpu_arch arch; |
| 273 | }; |
| 274 | |
| 275 | static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu) |
| 276 | { |
| 277 | return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Some of the bitops functions do not support too long bitmaps. |
| 282 | * This number must be determined not to exceed such limits. |
| 283 | */ |
| 284 | #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1) |
| 285 | |
| 286 | struct kvm_memory_slot { |
| 287 | gfn_t base_gfn; |
| 288 | unsigned long npages; |
| 289 | unsigned long *dirty_bitmap; |
| 290 | struct kvm_arch_memory_slot arch; |
| 291 | unsigned long userspace_addr; |
| 292 | u32 flags; |
| 293 | short id; |
| 294 | }; |
| 295 | |
| 296 | static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot) |
| 297 | { |
| 298 | return ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
| 299 | } |
| 300 | |
| 301 | struct kvm_s390_adapter_int { |
| 302 | u64 ind_addr; |
| 303 | u64 summary_addr; |
| 304 | u64 ind_offset; |
| 305 | u32 summary_offset; |
| 306 | u32 adapter_id; |
| 307 | }; |
| 308 | |
| 309 | struct kvm_kernel_irq_routing_entry { |
| 310 | u32 gsi; |
| 311 | u32 type; |
| 312 | int (*set)(struct kvm_kernel_irq_routing_entry *e, |
| 313 | struct kvm *kvm, int irq_source_id, int level, |
| 314 | bool line_status); |
| 315 | union { |
| 316 | struct { |
| 317 | unsigned irqchip; |
| 318 | unsigned pin; |
| 319 | } irqchip; |
| 320 | struct msi_msg msi; |
| 321 | struct kvm_s390_adapter_int adapter; |
| 322 | }; |
| 323 | struct hlist_node link; |
| 324 | }; |
| 325 | |
| 326 | #ifndef KVM_PRIVATE_MEM_SLOTS |
| 327 | #define KVM_PRIVATE_MEM_SLOTS 0 |
| 328 | #endif |
| 329 | |
| 330 | #ifndef KVM_MEM_SLOTS_NUM |
| 331 | #define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
| 332 | #endif |
| 333 | |
| 334 | /* |
| 335 | * Note: |
| 336 | * memslots are not sorted by id anymore, please use id_to_memslot() |
| 337 | * to get the memslot by its id. |
| 338 | */ |
| 339 | struct kvm_memslots { |
| 340 | u64 generation; |
| 341 | struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM]; |
| 342 | /* The mapping table from slot id to the index in memslots[]. */ |
| 343 | short id_to_index[KVM_MEM_SLOTS_NUM]; |
| 344 | atomic_t lru_slot; |
| 345 | int used_slots; |
| 346 | }; |
| 347 | |
| 348 | struct kvm { |
| 349 | spinlock_t mmu_lock; |
| 350 | struct mutex slots_lock; |
| 351 | struct mm_struct *mm; /* userspace tied to this vm */ |
| 352 | struct kvm_memslots *memslots; |
| 353 | struct srcu_struct srcu; |
| 354 | struct srcu_struct irq_srcu; |
| 355 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
| 356 | u32 bsp_vcpu_id; |
| 357 | #endif |
| 358 | struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; |
| 359 | atomic_t online_vcpus; |
| 360 | int last_boosted_vcpu; |
| 361 | struct list_head vm_list; |
| 362 | struct mutex lock; |
| 363 | struct kvm_io_bus *buses[KVM_NR_BUSES]; |
| 364 | #ifdef CONFIG_HAVE_KVM_EVENTFD |
| 365 | struct { |
| 366 | spinlock_t lock; |
| 367 | struct list_head items; |
| 368 | struct list_head resampler_list; |
| 369 | struct mutex resampler_lock; |
| 370 | } irqfds; |
| 371 | struct list_head ioeventfds; |
| 372 | #endif |
| 373 | struct kvm_vm_stat stat; |
| 374 | struct kvm_arch arch; |
| 375 | atomic_t users_count; |
| 376 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 377 | struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; |
| 378 | spinlock_t ring_lock; |
| 379 | struct list_head coalesced_zones; |
| 380 | #endif |
| 381 | |
| 382 | struct mutex irq_lock; |
| 383 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
| 384 | /* |
| 385 | * Update side is protected by irq_lock. |
| 386 | */ |
| 387 | struct kvm_irq_routing_table __rcu *irq_routing; |
| 388 | #endif |
| 389 | #ifdef CONFIG_HAVE_KVM_IRQFD |
| 390 | struct hlist_head irq_ack_notifier_list; |
| 391 | #endif |
| 392 | |
| 393 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 394 | struct mmu_notifier mmu_notifier; |
| 395 | unsigned long mmu_notifier_seq; |
| 396 | long mmu_notifier_count; |
| 397 | #endif |
| 398 | long tlbs_dirty; |
| 399 | struct list_head devices; |
| 400 | }; |
| 401 | |
| 402 | #define kvm_err(fmt, ...) \ |
| 403 | pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) |
| 404 | #define kvm_info(fmt, ...) \ |
| 405 | pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) |
| 406 | #define kvm_debug(fmt, ...) \ |
| 407 | pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) |
| 408 | #define kvm_pr_unimpl(fmt, ...) \ |
| 409 | pr_err_ratelimited("kvm [%i]: " fmt, \ |
| 410 | task_tgid_nr(current), ## __VA_ARGS__) |
| 411 | |
| 412 | /* The guest did something we don't support. */ |
| 413 | #define vcpu_unimpl(vcpu, fmt, ...) \ |
| 414 | kvm_pr_unimpl("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__) |
| 415 | |
| 416 | static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) |
| 417 | { |
| 418 | smp_rmb(); |
| 419 | return kvm->vcpus[i]; |
| 420 | } |
| 421 | |
| 422 | #define kvm_for_each_vcpu(idx, vcpup, kvm) \ |
| 423 | for (idx = 0; \ |
| 424 | idx < atomic_read(&kvm->online_vcpus) && \ |
| 425 | (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \ |
| 426 | idx++) |
| 427 | |
| 428 | #define kvm_for_each_memslot(memslot, slots) \ |
| 429 | for (memslot = &slots->memslots[0]; \ |
| 430 | memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\ |
| 431 | memslot++) |
| 432 | |
| 433 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id); |
| 434 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu); |
| 435 | |
| 436 | int __must_check vcpu_load(struct kvm_vcpu *vcpu); |
| 437 | void vcpu_put(struct kvm_vcpu *vcpu); |
| 438 | |
| 439 | #ifdef __KVM_HAVE_IOAPIC |
| 440 | void kvm_vcpu_request_scan_ioapic(struct kvm *kvm); |
| 441 | #else |
| 442 | static inline void kvm_vcpu_request_scan_ioapic(struct kvm *kvm) |
| 443 | { |
| 444 | } |
| 445 | #endif |
| 446 | |
| 447 | #ifdef CONFIG_HAVE_KVM_IRQFD |
| 448 | int kvm_irqfd_init(void); |
| 449 | void kvm_irqfd_exit(void); |
| 450 | #else |
| 451 | static inline int kvm_irqfd_init(void) |
| 452 | { |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static inline void kvm_irqfd_exit(void) |
| 457 | { |
| 458 | } |
| 459 | #endif |
| 460 | int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, |
| 461 | struct module *module); |
| 462 | void kvm_exit(void); |
| 463 | |
| 464 | void kvm_get_kvm(struct kvm *kvm); |
| 465 | void kvm_put_kvm(struct kvm *kvm); |
| 466 | |
| 467 | static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm) |
| 468 | { |
| 469 | return rcu_dereference_check(kvm->memslots, |
| 470 | srcu_read_lock_held(&kvm->srcu) |
| 471 | || lockdep_is_held(&kvm->slots_lock)); |
| 472 | } |
| 473 | |
| 474 | static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu) |
| 475 | { |
| 476 | return kvm_memslots(vcpu->kvm); |
| 477 | } |
| 478 | |
| 479 | static inline struct kvm_memory_slot * |
| 480 | id_to_memslot(struct kvm_memslots *slots, int id) |
| 481 | { |
| 482 | int index = slots->id_to_index[id]; |
| 483 | struct kvm_memory_slot *slot; |
| 484 | |
| 485 | slot = &slots->memslots[index]; |
| 486 | |
| 487 | WARN_ON(slot->id != id); |
| 488 | return slot; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations: |
| 493 | * - create a new memory slot |
| 494 | * - delete an existing memory slot |
| 495 | * - modify an existing memory slot |
| 496 | * -- move it in the guest physical memory space |
| 497 | * -- just change its flags |
| 498 | * |
| 499 | * Since flags can be changed by some of these operations, the following |
| 500 | * differentiation is the best we can do for __kvm_set_memory_region(): |
| 501 | */ |
| 502 | enum kvm_mr_change { |
| 503 | KVM_MR_CREATE, |
| 504 | KVM_MR_DELETE, |
| 505 | KVM_MR_MOVE, |
| 506 | KVM_MR_FLAGS_ONLY, |
| 507 | }; |
| 508 | |
| 509 | int kvm_set_memory_region(struct kvm *kvm, |
| 510 | const struct kvm_userspace_memory_region *mem); |
| 511 | int __kvm_set_memory_region(struct kvm *kvm, |
| 512 | const struct kvm_userspace_memory_region *mem); |
| 513 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
| 514 | struct kvm_memory_slot *dont); |
| 515 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 516 | unsigned long npages); |
| 517 | void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots); |
| 518 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 519 | struct kvm_memory_slot *memslot, |
| 520 | const struct kvm_userspace_memory_region *mem, |
| 521 | enum kvm_mr_change change); |
| 522 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 523 | const struct kvm_userspace_memory_region *mem, |
| 524 | const struct kvm_memory_slot *old, |
| 525 | const struct kvm_memory_slot *new, |
| 526 | enum kvm_mr_change change); |
| 527 | bool kvm_largepages_enabled(void); |
| 528 | void kvm_disable_largepages(void); |
| 529 | /* flush all memory translations */ |
| 530 | void kvm_arch_flush_shadow_all(struct kvm *kvm); |
| 531 | /* flush memory translations pointing to 'slot' */ |
| 532 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 533 | struct kvm_memory_slot *slot); |
| 534 | |
| 535 | int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
| 536 | struct page **pages, int nr_pages); |
| 537 | |
| 538 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); |
| 539 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn); |
| 540 | unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable); |
| 541 | unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn); |
| 542 | unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn, |
| 543 | bool *writable); |
| 544 | void kvm_release_page_clean(struct page *page); |
| 545 | void kvm_release_page_dirty(struct page *page); |
| 546 | void kvm_set_page_accessed(struct page *page); |
| 547 | |
| 548 | pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn); |
| 549 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); |
| 550 | pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, |
| 551 | bool *writable); |
| 552 | pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn); |
| 553 | pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn); |
| 554 | pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic, |
| 555 | bool *async, bool write_fault, bool *writable); |
| 556 | |
| 557 | void kvm_release_pfn_clean(pfn_t pfn); |
| 558 | void kvm_set_pfn_dirty(pfn_t pfn); |
| 559 | void kvm_set_pfn_accessed(pfn_t pfn); |
| 560 | void kvm_get_pfn(pfn_t pfn); |
| 561 | |
| 562 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 563 | int len); |
| 564 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
| 565 | unsigned long len); |
| 566 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); |
| 567 | int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 568 | void *data, unsigned long len); |
| 569 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 570 | int offset, int len); |
| 571 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 572 | unsigned long len); |
| 573 | int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 574 | void *data, unsigned long len); |
| 575 | int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 576 | gpa_t gpa, unsigned long len); |
| 577 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len); |
| 578 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len); |
| 579 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); |
| 580 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); |
| 581 | unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn); |
| 582 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn); |
| 583 | |
| 584 | struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); |
| 585 | struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); |
| 586 | pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn); |
| 587 | pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); |
| 588 | struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn); |
| 589 | unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn); |
| 590 | unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable); |
| 591 | int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset, |
| 592 | int len); |
| 593 | int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, |
| 594 | unsigned long len); |
| 595 | int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, |
| 596 | unsigned long len); |
| 597 | int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data, |
| 598 | int offset, int len); |
| 599 | int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data, |
| 600 | unsigned long len); |
| 601 | void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn); |
| 602 | |
| 603 | void kvm_vcpu_block(struct kvm_vcpu *vcpu); |
| 604 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu); |
| 605 | int kvm_vcpu_yield_to(struct kvm_vcpu *target); |
| 606 | void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu); |
| 607 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu); |
| 608 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu); |
| 609 | |
| 610 | void kvm_flush_remote_tlbs(struct kvm *kvm); |
| 611 | void kvm_reload_remote_mmus(struct kvm *kvm); |
| 612 | void kvm_make_mclock_inprogress_request(struct kvm *kvm); |
| 613 | void kvm_make_scan_ioapic_request(struct kvm *kvm); |
| 614 | bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req); |
| 615 | |
| 616 | long kvm_arch_dev_ioctl(struct file *filp, |
| 617 | unsigned int ioctl, unsigned long arg); |
| 618 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 619 | unsigned int ioctl, unsigned long arg); |
| 620 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf); |
| 621 | |
| 622 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext); |
| 623 | |
| 624 | int kvm_get_dirty_log(struct kvm *kvm, |
| 625 | struct kvm_dirty_log *log, int *is_dirty); |
| 626 | |
| 627 | int kvm_get_dirty_log_protect(struct kvm *kvm, |
| 628 | struct kvm_dirty_log *log, bool *is_dirty); |
| 629 | |
| 630 | void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 631 | struct kvm_memory_slot *slot, |
| 632 | gfn_t gfn_offset, |
| 633 | unsigned long mask); |
| 634 | |
| 635 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 636 | struct kvm_dirty_log *log); |
| 637 | |
| 638 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, |
| 639 | bool line_status); |
| 640 | long kvm_arch_vm_ioctl(struct file *filp, |
| 641 | unsigned int ioctl, unsigned long arg); |
| 642 | |
| 643 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); |
| 644 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); |
| 645 | |
| 646 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 647 | struct kvm_translation *tr); |
| 648 | |
| 649 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); |
| 650 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); |
| 651 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 652 | struct kvm_sregs *sregs); |
| 653 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 654 | struct kvm_sregs *sregs); |
| 655 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 656 | struct kvm_mp_state *mp_state); |
| 657 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 658 | struct kvm_mp_state *mp_state); |
| 659 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 660 | struct kvm_guest_debug *dbg); |
| 661 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run); |
| 662 | |
| 663 | int kvm_arch_init(void *opaque); |
| 664 | void kvm_arch_exit(void); |
| 665 | |
| 666 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu); |
| 667 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu); |
| 668 | |
| 669 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu); |
| 670 | |
| 671 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu); |
| 672 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); |
| 673 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); |
| 674 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id); |
| 675 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu); |
| 676 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu); |
| 677 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu); |
| 678 | |
| 679 | int kvm_arch_hardware_enable(void); |
| 680 | void kvm_arch_hardware_disable(void); |
| 681 | int kvm_arch_hardware_setup(void); |
| 682 | void kvm_arch_hardware_unsetup(void); |
| 683 | void kvm_arch_check_processor_compat(void *rtn); |
| 684 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); |
| 685 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); |
| 686 | |
| 687 | void *kvm_kvzalloc(unsigned long size); |
| 688 | |
| 689 | #ifndef __KVM_HAVE_ARCH_VM_ALLOC |
| 690 | static inline struct kvm *kvm_arch_alloc_vm(void) |
| 691 | { |
| 692 | return kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 693 | } |
| 694 | |
| 695 | static inline void kvm_arch_free_vm(struct kvm *kvm) |
| 696 | { |
| 697 | kfree(kvm); |
| 698 | } |
| 699 | #endif |
| 700 | |
| 701 | #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA |
| 702 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm); |
| 703 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm); |
| 704 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm); |
| 705 | #else |
| 706 | static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
| 707 | { |
| 708 | } |
| 709 | |
| 710 | static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) |
| 711 | { |
| 712 | } |
| 713 | |
| 714 | static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) |
| 715 | { |
| 716 | return false; |
| 717 | } |
| 718 | #endif |
| 719 | |
| 720 | static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu) |
| 721 | { |
| 722 | #ifdef __KVM_HAVE_ARCH_WQP |
| 723 | return vcpu->arch.wqp; |
| 724 | #else |
| 725 | return &vcpu->wq; |
| 726 | #endif |
| 727 | } |
| 728 | |
| 729 | #ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED |
| 730 | /* |
| 731 | * returns true if the virtual interrupt controller is initialized and |
| 732 | * ready to accept virtual IRQ. On some architectures the virtual interrupt |
| 733 | * controller is dynamically instantiated and this is not always true. |
| 734 | */ |
| 735 | bool kvm_arch_intc_initialized(struct kvm *kvm); |
| 736 | #else |
| 737 | static inline bool kvm_arch_intc_initialized(struct kvm *kvm) |
| 738 | { |
| 739 | return true; |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type); |
| 744 | void kvm_arch_destroy_vm(struct kvm *kvm); |
| 745 | void kvm_arch_sync_events(struct kvm *kvm); |
| 746 | |
| 747 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); |
| 748 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu); |
| 749 | |
| 750 | bool kvm_is_reserved_pfn(pfn_t pfn); |
| 751 | |
| 752 | struct kvm_irq_ack_notifier { |
| 753 | struct hlist_node link; |
| 754 | unsigned gsi; |
| 755 | void (*irq_acked)(struct kvm_irq_ack_notifier *kian); |
| 756 | }; |
| 757 | |
| 758 | int kvm_irq_map_gsi(struct kvm *kvm, |
| 759 | struct kvm_kernel_irq_routing_entry *entries, int gsi); |
| 760 | int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin); |
| 761 | |
| 762 | int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, |
| 763 | bool line_status); |
| 764 | int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level); |
| 765 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm, |
| 766 | int irq_source_id, int level, bool line_status); |
| 767 | bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin); |
| 768 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin); |
| 769 | void kvm_register_irq_ack_notifier(struct kvm *kvm, |
| 770 | struct kvm_irq_ack_notifier *kian); |
| 771 | void kvm_unregister_irq_ack_notifier(struct kvm *kvm, |
| 772 | struct kvm_irq_ack_notifier *kian); |
| 773 | int kvm_request_irq_source_id(struct kvm *kvm); |
| 774 | void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id); |
| 775 | |
| 776 | #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT |
| 777 | int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot); |
| 778 | void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot); |
| 779 | #else |
| 780 | static inline int kvm_iommu_map_pages(struct kvm *kvm, |
| 781 | struct kvm_memory_slot *slot) |
| 782 | { |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static inline void kvm_iommu_unmap_pages(struct kvm *kvm, |
| 787 | struct kvm_memory_slot *slot) |
| 788 | { |
| 789 | } |
| 790 | #endif |
| 791 | |
| 792 | /* must be called with irqs disabled */ |
| 793 | static inline void __kvm_guest_enter(void) |
| 794 | { |
| 795 | guest_enter(); |
| 796 | /* KVM does not hold any references to rcu protected data when it |
| 797 | * switches CPU into a guest mode. In fact switching to a guest mode |
| 798 | * is very similar to exiting to userspace from rcu point of view. In |
| 799 | * addition CPU may stay in a guest mode for quite a long time (up to |
| 800 | * one time slice). Lets treat guest mode as quiescent state, just like |
| 801 | * we do with user-mode execution. |
| 802 | */ |
| 803 | if (!context_tracking_cpu_is_enabled()) |
| 804 | rcu_virt_note_context_switch(smp_processor_id()); |
| 805 | } |
| 806 | |
| 807 | /* must be called with irqs disabled */ |
| 808 | static inline void __kvm_guest_exit(void) |
| 809 | { |
| 810 | guest_exit(); |
| 811 | } |
| 812 | |
| 813 | static inline void kvm_guest_enter(void) |
| 814 | { |
| 815 | unsigned long flags; |
| 816 | |
| 817 | local_irq_save(flags); |
| 818 | __kvm_guest_enter(); |
| 819 | local_irq_restore(flags); |
| 820 | } |
| 821 | |
| 822 | static inline void kvm_guest_exit(void) |
| 823 | { |
| 824 | unsigned long flags; |
| 825 | |
| 826 | local_irq_save(flags); |
| 827 | __kvm_guest_exit(); |
| 828 | local_irq_restore(flags); |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * search_memslots() and __gfn_to_memslot() are here because they are |
| 833 | * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c. |
| 834 | * gfn_to_memslot() itself isn't here as an inline because that would |
| 835 | * bloat other code too much. |
| 836 | */ |
| 837 | static inline struct kvm_memory_slot * |
| 838 | search_memslots(struct kvm_memslots *slots, gfn_t gfn) |
| 839 | { |
| 840 | int start = 0, end = slots->used_slots; |
| 841 | int slot = atomic_read(&slots->lru_slot); |
| 842 | struct kvm_memory_slot *memslots = slots->memslots; |
| 843 | |
| 844 | if (gfn >= memslots[slot].base_gfn && |
| 845 | gfn < memslots[slot].base_gfn + memslots[slot].npages) |
| 846 | return &memslots[slot]; |
| 847 | |
| 848 | while (start < end) { |
| 849 | slot = start + (end - start) / 2; |
| 850 | |
| 851 | if (gfn >= memslots[slot].base_gfn) |
| 852 | end = slot; |
| 853 | else |
| 854 | start = slot + 1; |
| 855 | } |
| 856 | |
| 857 | if (gfn >= memslots[start].base_gfn && |
| 858 | gfn < memslots[start].base_gfn + memslots[start].npages) { |
| 859 | atomic_set(&slots->lru_slot, start); |
| 860 | return &memslots[start]; |
| 861 | } |
| 862 | |
| 863 | return NULL; |
| 864 | } |
| 865 | |
| 866 | static inline struct kvm_memory_slot * |
| 867 | __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn) |
| 868 | { |
| 869 | return search_memslots(slots, gfn); |
| 870 | } |
| 871 | |
| 872 | static inline unsigned long |
| 873 | __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn) |
| 874 | { |
| 875 | return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE; |
| 876 | } |
| 877 | |
| 878 | static inline int memslot_id(struct kvm *kvm, gfn_t gfn) |
| 879 | { |
| 880 | return gfn_to_memslot(kvm, gfn)->id; |
| 881 | } |
| 882 | |
| 883 | static inline gfn_t |
| 884 | hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) |
| 885 | { |
| 886 | gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT; |
| 887 | |
| 888 | return slot->base_gfn + gfn_offset; |
| 889 | } |
| 890 | |
| 891 | static inline gpa_t gfn_to_gpa(gfn_t gfn) |
| 892 | { |
| 893 | return (gpa_t)gfn << PAGE_SHIFT; |
| 894 | } |
| 895 | |
| 896 | static inline gfn_t gpa_to_gfn(gpa_t gpa) |
| 897 | { |
| 898 | return (gfn_t)(gpa >> PAGE_SHIFT); |
| 899 | } |
| 900 | |
| 901 | static inline hpa_t pfn_to_hpa(pfn_t pfn) |
| 902 | { |
| 903 | return (hpa_t)pfn << PAGE_SHIFT; |
| 904 | } |
| 905 | |
| 906 | static inline bool kvm_is_error_gpa(struct kvm *kvm, gpa_t gpa) |
| 907 | { |
| 908 | unsigned long hva = gfn_to_hva(kvm, gpa_to_gfn(gpa)); |
| 909 | |
| 910 | return kvm_is_error_hva(hva); |
| 911 | } |
| 912 | |
| 913 | static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu) |
| 914 | { |
| 915 | set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests); |
| 916 | } |
| 917 | |
| 918 | enum kvm_stat_kind { |
| 919 | KVM_STAT_VM, |
| 920 | KVM_STAT_VCPU, |
| 921 | }; |
| 922 | |
| 923 | struct kvm_stats_debugfs_item { |
| 924 | const char *name; |
| 925 | int offset; |
| 926 | enum kvm_stat_kind kind; |
| 927 | struct dentry *dentry; |
| 928 | }; |
| 929 | extern struct kvm_stats_debugfs_item debugfs_entries[]; |
| 930 | extern struct dentry *kvm_debugfs_dir; |
| 931 | |
| 932 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 933 | static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq) |
| 934 | { |
| 935 | if (unlikely(kvm->mmu_notifier_count)) |
| 936 | return 1; |
| 937 | /* |
| 938 | * Ensure the read of mmu_notifier_count happens before the read |
| 939 | * of mmu_notifier_seq. This interacts with the smp_wmb() in |
| 940 | * mmu_notifier_invalidate_range_end to make sure that the caller |
| 941 | * either sees the old (non-zero) value of mmu_notifier_count or |
| 942 | * the new (incremented) value of mmu_notifier_seq. |
| 943 | * PowerPC Book3s HV KVM calls this under a per-page lock |
| 944 | * rather than under kvm->mmu_lock, for scalability, so |
| 945 | * can't rely on kvm->mmu_lock to keep things ordered. |
| 946 | */ |
| 947 | smp_rmb(); |
| 948 | if (kvm->mmu_notifier_seq != mmu_seq) |
| 949 | return 1; |
| 950 | return 0; |
| 951 | } |
| 952 | #endif |
| 953 | |
| 954 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
| 955 | |
| 956 | #ifdef CONFIG_S390 |
| 957 | #define KVM_MAX_IRQ_ROUTES 4096 //FIXME: we can have more than that... |
| 958 | #else |
| 959 | #define KVM_MAX_IRQ_ROUTES 1024 |
| 960 | #endif |
| 961 | |
| 962 | int kvm_setup_default_irq_routing(struct kvm *kvm); |
| 963 | int kvm_set_irq_routing(struct kvm *kvm, |
| 964 | const struct kvm_irq_routing_entry *entries, |
| 965 | unsigned nr, |
| 966 | unsigned flags); |
| 967 | int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e, |
| 968 | const struct kvm_irq_routing_entry *ue); |
| 969 | void kvm_free_irq_routing(struct kvm *kvm); |
| 970 | |
| 971 | #else |
| 972 | |
| 973 | static inline void kvm_free_irq_routing(struct kvm *kvm) {} |
| 974 | |
| 975 | #endif |
| 976 | |
| 977 | int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi); |
| 978 | |
| 979 | #ifdef CONFIG_HAVE_KVM_EVENTFD |
| 980 | |
| 981 | void kvm_eventfd_init(struct kvm *kvm); |
| 982 | int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args); |
| 983 | |
| 984 | #ifdef CONFIG_HAVE_KVM_IRQFD |
| 985 | int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args); |
| 986 | void kvm_irqfd_release(struct kvm *kvm); |
| 987 | void kvm_irq_routing_update(struct kvm *); |
| 988 | #else |
| 989 | static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) |
| 990 | { |
| 991 | return -EINVAL; |
| 992 | } |
| 993 | |
| 994 | static inline void kvm_irqfd_release(struct kvm *kvm) {} |
| 995 | #endif |
| 996 | |
| 997 | #else |
| 998 | |
| 999 | static inline void kvm_eventfd_init(struct kvm *kvm) {} |
| 1000 | |
| 1001 | static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) |
| 1002 | { |
| 1003 | return -EINVAL; |
| 1004 | } |
| 1005 | |
| 1006 | static inline void kvm_irqfd_release(struct kvm *kvm) {} |
| 1007 | |
| 1008 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
| 1009 | static inline void kvm_irq_routing_update(struct kvm *kvm) |
| 1010 | { |
| 1011 | } |
| 1012 | #endif |
| 1013 | |
| 1014 | static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 1015 | { |
| 1016 | return -ENOSYS; |
| 1017 | } |
| 1018 | |
| 1019 | #endif /* CONFIG_HAVE_KVM_EVENTFD */ |
| 1020 | |
| 1021 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
| 1022 | static inline bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) |
| 1023 | { |
| 1024 | return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id; |
| 1025 | } |
| 1026 | |
| 1027 | static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) |
| 1028 | { |
| 1029 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; |
| 1030 | } |
| 1031 | |
| 1032 | bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu); |
| 1033 | |
| 1034 | #else |
| 1035 | |
| 1036 | static inline bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; } |
| 1037 | |
| 1038 | #endif |
| 1039 | |
| 1040 | static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu) |
| 1041 | { |
| 1042 | set_bit(req, &vcpu->requests); |
| 1043 | } |
| 1044 | |
| 1045 | static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) |
| 1046 | { |
| 1047 | if (test_bit(req, &vcpu->requests)) { |
| 1048 | clear_bit(req, &vcpu->requests); |
| 1049 | return true; |
| 1050 | } else { |
| 1051 | return false; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | extern bool kvm_rebooting; |
| 1056 | |
| 1057 | struct kvm_device { |
| 1058 | struct kvm_device_ops *ops; |
| 1059 | struct kvm *kvm; |
| 1060 | void *private; |
| 1061 | struct list_head vm_node; |
| 1062 | }; |
| 1063 | |
| 1064 | /* create, destroy, and name are mandatory */ |
| 1065 | struct kvm_device_ops { |
| 1066 | const char *name; |
| 1067 | int (*create)(struct kvm_device *dev, u32 type); |
| 1068 | |
| 1069 | /* |
| 1070 | * Destroy is responsible for freeing dev. |
| 1071 | * |
| 1072 | * Destroy may be called before or after destructors are called |
| 1073 | * on emulated I/O regions, depending on whether a reference is |
| 1074 | * held by a vcpu or other kvm component that gets destroyed |
| 1075 | * after the emulated I/O. |
| 1076 | */ |
| 1077 | void (*destroy)(struct kvm_device *dev); |
| 1078 | |
| 1079 | int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr); |
| 1080 | int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr); |
| 1081 | int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr); |
| 1082 | long (*ioctl)(struct kvm_device *dev, unsigned int ioctl, |
| 1083 | unsigned long arg); |
| 1084 | }; |
| 1085 | |
| 1086 | void kvm_device_get(struct kvm_device *dev); |
| 1087 | void kvm_device_put(struct kvm_device *dev); |
| 1088 | struct kvm_device *kvm_device_from_filp(struct file *filp); |
| 1089 | int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type); |
| 1090 | void kvm_unregister_device_ops(u32 type); |
| 1091 | |
| 1092 | extern struct kvm_device_ops kvm_mpic_ops; |
| 1093 | extern struct kvm_device_ops kvm_xics_ops; |
| 1094 | extern struct kvm_device_ops kvm_arm_vgic_v2_ops; |
| 1095 | extern struct kvm_device_ops kvm_arm_vgic_v3_ops; |
| 1096 | |
| 1097 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
| 1098 | |
| 1099 | static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val) |
| 1100 | { |
| 1101 | vcpu->spin_loop.in_spin_loop = val; |
| 1102 | } |
| 1103 | static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) |
| 1104 | { |
| 1105 | vcpu->spin_loop.dy_eligible = val; |
| 1106 | } |
| 1107 | |
| 1108 | #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ |
| 1109 | |
| 1110 | static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val) |
| 1111 | { |
| 1112 | } |
| 1113 | |
| 1114 | static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) |
| 1115 | { |
| 1116 | } |
| 1117 | #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ |
| 1118 | #endif |
| 1119 | |